bus: Assign a serial number for messages from the driver
[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 <stdio.h>
32 #include <string.h>
33
34 #ifdef HAVE_ERRNO_H
35 #include <errno.h>
36 #endif
37
38 #include <config.h>
39
40 #ifdef HAVE_MONOTONIC_CLOCK
41 /* Whether we have a "monotonic" clock; i.e. a clock not affected by
42  * changes in system time.
43  * This is initialized once in check_monotonic_clock below.
44  * https://bugs.freedesktop.org/show_bug.cgi?id=18121
45  */
46 static dbus_bool_t have_monotonic_clock = 0;
47 #endif
48
49 struct DBusRMutex {
50   pthread_mutex_t lock; /**< the lock */
51 };
52
53 struct DBusCMutex {
54   pthread_mutex_t lock; /**< the lock */
55 };
56
57 struct DBusCondVar {
58   pthread_cond_t cond; /**< the condition */
59 };
60
61 #define DBUS_MUTEX(m)         ((DBusMutex*) m)
62 #define DBUS_MUTEX_PTHREAD(m) ((DBusMutexPThread*) m)
63
64 #define DBUS_COND_VAR(c)         ((DBusCondVar*) c)
65 #define DBUS_COND_VAR_PTHREAD(c) ((DBusCondVarPThread*) c)
66
67
68 #ifdef DBUS_DISABLE_ASSERT
69 /* (tmp != 0) is a no-op usage to silence compiler */
70 #define PTHREAD_CHECK(func_name, result_or_call)    \
71     do { int tmp = (result_or_call); if (tmp != 0) {;} } while (0)
72 #else
73 #define PTHREAD_CHECK(func_name, result_or_call) do {                                  \
74     int tmp = (result_or_call);                                                        \
75     if (tmp != 0) {                                                                    \
76       _dbus_warn_check_failed ("pthread function %s failed with %d %s in %s",          \
77                                func_name, tmp, strerror(tmp), _DBUS_FUNCTION_NAME);    \
78     }                                                                                  \
79 } while (0)
80 #endif /* !DBUS_DISABLE_ASSERT */
81
82 DBusCMutex *
83 _dbus_platform_cmutex_new (void)
84 {
85   DBusCMutex *pmutex;
86   int result;
87
88   pmutex = dbus_new (DBusCMutex, 1);
89   if (pmutex == NULL)
90     return NULL;
91
92   result = pthread_mutex_init (&pmutex->lock, NULL);
93
94   if (result == ENOMEM || result == EAGAIN)
95     {
96       dbus_free (pmutex);
97       return NULL;
98     }
99   else
100     {
101       PTHREAD_CHECK ("pthread_mutex_init", result);
102     }
103
104   return pmutex;
105 }
106
107 DBusRMutex *
108 _dbus_platform_rmutex_new (void)
109 {
110   DBusRMutex *pmutex;
111   pthread_mutexattr_t mutexattr;
112   int result;
113
114   pmutex = dbus_new (DBusRMutex, 1);
115   if (pmutex == NULL)
116     return NULL;
117
118   pthread_mutexattr_init (&mutexattr);
119   pthread_mutexattr_settype (&mutexattr, PTHREAD_MUTEX_RECURSIVE);
120   result = pthread_mutex_init (&pmutex->lock, &mutexattr);
121   pthread_mutexattr_destroy (&mutexattr);
122
123   if (result == ENOMEM || result == EAGAIN)
124     {
125       dbus_free (pmutex);
126       return NULL;
127     }
128   else
129     {
130       PTHREAD_CHECK ("pthread_mutex_init", result);
131     }
132
133   return pmutex;
134 }
135
136 void
137 _dbus_platform_cmutex_free (DBusCMutex *mutex)
138 {
139   PTHREAD_CHECK ("pthread_mutex_destroy", pthread_mutex_destroy (&mutex->lock));
140   dbus_free (mutex);
141 }
142
143 void
144 _dbus_platform_rmutex_free (DBusRMutex *mutex)
145 {
146   PTHREAD_CHECK ("pthread_mutex_destroy", pthread_mutex_destroy (&mutex->lock));
147   dbus_free (mutex);
148 }
149
150 void
151 _dbus_platform_cmutex_lock (DBusCMutex *mutex)
152 {
153   PTHREAD_CHECK ("pthread_mutex_lock", pthread_mutex_lock (&mutex->lock));
154 }
155
156 void
157 _dbus_platform_rmutex_lock (DBusRMutex *mutex)
158 {
159   PTHREAD_CHECK ("pthread_mutex_lock", pthread_mutex_lock (&mutex->lock));
160 }
161
162 void
163 _dbus_platform_cmutex_unlock (DBusCMutex *mutex)
164 {
165   PTHREAD_CHECK ("pthread_mutex_unlock", pthread_mutex_unlock (&mutex->lock));
166 }
167
168 void
169 _dbus_platform_rmutex_unlock (DBusRMutex *mutex)
170 {
171   PTHREAD_CHECK ("pthread_mutex_unlock", pthread_mutex_unlock (&mutex->lock));
172 }
173
174 DBusCondVar *
175 _dbus_platform_condvar_new (void)
176 {
177   DBusCondVar *pcond;
178   pthread_condattr_t attr;
179   int result;
180   
181   pcond = dbus_new (DBusCondVar, 1);
182   if (pcond == NULL)
183     return NULL;
184
185   pthread_condattr_init (&attr);
186 #ifdef HAVE_MONOTONIC_CLOCK
187   if (have_monotonic_clock)
188     pthread_condattr_setclock (&attr, CLOCK_MONOTONIC);
189 #endif
190
191   result = pthread_cond_init (&pcond->cond, &attr);
192   pthread_condattr_destroy (&attr);
193
194   if (result == EAGAIN || result == ENOMEM)
195     {
196       dbus_free (pcond);
197       return NULL;
198     }
199   else
200     {
201       PTHREAD_CHECK ("pthread_cond_init", result);
202     }
203   
204   return pcond;
205 }
206
207 void
208 _dbus_platform_condvar_free (DBusCondVar *cond)
209 {
210   PTHREAD_CHECK ("pthread_cond_destroy", pthread_cond_destroy (&cond->cond));
211   dbus_free (cond);
212 }
213
214 void
215 _dbus_platform_condvar_wait (DBusCondVar *cond,
216                              DBusCMutex  *mutex)
217 {
218   PTHREAD_CHECK ("pthread_cond_wait", pthread_cond_wait (&cond->cond, &mutex->lock));
219 }
220
221 dbus_bool_t
222 _dbus_platform_condvar_wait_timeout (DBusCondVar               *cond,
223                                      DBusCMutex                *mutex,
224                                      int                        timeout_milliseconds)
225 {
226   struct timeval time_now;
227   struct timespec end_time;
228   int result;
229
230 #ifdef HAVE_MONOTONIC_CLOCK
231   if (have_monotonic_clock)
232     {
233       struct timespec monotonic_timer;
234       clock_gettime (CLOCK_MONOTONIC,&monotonic_timer);
235       time_now.tv_sec = monotonic_timer.tv_sec;
236       time_now.tv_usec = monotonic_timer.tv_nsec / 1000;
237     }
238   else
239     /* This else falls through to gettimeofday */
240 #endif
241   gettimeofday (&time_now, NULL);
242   
243   end_time.tv_sec = time_now.tv_sec + timeout_milliseconds / 1000;
244   end_time.tv_nsec = (time_now.tv_usec + (timeout_milliseconds % 1000) * 1000) * 1000;
245   if (end_time.tv_nsec > 1000*1000*1000)
246     {
247       end_time.tv_sec += 1;
248       end_time.tv_nsec -= 1000*1000*1000;
249     }
250
251   result = pthread_cond_timedwait (&cond->cond, &mutex->lock, &end_time);
252   
253   if (result != ETIMEDOUT)
254     {
255       PTHREAD_CHECK ("pthread_cond_timedwait", result);
256     }
257
258   /* return true if we did not time out */
259   return result != ETIMEDOUT;
260 }
261
262 void
263 _dbus_platform_condvar_wake_one (DBusCondVar *cond)
264 {
265   PTHREAD_CHECK ("pthread_cond_signal", pthread_cond_signal (&cond->cond));
266 }
267
268 static void
269 check_monotonic_clock (void)
270 {
271 #ifdef HAVE_MONOTONIC_CLOCK
272   struct timespec dummy;
273   if (clock_getres (CLOCK_MONOTONIC, &dummy) == 0)
274     have_monotonic_clock = TRUE;
275 #endif
276 }
277
278 dbus_bool_t
279 _dbus_threads_init_platform_specific (void)
280 {
281   /* These have static variables, and we need to handle both the case
282    * where dbus_threads_init() has been called and when it hasn't;
283    * so initialize them before any threads are allowed to enter.
284    */
285   check_monotonic_clock ();
286   (void) _dbus_check_setuid ();
287
288   return TRUE;
289 }
290
291 static pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER;
292
293 void
294 _dbus_threads_lock_platform_specific (void)
295 {
296   pthread_mutex_lock (&init_mutex);
297 }
298
299 void
300 _dbus_threads_unlock_platform_specific (void)
301 {
302   pthread_mutex_unlock (&init_mutex);
303 }
304
305 #ifdef DBUS_ENABLE_VERBOSE_MODE
306 /*
307  * If we can identify the current process and/or thread, print them to stderr followed by a colon.
308  */
309 void
310 _dbus_print_thread (void)
311 {
312 #ifdef __linux__
313   /* we know a pthread_t is numeric on Linux */
314   fprintf (stderr, "%lu: 0x%lx: ", _dbus_pid_for_log (), (unsigned long) pthread_self ());
315 #else
316   /* in principle pthread_t isn't required to be printable */
317   fprintf (stderr, "%lu: ", _dbus_pid_for_log ());
318 #endif
319 }
320 #endif