f16c5b2c7d03ba5f0c0e235358bb8f706033ecf2
[strongswan.git] / src / libstrongswan / utils / mutex.c
1 /*
2 * Copyright (C) 2008 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 *
15 * $Id$
16 */
17
18 #include "mutex.h"
19
20 #include <library.h>
21 #include <debug.h>
22
23 #include <pthread.h>
24 #include <sys/time.h>
25 #include <time.h>
26 #include <errno.h>
27
28
29 typedef struct private_mutex_t private_mutex_t;
30 typedef struct private_n_mutex_t private_n_mutex_t;
31 typedef struct private_r_mutex_t private_r_mutex_t;
32 typedef struct private_condvar_t private_condvar_t;
33
34 /**
35 * private data of mutex
36 */
37 struct private_mutex_t {
38
39 /**
40 * public functions
41 */
42 mutex_t public;
43
44 /**
45 * wrapped pthread mutex
46 */
47 pthread_mutex_t mutex;
48 };
49
50 /**
51 * private data of mutex, extended by recursive locking information
52 */
53 struct private_r_mutex_t {
54
55 /**
56 * public functions
57 */
58 private_mutex_t generic;
59
60 /**
61 * thread which currently owns mutex
62 */
63 pthread_t thread;
64
65 /**
66 * times we have locked the lock
67 */
68 int times;
69 };
70
71 /**
72 * private data of condvar
73 */
74 struct private_condvar_t {
75
76 /**
77 * public functions
78 */
79 condvar_t public;
80
81 /**
82 * wrapped pthread condvar
83 */
84 pthread_cond_t condvar;
85 };
86
87 /**
88 * Implementation of mutex_t.lock.
89 */
90 static void lock(private_mutex_t *this)
91 {
92 if (pthread_mutex_lock(&this->mutex))
93 {
94 DBG1("!!!! MUTEX %sLOCK ERROR, your code is buggy !!!", "");
95 }
96 }
97
98 /**
99 * Implementation of mutex_t.unlock.
100 */
101 static void unlock(private_mutex_t *this)
102 {
103 if (pthread_mutex_unlock(&this->mutex))
104 {
105 DBG1("!!!! MUTEX %sLOCK ERROR, your code is buggy !!!", "UN");
106 }
107 }
108
109 /**
110 * Implementation of mutex_t.lock.
111 */
112 static void lock_r(private_r_mutex_t *this)
113 {
114 pthread_t self = pthread_self();
115
116 if (this->thread == self)
117 {
118 this->times++;
119 return;
120 }
121 lock(&this->generic);
122 this->thread = self;
123 this->times = 1;
124 }
125
126 /**
127 * Implementation of mutex_t.unlock.
128 */
129 static void unlock_r(private_r_mutex_t *this)
130 {
131 if (--this->times == 0)
132 {
133 this->thread = 0;
134 unlock(&this->generic);
135 }
136 }
137
138 /**
139 * Implementation of mutex_t.destroy
140 */
141 static void mutex_destroy(private_mutex_t *this)
142 {
143 pthread_mutex_destroy(&this->mutex);
144 free(this);
145 }
146
147 /*
148 * see header file
149 */
150 mutex_t *mutex_create(mutex_type_t type)
151 {
152 switch (type)
153 {
154 case MUTEX_RECURSIVE:
155 {
156 private_r_mutex_t *this = malloc_thing(private_r_mutex_t);
157
158 this->generic.public.lock = (void(*)(mutex_t*))lock_r;
159 this->generic.public.unlock = (void(*)(mutex_t*))unlock_r;
160 this->generic.public.destroy = (void(*)(mutex_t*))mutex_destroy;
161
162 pthread_mutex_init(&this->generic.mutex, NULL);
163 this->thread = 0;
164 this->times = 0;
165
166 return &this->generic.public;
167 }
168 case MUTEX_DEFAULT:
169 default:
170 {
171 private_mutex_t *this = malloc_thing(private_mutex_t);
172
173 this->public.lock = (void(*)(mutex_t*))lock;
174 this->public.unlock = (void(*)(mutex_t*))unlock;
175 this->public.destroy = (void(*)(mutex_t*))mutex_destroy;
176
177 pthread_mutex_init(&this->mutex, NULL);
178
179 return &this->public;
180 }
181 }
182 }
183
184 /**
185 * Implementation of condvar_t.wait.
186 */
187 static void wait(private_condvar_t *this, private_mutex_t *mutex)
188 {
189 pthread_cond_wait(&this->condvar, &mutex->mutex);
190 }
191
192 /**
193 * Implementation of condvar_t.timed_wait.
194 */
195 static bool timed_wait(private_condvar_t *this, private_mutex_t *mutex,
196 u_int timeout)
197 {
198 struct timespec ts;
199 struct timeval tv;
200 u_int s, ms;
201
202 gettimeofday(&tv, NULL);
203
204 s = timeout / 1000;
205 ms = timeout % 1000;
206
207 ts.tv_sec = tv.tv_sec + s;
208 ts.tv_nsec = tv.tv_usec * 1000 + ms * 1000000;
209 if (ts.tv_nsec > 1000000000 /* 1s */)
210 {
211 ts.tv_nsec -= 1000000000;
212 ts.tv_sec++;
213 }
214 return (pthread_cond_timedwait(&this->condvar, &mutex->mutex,
215 &ts) == ETIMEDOUT);
216 }
217
218 /**
219 * Implementation of condvar_t.signal.
220 */
221 static void signal(private_condvar_t *this)
222 {
223 pthread_cond_signal(&this->condvar);
224 }
225
226 /**
227 * Implementation of condvar_t.broadcast.
228 */
229 static void broadcast(private_condvar_t *this)
230 {
231 pthread_cond_broadcast(&this->condvar);
232 }
233
234 /**
235 * Implementation of condvar_t.destroy
236 */
237 static void condvar_destroy(private_condvar_t *this)
238 {
239 pthread_cond_destroy(&this->condvar);
240 free(this);
241 }
242
243 /*
244 * see header file
245 */
246 condvar_t *condvar_create(condvar_type_t type)
247 {
248 switch (type)
249 {
250 case CONDVAR_DEFAULT:
251 default:
252 {
253 private_condvar_t *this = malloc_thing(private_condvar_t);
254
255 this->public.wait = (void(*)(condvar_t*, mutex_t *mutex))wait;
256 this->public.timed_wait = (bool(*)(condvar_t*, mutex_t *mutex, u_int timeout))timed_wait;
257 this->public.signal = (void(*)(condvar_t*))signal;
258 this->public.broadcast = (void(*)(condvar_t*))broadcast;
259 this->public.destroy = (void(*)(condvar_t*))condvar_destroy;
260
261 pthread_cond_init(&this->condvar, NULL);
262
263 return &this->public;
264 }
265 }
266 }
267