Use actual recursive pthreads mutexes, rather than NIH'ing them, wrong
[platform/upstream/dbus.git] / dbus / dbus-sysdeps-pthread.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-sysdeps-pthread.c Implements threads using pthreads (internal to libdbus)
3  * 
4  * Copyright (C) 2002, 2003, 2006  Red Hat, Inc.
5  *
6  * Licensed under the Academic Free License version 2.1
7  * 
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
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 Street, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23
24 #include <config.h>
25 #include "dbus-internals.h"
26 #include "dbus-sysdeps.h"
27 #include "dbus-threads.h"
28
29 #include <sys/time.h>
30 #include <pthread.h>
31 #include <string.h>
32
33 #ifdef HAVE_ERRNO_H
34 #include <errno.h>
35 #endif
36
37 #include <config.h>
38
39 /* Whether we have a "monotonic" clock; i.e. a clock not affected by
40  * changes in system time.
41  * This is initialized once in check_monotonic_clock below.
42  * https://bugs.freedesktop.org/show_bug.cgi?id=18121
43  */
44 static dbus_bool_t have_monotonic_clock = 0;
45
46 typedef struct {
47   pthread_mutex_t lock; /**< the lock */
48 } DBusMutexPThread;
49
50 typedef struct {
51   pthread_cond_t cond; /**< the condition */
52 } DBusCondVarPThread;
53
54 #define DBUS_MUTEX(m)         ((DBusMutex*) m)
55 #define DBUS_MUTEX_PTHREAD(m) ((DBusMutexPThread*) m)
56
57 #define DBUS_COND_VAR(c)         ((DBusCondVar*) c)
58 #define DBUS_COND_VAR_PTHREAD(c) ((DBusCondVarPThread*) c)
59
60
61 #ifdef DBUS_DISABLE_ASSERT
62 /* (tmp != 0) is a no-op usage to silence compiler */
63 #define PTHREAD_CHECK(func_name, result_or_call)    \
64     do { int tmp = (result_or_call); if (tmp != 0) {;} } while (0)
65 #else
66 #define PTHREAD_CHECK(func_name, result_or_call) do {                                  \
67     int tmp = (result_or_call);                                                        \
68     if (tmp != 0) {                                                                    \
69       _dbus_warn_check_failed ("pthread function %s failed with %d %s in %s\n",        \
70                                func_name, tmp, strerror(tmp), _DBUS_FUNCTION_NAME);    \
71     }                                                                                  \
72 } while (0)
73 #endif /* !DBUS_DISABLE_ASSERT */
74
75 static DBusMutex *
76 _dbus_pthread_cmutex_new (void)
77 {
78   DBusMutexPThread *pmutex;
79   int result;
80
81   pmutex = dbus_new (DBusMutexPThread, 1);
82   if (pmutex == NULL)
83     return NULL;
84
85   result = pthread_mutex_init (&pmutex->lock, NULL);
86
87   if (result == ENOMEM || result == EAGAIN)
88     {
89       dbus_free (pmutex);
90       return NULL;
91     }
92   else
93     {
94       PTHREAD_CHECK ("pthread_mutex_init", result);
95     }
96
97   return DBUS_MUTEX (pmutex);
98 }
99
100 static DBusMutex *
101 _dbus_pthread_rmutex_new (void)
102 {
103   DBusMutexPThread *pmutex;
104   pthread_mutexattr_t mutexattr;
105   int result;
106
107   pmutex = dbus_new (DBusMutexPThread, 1);
108   if (pmutex == NULL)
109     return NULL;
110
111   pthread_mutexattr_init (&mutexattr);
112   pthread_mutexattr_settype (&mutexattr, PTHREAD_MUTEX_RECURSIVE);
113   result = pthread_mutex_init (&pmutex->lock, &mutexattr);
114   pthread_mutexattr_destroy (&mutexattr);
115
116   if (result == ENOMEM || result == EAGAIN)
117     {
118       dbus_free (pmutex);
119       return NULL;
120     }
121   else
122     {
123       PTHREAD_CHECK ("pthread_mutex_init", result);
124     }
125
126   return DBUS_MUTEX (pmutex);
127 }
128
129 static void
130 _dbus_pthread_mutex_free (DBusMutex *mutex)
131 {
132   DBusMutexPThread *pmutex = DBUS_MUTEX_PTHREAD (mutex);
133
134   PTHREAD_CHECK ("pthread_mutex_destroy", pthread_mutex_destroy (&pmutex->lock));
135   dbus_free (pmutex);
136 }
137
138 static dbus_bool_t
139 _dbus_pthread_mutex_lock (DBusMutex *mutex)
140 {
141   DBusMutexPThread *pmutex = DBUS_MUTEX_PTHREAD (mutex);
142
143   PTHREAD_CHECK ("pthread_mutex_lock", pthread_mutex_lock (&pmutex->lock));
144   return TRUE;
145 }
146
147 static dbus_bool_t
148 _dbus_pthread_mutex_unlock (DBusMutex *mutex)
149 {
150   DBusMutexPThread *pmutex = DBUS_MUTEX_PTHREAD (mutex);
151
152   PTHREAD_CHECK ("pthread_mutex_unlock", pthread_mutex_unlock (&pmutex->lock));
153   return TRUE;
154 }
155
156 static DBusCondVar *
157 _dbus_pthread_condvar_new (void)
158 {
159   DBusCondVarPThread *pcond;
160   pthread_condattr_t attr;
161   int result;
162   
163   pcond = dbus_new (DBusCondVarPThread, 1);
164   if (pcond == NULL)
165     return NULL;
166
167   pthread_condattr_init (&attr);
168 #ifdef HAVE_MONOTONIC_CLOCK
169   if (have_monotonic_clock)
170     pthread_condattr_setclock (&attr, CLOCK_MONOTONIC);
171 #endif
172
173   result = pthread_cond_init (&pcond->cond, &attr);
174   pthread_condattr_destroy (&attr);
175
176   if (result == EAGAIN || result == ENOMEM)
177     {
178       dbus_free (pcond);
179       return NULL;
180     }
181   else
182     {
183       PTHREAD_CHECK ("pthread_cond_init", result);
184     }
185   
186   return DBUS_COND_VAR (pcond);
187 }
188
189 static void
190 _dbus_pthread_condvar_free (DBusCondVar *cond)
191 {  
192   DBusCondVarPThread *pcond = DBUS_COND_VAR_PTHREAD (cond);
193   
194   PTHREAD_CHECK ("pthread_cond_destroy", pthread_cond_destroy (&pcond->cond));
195
196   dbus_free (pcond);
197 }
198
199 static void
200 _dbus_pthread_condvar_wait (DBusCondVar *cond,
201                             DBusMutex   *mutex)
202 {
203   DBusMutexPThread *pmutex = DBUS_MUTEX_PTHREAD (mutex);
204   DBusCondVarPThread *pcond = DBUS_COND_VAR_PTHREAD (cond);
205
206   PTHREAD_CHECK ("pthread_cond_wait", pthread_cond_wait (&pcond->cond, &pmutex->lock));
207 }
208
209 static dbus_bool_t
210 _dbus_pthread_condvar_wait_timeout (DBusCondVar               *cond,
211                                     DBusMutex                 *mutex,
212                                     int                        timeout_milliseconds)
213 {
214   DBusMutexPThread *pmutex = DBUS_MUTEX_PTHREAD (mutex);
215   DBusCondVarPThread *pcond = DBUS_COND_VAR_PTHREAD (cond);
216   struct timeval time_now;
217   struct timespec end_time;
218   int result;
219
220 #ifdef HAVE_MONOTONIC_CLOCK
221   if (have_monotonic_clock)
222     {
223       struct timespec monotonic_timer;
224       clock_gettime (CLOCK_MONOTONIC,&monotonic_timer);
225       time_now.tv_sec = monotonic_timer.tv_sec;
226       time_now.tv_usec = monotonic_timer.tv_nsec / 1000;
227     }
228   else
229     /* This else falls through to gettimeofday */
230 #endif
231   gettimeofday (&time_now, NULL);
232   
233   end_time.tv_sec = time_now.tv_sec + timeout_milliseconds / 1000;
234   end_time.tv_nsec = (time_now.tv_usec + (timeout_milliseconds % 1000) * 1000) * 1000;
235   if (end_time.tv_nsec > 1000*1000*1000)
236     {
237       end_time.tv_sec += 1;
238       end_time.tv_nsec -= 1000*1000*1000;
239     }
240
241   result = pthread_cond_timedwait (&pcond->cond, &pmutex->lock, &end_time);
242   
243   if (result != ETIMEDOUT)
244     {
245       PTHREAD_CHECK ("pthread_cond_timedwait", result);
246     }
247
248   /* return true if we did not time out */
249   return result != ETIMEDOUT;
250 }
251
252 static void
253 _dbus_pthread_condvar_wake_one (DBusCondVar *cond)
254 {
255   DBusCondVarPThread *pcond = DBUS_COND_VAR_PTHREAD (cond);
256
257   PTHREAD_CHECK ("pthread_cond_signal", pthread_cond_signal (&pcond->cond));
258 }
259
260 static void
261 _dbus_pthread_condvar_wake_all (DBusCondVar *cond)
262 {
263   DBusCondVarPThread *pcond = DBUS_COND_VAR_PTHREAD (cond);
264   
265   PTHREAD_CHECK ("pthread_cond_broadcast", pthread_cond_broadcast (&pcond->cond));
266 }
267
268 static const DBusThreadFunctions pthread_functions =
269 {
270   DBUS_THREAD_FUNCTIONS_MUTEX_NEW_MASK |
271   DBUS_THREAD_FUNCTIONS_MUTEX_FREE_MASK |
272   DBUS_THREAD_FUNCTIONS_MUTEX_LOCK_MASK |
273   DBUS_THREAD_FUNCTIONS_MUTEX_UNLOCK_MASK |
274   DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_NEW_MASK |
275   DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_FREE_MASK |
276   DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_LOCK_MASK |
277   DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_UNLOCK_MASK |
278   DBUS_THREAD_FUNCTIONS_CONDVAR_NEW_MASK |
279   DBUS_THREAD_FUNCTIONS_CONDVAR_FREE_MASK |
280   DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_MASK |
281   DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_TIMEOUT_MASK |
282   DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ONE_MASK|
283   DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ALL_MASK,
284   _dbus_pthread_cmutex_new,
285   _dbus_pthread_mutex_free,
286   _dbus_pthread_mutex_lock,
287   _dbus_pthread_mutex_unlock,
288   _dbus_pthread_condvar_new,
289   _dbus_pthread_condvar_free,
290   _dbus_pthread_condvar_wait,
291   _dbus_pthread_condvar_wait_timeout,
292   _dbus_pthread_condvar_wake_one,
293   _dbus_pthread_condvar_wake_all,
294   _dbus_pthread_rmutex_new,
295   _dbus_pthread_mutex_free,
296   (void (*) (DBusMutex *)) _dbus_pthread_mutex_lock,
297   (void (*) (DBusMutex *)) _dbus_pthread_mutex_unlock
298 };
299
300 static void
301 check_monotonic_clock (void)
302 {
303 #ifdef HAVE_MONOTONIC_CLOCK
304   struct timespec dummy;
305   if (clock_getres (CLOCK_MONOTONIC, &dummy) == 0)
306     have_monotonic_clock = TRUE;
307 #endif
308 }
309
310 dbus_bool_t
311 _dbus_threads_init_platform_specific (void)
312 {
313   check_monotonic_clock ();
314   return dbus_threads_init (&pthread_functions);
315 }