Home | History | Annotate | Download | only in Common
      1 /** @addtogroup MCD_MCDIMPL_DAEMON_SRV
      2  * @{
      3  * @file
      4  *
      5  * Semaphore implementation (pthread wrapper).
      6  *
      7  * <!-- Copyright Giesecke & Devrient GmbH 2009 - 2012 -->
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. The name of the author may not be used to endorse or promote
     18  *    products derived from this software without specific prior
     19  *    written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     22  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     23  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     25  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     27  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     29  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     30  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     31  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 #ifndef CSEMAPHORE_H_
     34 #define CSEMAPHORE_H_
     35 
     36 #include "pthread.h"
     37 
     38 /**
     39  * Could inherit from CMutex, or use CMutex internally.
     40  * Semaphore is a mutex with a counter. Constructor and destructor
     41  * code is the same.
     42  */
     43 
     44 class CSemaphore
     45 {
     46 
     47 public:
     48 
     49     CSemaphore(int size = 0);
     50 
     51     ~CSemaphore(void);
     52 
     53     void wait(void);
     54     bool wait(int sec);
     55 
     56     bool wouldWait(void);
     57 
     58     void signal(void);
     59 
     60 private:
     61 
     62     pthread_mutex_t m_mutex;
     63     pthread_cond_t m_cond;
     64     int m_waiters_count;
     65     int m_count;
     66 
     67 };
     68 
     69 #endif /*CSEMAPHORE_H_*/
     70 
     71 /** @} */
     72