]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/unix_semaphore.cc
add OpenSSL exception to PowerDNS, Netherlabs, van Dijk and Hubert copyrights
[thirdparty/pdns.git] / pdns / unix_semaphore.cc
1 /*
2 PowerDNS Versatile Database Driven Nameserver
3 Copyright (C) 2002 - 2005 PowerDNS.COM BV
4
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
8
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.
12
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.
17
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
21 */
22
23
24 #include "utility.hh"
25 #include <cstring>
26 #include <fcntl.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include "pdnsexception.hh"
30 #include "logger.hh"
31 #include "misc.hh"
32 #include <pwd.h>
33 #include <grp.h>
34 #include <sys/types.h>
35
36
37 #if DARWIN || _AIX || __APPLE__
38
39 // Darwin 6.0 Compatible implementation, uses pthreads so it portable across more platforms.
40
41 #define SEM_VALUE_MAX 32767
42 #define SEM_MAGIC ((uint32_t) 0x09fa4012)
43
44 Semaphore::Semaphore(unsigned int value)
45 {
46 if (value > SEM_VALUE_MAX) {
47 throw PDNSException("Cannot create semaphore: value too large");
48 }
49
50 // Initialize
51
52 if (pthread_mutex_init(&m_lock, NULL) != 0) {
53 throw PDNSException("Cannot create semaphore: cannot allocate mutex");
54 }
55
56 if (pthread_cond_init(&m_gtzero, NULL) != 0) {
57 pthread_mutex_destroy(&m_lock);
58 throw PDNSException("Cannot create semaphore: cannot allocate condition");
59 }
60
61 m_count = (uint32_t) value;
62 m_nwaiters = 0;
63 m_magic = SEM_MAGIC;
64 }
65
66 int Semaphore::post()
67 {
68 pthread_mutex_lock(&m_lock);
69
70 m_count++;
71 if (m_nwaiters > 0) {
72 pthread_cond_signal(&m_gtzero);
73 }
74
75 pthread_mutex_unlock(&m_lock);
76
77 return 0;
78 }
79
80 int Semaphore::wait()
81 {
82 pthread_mutex_lock(&m_lock);
83
84 while (m_count == 0) {
85 m_nwaiters++;
86 pthread_cond_wait(&m_gtzero, &m_lock);
87 m_nwaiters--;
88 }
89
90 m_count--;
91
92 pthread_mutex_unlock(&m_lock);
93
94 return 0;
95 }
96
97 int Semaphore::tryWait()
98 {
99 int retval = 0;
100
101 pthread_mutex_lock(&m_lock);
102
103 if (m_count > 0) {
104 m_count--;
105 } else {
106 errno = EAGAIN;
107 retval = -1;
108 }
109
110 pthread_mutex_unlock(&m_lock);
111
112 return retval;
113 }
114
115 int Semaphore::getValue(Semaphore::sem_value_t *sval)
116 {
117 pthread_mutex_lock(&m_lock);
118 *sval = m_count;
119 pthread_mutex_unlock(&m_lock);
120
121 return 0;
122 }
123
124 Semaphore::~Semaphore()
125 {
126 // Make sure there are no waiters.
127
128 pthread_mutex_lock(&m_lock);
129 if (m_nwaiters > 0) {
130 pthread_mutex_unlock(&m_lock);
131 //errno = EBUSY;
132 //return -1;
133 }
134 pthread_mutex_unlock(&m_lock);
135
136 // Destroy it.
137
138 pthread_mutex_destroy(&m_lock);
139 pthread_cond_destroy(&m_gtzero);
140 m_magic = 0;
141
142 //return 0;
143 }
144
145 #else /* not DARWIN from here on */
146
147
148 Semaphore::Semaphore(unsigned int value)
149 {
150 m_pSemaphore=new sem_t;
151 if (sem_init(m_pSemaphore, 0, value) == -1) {
152 theL() << Logger::Error << "Cannot create semaphore: " << stringerror() << endl;
153 exit(1);
154 }
155 }
156
157 int Semaphore::post()
158 {
159 return sem_post(m_pSemaphore);
160 }
161
162 int Semaphore::wait()
163 {
164 int ret;
165 do
166 ret = sem_wait(m_pSemaphore);
167 while (ret == -1 && errno == EINTR);
168 return ret;
169 }
170 int Semaphore::tryWait()
171 {
172 return sem_trywait(m_pSemaphore);
173 }
174
175 int Semaphore::getValue(Semaphore::sem_value_t *sval)
176 {
177 return sem_getvalue(m_pSemaphore, sval);
178 }
179
180 Semaphore::~Semaphore()
181 {
182 }
183
184 #endif