2006-10-27 Havoc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / dbus / dbus-sysdeps-pthread.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23
24 #include "dbus-internals.h"
25 #include "dbus-sysdeps.h"
26 #include "dbus-threads.h"
27
28 #include <sys/time.h>
29 #include <pthread.h>
30 #include <string.h>
31
32 typedef struct {
33   pthread_mutex_t lock; /**< lock protecting count field */
34   volatile int count;   /**< count of how many times lock holder has recursively locked */
35   volatile pthread_t holder; /**< holder of the lock if count >0,
36                                 valid but arbitrary thread if count
37                                 has ever been >0, uninitialized memory
38                                 if count has never been >0 */
39 } DBusMutexPThread;
40
41 typedef struct {
42   pthread_cond_t cond; /**< the condition */
43 } DBusCondVarPThread;
44
45 #define DBUS_MUTEX(m)         ((DBusMutex*) m)
46 #define DBUS_MUTEX_PTHREAD(m) ((DBusMutexPThread*) m)
47
48 #define DBUS_COND_VAR(c)         ((DBusCondVar*) c)
49 #define DBUS_COND_VAR_PTHREAD(c) ((DBusCondVarPThread*) c)
50
51
52 #ifdef DBUS_DISABLE_ASSERT
53 #define PTHREAD_CHECK(func_name, result_or_call) do {                                  \
54     do { (result_or_call) } while (0)
55 #else
56 #define PTHREAD_CHECK(func_name, result_or_call) do {                                  \
57     int tmp = (result_or_call);                                                        \
58     if (tmp != 0) {                                                                    \
59       _dbus_warn_check_failed ("pthread function %s failed with %d %s in %s\n",        \
60                                func_name, tmp, strerror(tmp), _DBUS_FUNCTION_NAME);    \
61     }                                                                                  \
62 } while (0)
63 #endif /* !DBUS_DISABLE_ASSERT */
64
65 static DBusMutex*
66 _dbus_pthread_mutex_new (void)
67 {
68   DBusMutexPThread *pmutex;
69   int result;
70   
71   pmutex = dbus_new (DBusMutexPThread, 1);
72   if (pmutex == NULL)
73     return NULL;
74
75   result = pthread_mutex_init (&pmutex->lock, NULL);
76
77   if (result == ENOMEM || result == EAGAIN)
78     {
79       dbus_free (pmutex);
80       return NULL;
81     }
82   else
83     {
84       PTHREAD_CHECK ("pthread_mutex_init", result);
85     }
86
87   /* Only written */
88   pmutex->count = 0;
89
90   /* There's no portable way to have a "null" pthread afaik so we
91    * can't set pmutex->holder to anything sensible.  We only access it
92    * once the lock is held (which means we've set it).
93    */
94   
95   return DBUS_MUTEX (pmutex);
96 }
97
98 static void
99 _dbus_pthread_mutex_free (DBusMutex *mutex)
100 {
101   DBusMutexPThread *pmutex = DBUS_MUTEX_PTHREAD (mutex);
102
103   _dbus_assert (pmutex->count == 0);
104   
105   PTHREAD_CHECK ("pthread_mutex_destroy", pthread_mutex_destroy (&pmutex->lock));
106
107   dbus_free (pmutex);
108 }
109
110 static void
111 _dbus_pthread_mutex_lock (DBusMutex *mutex)
112 {
113   DBusMutexPThread *pmutex = DBUS_MUTEX_PTHREAD (mutex);
114   pthread_t self = pthread_self ();
115
116   /* If the count is > 0 then someone had the lock, maybe us. If it is
117    * 0, then it might immediately change right after we read it,
118    * but it will be changed by another thread; i.e. if we read 0,
119    * we assume that this thread doesn't have the lock.
120    *
121    * Not 100% sure this is safe, but ... seems like it should be.
122    */
123   if (pmutex->count == 0)
124     {
125       /* We know we don't have the lock; someone may have the lock. */
126       
127       PTHREAD_CHECK ("pthread_mutex_lock", pthread_mutex_lock (&pmutex->lock));
128
129       /* We now have the lock. Count must be 0 since it must be 0 when
130        * the lock is released by another thread, and we just now got
131        * the lock.
132        */
133       _dbus_assert (pmutex->count == 0);
134       
135       pmutex->holder = self;
136       pmutex->count = 1;
137     }
138   else
139     {
140       /* We know someone had the lock, possibly us. Thus
141        * pmutex->holder is not pointing to junk, though it may not be
142        * the lock holder anymore if the lock holder is not us.  If the
143        * lock holder is us, then we definitely have the lock.
144        */
145
146       if (pthread_equal (pmutex->holder, self))
147         {
148           /* We already have the lock. */
149           _dbus_assert (pmutex->count > 0);
150         }
151       else
152         {
153           /* Wait for the lock */
154           PTHREAD_CHECK ("pthread_mutex_lock", pthread_mutex_lock (&pmutex->lock));
155           _dbus_assert (pmutex->count == 0);
156         }
157
158       pmutex->count += 1;
159     }
160 }
161
162 static void
163 _dbus_pthread_mutex_unlock (DBusMutex *mutex)
164 {
165   DBusMutexPThread *pmutex = DBUS_MUTEX_PTHREAD (mutex);
166
167   _dbus_assert (pmutex->count > 0);
168   
169   pmutex->count -= 1;
170
171   if (pmutex->count == 0)
172     PTHREAD_CHECK ("pthread_mutex_unlock", pthread_mutex_unlock (&pmutex->lock));
173   
174   /* We leave pmutex->holder set to ourselves, its content is undefined if count is 0 */
175 }
176
177 static DBusCondVar *
178 _dbus_pthread_condvar_new (void)
179 {
180   DBusCondVarPThread *pcond;
181   int result;
182   
183   pcond = dbus_new (DBusCondVarPThread, 1);
184   if (pcond == NULL)
185     return NULL;
186
187   result = pthread_cond_init (&pcond->cond, NULL);
188
189   if (result == EAGAIN || result == ENOMEM)
190     {
191       dbus_free (pcond);
192       return NULL;
193     }
194   else
195     {
196       PTHREAD_CHECK ("pthread_cond_init", result);
197     }
198   
199   return DBUS_COND_VAR (pcond);
200 }
201
202 static void
203 _dbus_pthread_condvar_free (DBusCondVar *cond)
204 {  
205   DBusCondVarPThread *pcond = DBUS_COND_VAR_PTHREAD (cond);
206   
207   PTHREAD_CHECK ("pthread_cond_destroy", pthread_cond_destroy (&pcond->cond));
208
209   dbus_free (pcond);
210 }
211
212 static void
213 _dbus_pthread_condvar_wait (DBusCondVar *cond,
214                             DBusMutex   *mutex)
215 {
216   DBusMutexPThread *pmutex = DBUS_MUTEX_PTHREAD (mutex);
217   DBusCondVarPThread *pcond = DBUS_COND_VAR_PTHREAD (cond);
218   int old_count;
219   
220   _dbus_assert (pmutex->count > 0);
221   _dbus_assert (pthread_equal (pmutex->holder, pthread_self ()));
222
223   old_count = pmutex->count;
224   pmutex->count = 0;
225   PTHREAD_CHECK ("pthread_cond_wait", pthread_cond_wait (&pcond->cond, &pmutex->lock));
226   _dbus_assert (pmutex->count == 0);
227   pmutex->count = old_count;
228 }
229
230 static dbus_bool_t
231 _dbus_pthread_condvar_wait_timeout (DBusCondVar               *cond,
232                                     DBusMutex                 *mutex,
233                                     int                        timeout_milliseconds)
234 {
235   DBusMutexPThread *pmutex = DBUS_MUTEX_PTHREAD (mutex);
236   DBusCondVarPThread *pcond = DBUS_COND_VAR_PTHREAD (cond);
237   struct timeval time_now;
238   struct timespec end_time;
239   int result;
240   int old_count;
241   
242   _dbus_assert (pmutex->count > 0);
243   _dbus_assert (pthread_equal (pmutex->holder, pthread_self ()));  
244   
245   gettimeofday (&time_now, NULL);
246   
247   end_time.tv_sec = time_now.tv_sec + timeout_milliseconds / 1000;
248   end_time.tv_nsec = (time_now.tv_usec + (timeout_milliseconds % 1000) * 1000) * 1000;
249   if (end_time.tv_nsec > 1000*1000*1000)
250     {
251       end_time.tv_sec += 1;
252       end_time.tv_nsec -= 1000*1000*1000;
253     }
254
255   old_count = pmutex->count;
256   pmutex->count = 0;
257   result = pthread_cond_timedwait (&pcond->cond, &pmutex->lock, &end_time);
258   
259   if (result != ETIMEDOUT)
260     {
261       PTHREAD_CHECK ("pthread_cond_timedwait", result);
262     }
263
264   _dbus_assert (pmutex->count == 0);
265   pmutex->count = old_count;
266   
267   /* return true if we did not time out */
268   return result != ETIMEDOUT;
269 }
270
271 static void
272 _dbus_pthread_condvar_wake_one (DBusCondVar *cond)
273 {
274   DBusCondVarPThread *pcond = DBUS_COND_VAR_PTHREAD (cond);
275
276   PTHREAD_CHECK ("pthread_cond_signal", pthread_cond_signal (&pcond->cond));
277 }
278
279 static void
280 _dbus_pthread_condvar_wake_all (DBusCondVar *cond)
281 {
282   DBusCondVarPThread *pcond = DBUS_COND_VAR_PTHREAD (cond);
283   
284   PTHREAD_CHECK ("pthread_cond_broadcast", pthread_cond_broadcast (&pcond->cond));
285 }
286
287 static const DBusThreadFunctions pthread_functions =
288 {
289   DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_NEW_MASK |
290   DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_FREE_MASK |
291   DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_LOCK_MASK |
292   DBUS_THREAD_FUNCTIONS_RECURSIVE_MUTEX_UNLOCK_MASK |
293   DBUS_THREAD_FUNCTIONS_CONDVAR_NEW_MASK |
294   DBUS_THREAD_FUNCTIONS_CONDVAR_FREE_MASK |
295   DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_MASK |
296   DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_TIMEOUT_MASK |
297   DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ONE_MASK|
298   DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ALL_MASK,
299   NULL, NULL, NULL, NULL,
300   _dbus_pthread_condvar_new,
301   _dbus_pthread_condvar_free,
302   _dbus_pthread_condvar_wait,
303   _dbus_pthread_condvar_wait_timeout,
304   _dbus_pthread_condvar_wake_one,
305   _dbus_pthread_condvar_wake_all,
306   _dbus_pthread_mutex_new,
307   _dbus_pthread_mutex_free,
308   _dbus_pthread_mutex_lock,
309   _dbus_pthread_mutex_unlock
310 };
311
312 dbus_bool_t
313 _dbus_threads_init_platform_specific (void)
314 {
315   return dbus_threads_init (&pthread_functions);
316 }