]>
git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/unix_semaphore.cc
2 PowerDNS Versatile Database Driven Nameserver
3 Copyright (C) 2002 - 2005 PowerDNS.COM BV
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License version 2 as
7 published by the Free Software Foundation
9 Additionally, the license of this program contains a special
10 exception which allows to distribute the program in binary form when
11 it is linked against OpenSSL.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
29 #include "pdnsexception.hh"
34 #include <sys/types.h>
37 #if DARWIN || _AIX || __APPLE__
39 // Darwin 6.0 Compatible implementation, uses pthreads so it portable across more platforms.
41 #define SEM_VALUE_MAX 32767
42 #define SEM_MAGIC ((uint32_t) 0x09fa4012)
44 Semaphore::Semaphore(unsigned int value
)
46 if (value
> SEM_VALUE_MAX
) {
47 throw PDNSException("Cannot create semaphore: value too large");
52 if (pthread_mutex_init(&m_lock
, NULL
) != 0) {
53 throw PDNSException("Cannot create semaphore: cannot allocate mutex");
56 if (pthread_cond_init(&m_gtzero
, NULL
) != 0) {
57 pthread_mutex_destroy(&m_lock
);
58 throw PDNSException("Cannot create semaphore: cannot allocate condition");
61 m_count
= (uint32_t) value
;
68 pthread_mutex_lock(&m_lock
);
72 pthread_cond_signal(&m_gtzero
);
75 pthread_mutex_unlock(&m_lock
);
82 pthread_mutex_lock(&m_lock
);
84 while (m_count
== 0) {
86 pthread_cond_wait(&m_gtzero
, &m_lock
);
92 pthread_mutex_unlock(&m_lock
);
97 int Semaphore::tryWait()
101 pthread_mutex_lock(&m_lock
);
110 pthread_mutex_unlock(&m_lock
);
115 int Semaphore::getValue(Semaphore::sem_value_t
*sval
)
117 pthread_mutex_lock(&m_lock
);
119 pthread_mutex_unlock(&m_lock
);
124 Semaphore::~Semaphore()
126 // Make sure there are no waiters.
128 pthread_mutex_lock(&m_lock
);
129 if (m_nwaiters
> 0) {
130 pthread_mutex_unlock(&m_lock
);
134 pthread_mutex_unlock(&m_lock
);
138 pthread_mutex_destroy(&m_lock
);
139 pthread_cond_destroy(&m_gtzero
);
145 #else /* not DARWIN from here on */
148 Semaphore::Semaphore(unsigned int value
)
150 m_pSemaphore
=new sem_t
;
151 if (sem_init(m_pSemaphore
, 0, value
) == -1) {
152 theL() << Logger::Error
<< "Cannot create semaphore: " << stringerror() << endl
;
157 int Semaphore::post()
159 return sem_post(m_pSemaphore
);
162 int Semaphore::wait()
166 ret
= sem_wait(m_pSemaphore
);
167 while (ret
== -1 && errno
== EINTR
);
170 int Semaphore::tryWait()
172 return sem_trywait(m_pSemaphore
);
175 int Semaphore::getValue(Semaphore::sem_value_t
*sval
)
177 return sem_getvalue(m_pSemaphore
, sval
);
180 Semaphore::~Semaphore()