Merge branch 'dbus-1.4'
[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 struct DBusRMutex {
47   pthread_mutex_t lock; /**< the lock */
48 };
49
50 struct DBusCMutex {
51   pthread_mutex_t lock; /**< the lock */
52 };
53
54 struct DBusCondVar {
55   pthread_cond_t cond; /**< the condition */
56 };
57
58 #define DBUS_MUTEX(m)         ((DBusMutex*) m)
59 #define DBUS_MUTEX_PTHREAD(m) ((DBusMutexPThread*) m)
60
61 #define DBUS_COND_VAR(c)         ((DBusCondVar*) c)
62 #define DBUS_COND_VAR_PTHREAD(c) ((DBusCondVarPThread*) c)
63
64
65 #ifdef DBUS_DISABLE_ASSERT
66 /* (tmp != 0) is a no-op usage to silence compiler */
67 #define PTHREAD_CHECK(func_name, result_or_call)    \
68     do { int tmp = (result_or_call); if (tmp != 0) {;} } while (0)
69 #else
70 #define PTHREAD_CHECK(func_name, result_or_call) do {                                  \
71     int tmp = (result_or_call);                                                        \
72     if (tmp != 0) {                                                                    \
73       _dbus_warn_check_failed ("pthread function %s failed with %d %s in %s\n",        \
74                                func_name, tmp, strerror(tmp), _DBUS_FUNCTION_NAME);    \
75     }                                                                                  \
76 } while (0)
77 #endif /* !DBUS_DISABLE_ASSERT */
78
79 DBusCMutex *
80 _dbus_platform_cmutex_new (void)
81 {
82   DBusCMutex *pmutex;
83   int result;
84
85   pmutex = dbus_new (DBusCMutex, 1);
86   if (pmutex == NULL)
87     return NULL;
88
89   result = pthread_mutex_init (&pmutex->lock, NULL);
90
91   if (result == ENOMEM || result == EAGAIN)
92     {
93       dbus_free (pmutex);
94       return NULL;
95     }
96   else
97     {
98       PTHREAD_CHECK ("pthread_mutex_init", result);
99     }
100
101   return pmutex;
102 }
103
104 DBusRMutex *
105 _dbus_platform_rmutex_new (void)
106 {
107   DBusRMutex *pmutex;
108   pthread_mutexattr_t mutexattr;
109   int result;
110
111   pmutex = dbus_new (DBusRMutex, 1);
112   if (pmutex == NULL)
113     return NULL;
114
115   pthread_mutexattr_init (&mutexattr);
116   pthread_mutexattr_settype (&mutexattr, PTHREAD_MUTEX_RECURSIVE);
117   result = pthread_mutex_init (&pmutex->lock, &mutexattr);
118   pthread_mutexattr_destroy (&mutexattr);
119
120   if (result == ENOMEM || result == EAGAIN)
121     {
122       dbus_free (pmutex);
123       return NULL;
124     }
125   else
126     {
127       PTHREAD_CHECK ("pthread_mutex_init", result);
128     }
129
130   return pmutex;
131 }
132
133 void
134 _dbus_platform_cmutex_free (DBusCMutex *mutex)
135 {
136   PTHREAD_CHECK ("pthread_mutex_destroy", pthread_mutex_destroy (&mutex->lock));
137   dbus_free (mutex);
138 }
139
140 void
141 _dbus_platform_rmutex_free (DBusRMutex *mutex)
142 {
143   PTHREAD_CHECK ("pthread_mutex_destroy", pthread_mutex_destroy (&mutex->lock));
144   dbus_free (mutex);
145 }
146
147 void
148 _dbus_platform_cmutex_lock (DBusCMutex *mutex)
149 {
150   PTHREAD_CHECK ("pthread_mutex_lock", pthread_mutex_lock (&mutex->lock));
151 }
152
153 void
154 _dbus_platform_rmutex_lock (DBusRMutex *mutex)
155 {
156   PTHREAD_CHECK ("pthread_mutex_lock", pthread_mutex_lock (&mutex->lock));
157 }
158
159 void
160 _dbus_platform_cmutex_unlock (DBusCMutex *mutex)
161 {
162   PTHREAD_CHECK ("pthread_mutex_unlock", pthread_mutex_unlock (&mutex->lock));
163 }
164
165 void
166 _dbus_platform_rmutex_unlock (DBusRMutex *mutex)
167 {
168   PTHREAD_CHECK ("pthread_mutex_unlock", pthread_mutex_unlock (&mutex->lock));
169 }
170
171 DBusCondVar *
172 _dbus_platform_condvar_new (void)
173 {
174   DBusCondVar *pcond;
175   pthread_condattr_t attr;
176   int result;
177   
178   pcond = dbus_new (DBusCondVar, 1);
179   if (pcond == NULL)
180     return NULL;
181
182   pthread_condattr_init (&attr);
183 #ifdef HAVE_MONOTONIC_CLOCK
184   if (have_monotonic_clock)
185     pthread_condattr_setclock (&attr, CLOCK_MONOTONIC);
186 #endif
187
188   result = pthread_cond_init (&pcond->cond, &attr);
189   pthread_condattr_destroy (&attr);
190
191   if (result == EAGAIN || result == ENOMEM)
192     {
193       dbus_free (pcond);
194       return NULL;
195     }
196   else
197     {
198       PTHREAD_CHECK ("pthread_cond_init", result);
199     }
200   
201   return pcond;
202 }
203
204 void
205 _dbus_platform_condvar_free (DBusCondVar *cond)
206 {
207   PTHREAD_CHECK ("pthread_cond_destroy", pthread_cond_destroy (&cond->cond));
208   dbus_free (cond);
209 }
210
211 void
212 _dbus_platform_condvar_wait (DBusCondVar *cond,
213                              DBusCMutex  *mutex)
214 {
215   PTHREAD_CHECK ("pthread_cond_wait", pthread_cond_wait (&cond->cond, &mutex->lock));
216 }
217
218 dbus_bool_t
219 _dbus_platform_condvar_wait_timeout (DBusCondVar               *cond,
220                                      DBusCMutex                *mutex,
221                                      int                        timeout_milliseconds)
222 {
223   struct timeval time_now;
224   struct timespec end_time;
225   int result;
226
227 #ifdef HAVE_MONOTONIC_CLOCK
228   if (have_monotonic_clock)
229     {
230       struct timespec monotonic_timer;
231       clock_gettime (CLOCK_MONOTONIC,&monotonic_timer);
232       time_now.tv_sec = monotonic_timer.tv_sec;
233       time_now.tv_usec = monotonic_timer.tv_nsec / 1000;
234     }
235   else
236     /* This else falls through to gettimeofday */
237 #endif
238   gettimeofday (&time_now, NULL);
239   
240   end_time.tv_sec = time_now.tv_sec + timeout_milliseconds / 1000;
241   end_time.tv_nsec = (time_now.tv_usec + (timeout_milliseconds % 1000) * 1000) * 1000;
242   if (end_time.tv_nsec > 1000*1000*1000)
243     {
244       end_time.tv_sec += 1;
245       end_time.tv_nsec -= 1000*1000*1000;
246     }
247
248   result = pthread_cond_timedwait (&cond->cond, &mutex->lock, &end_time);
249   
250   if (result != ETIMEDOUT)
251     {
252       PTHREAD_CHECK ("pthread_cond_timedwait", result);
253     }
254
255   /* return true if we did not time out */
256   return result != ETIMEDOUT;
257 }
258
259 void
260 _dbus_platform_condvar_wake_one (DBusCondVar *cond)
261 {
262   PTHREAD_CHECK ("pthread_cond_signal", pthread_cond_signal (&cond->cond));
263 }
264
265 static void
266 check_monotonic_clock (void)
267 {
268 #ifdef HAVE_MONOTONIC_CLOCK
269   struct timespec dummy;
270   if (clock_getres (CLOCK_MONOTONIC, &dummy) == 0)
271     have_monotonic_clock = TRUE;
272 #endif
273 }
274
275 dbus_bool_t
276 _dbus_threads_init_platform_specific (void)
277 {
278   check_monotonic_clock ();
279   return dbus_threads_init (NULL);
280 }