• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <time.h>
34 #include <limits.h>
35 #include "CSemaphore.h"
36 #include <stdio.h>
37 
38 //------------------------------------------------------------------------------
CSemaphore(int size)39 CSemaphore::CSemaphore(int size) : m_waiters_count(0), m_count(size)
40 {
41     pthread_mutex_init(&m_mutex, NULL);
42     pthread_cond_init(&m_cond, NULL);
43 }
44 
45 
46 //------------------------------------------------------------------------------
~CSemaphore()47 CSemaphore::~CSemaphore()
48 {
49     pthread_mutex_destroy(&m_mutex);
50     pthread_cond_destroy(&m_cond);
51 }
52 
53 
54 //------------------------------------------------------------------------------
wait()55 void CSemaphore::wait()
56 {
57     pthread_mutex_lock(&m_mutex);
58     m_waiters_count ++;
59     while ( m_count == 0 )
60         pthread_cond_wait(&m_cond, &m_mutex);
61     m_waiters_count --;
62     m_count --;
63     pthread_mutex_unlock(&m_mutex);
64 }
65 
66 //------------------------------------------------------------------------------
wait(int sec)67 bool CSemaphore::wait(int sec)
68 {
69     int rc = 0;
70     struct timespec tm;
71     if (sec < 0)
72         sec = LONG_MAX;
73     clock_gettime(CLOCK_REALTIME, &tm);
74     tm.tv_sec += sec;
75 
76     pthread_mutex_lock(&m_mutex);
77     m_waiters_count ++;
78     if ( m_count == 0 ) {
79         rc = pthread_cond_timedwait(&m_cond, &m_mutex, &tm);
80     }
81     m_waiters_count --;
82     // Decrement only if waiting actually succeeded, otherwise we
83     // just timed out
84     if (!rc)
85         m_count --;
86     pthread_mutex_unlock(&m_mutex);
87     return (rc == 0);
88 }
89 
90 
91 //------------------------------------------------------------------------------
wouldWait()92 bool CSemaphore::wouldWait()
93 {
94     bool ret = false;
95     pthread_mutex_lock(&m_mutex);
96     if ( m_count == 0 )
97         ret = true;
98     pthread_mutex_unlock(&m_mutex);
99     return ret;
100 }
101 
102 
103 //------------------------------------------------------------------------------
signal()104 void CSemaphore::signal()
105 {
106     pthread_mutex_lock(&m_mutex);
107     if ( m_waiters_count > 0 )
108         pthread_cond_signal(&m_cond);
109     m_count ++;
110     pthread_mutex_unlock(&m_mutex);
111 }
112 
113 /** @} */
114