D-Bus: dbus-sysdeps-pthread.c Source File

D-Bus 1.10.22

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 #ifdef HAVE_MONOTONIC_CLOCK
40 /* Whether we have a "monotonic" clock; i.e. a clock not affected by
41 * changes in system time.
42 * This is initialized once in check_monotonic_clock below.
43 * https://bugs.freedesktop.org/show_bug.cgi?id=18121
44 */
45 static dbus_bool_t have_monotonic_clock = 0;
46 #endif
47
48 struct DBusRMutex {
49 pthread_mutex_t lock;
50 };
51
52 struct DBusCMutex {
53 pthread_mutex_t lock;
54 };
55
56 struct DBusCondVar {
57 pthread_cond_t cond;
58 };
59
60 #define DBUS_MUTEX(m) ((DBusMutex*) m)
61 #define DBUS_MUTEX_PTHREAD(m) ((DBusMutexPThread*) m)
62
63 #define DBUS_COND_VAR(c) ((DBusCondVar*) c)
64 #define DBUS_COND_VAR_PTHREAD(c) ((DBusCondVarPThread*) c)
65
66
67 #ifdef DBUS_DISABLE_ASSERT
68 /* (tmp != 0) is a no-op usage to silence compiler */
69 #define PTHREAD_CHECK(func_name, result_or_call) \
70 do { int tmp = (result_or_call); if (tmp != 0) {;} } while (0)
71 #else
72 #define PTHREAD_CHECK(func_name, result_or_call) do { \
73 int tmp = (result_or_call); \
74 if (tmp != 0) { \
75 _dbus_warn_check_failed ("pthread function %s failed with %d %s in %s\n", \
76 func_name, tmp, strerror(tmp), _DBUS_FUNCTION_NAME); \
77 } \
78 } while (0)
79 #endif /* !DBUS_DISABLE_ASSERT */
80
81 DBusCMutex *
82 _dbus_platform_cmutex_new (void)
83 {
84 DBusCMutex *pmutex;
85 int result;
86
87 pmutex = dbus_new (DBusCMutex, 1);
88 if (pmutex == NULL)
89 return NULL;
90
91 result = pthread_mutex_init (&pmutex->lock, NULL);
92
93 if (result == ENOMEM || result == EAGAIN)
94 {
95 dbus_free (pmutex);
96 return NULL;
97 }
98 else
99 {
100 PTHREAD_CHECK ("pthread_mutex_init", result);
101 }
102
103 return pmutex;
104 }
105
106 DBusRMutex *
107 _dbus_platform_rmutex_new (void)
108 {
109 DBusRMutex *pmutex;
110 pthread_mutexattr_t mutexattr;
111 int result;
112
113 pmutex = dbus_new (DBusRMutex, 1);
114 if (pmutex == NULL)
115 return NULL;
116
117 pthread_mutexattr_init (&mutexattr);
118 pthread_mutexattr_settype (&mutexattr, PTHREAD_MUTEX_RECURSIVE);
119 result = pthread_mutex_init (&pmutex->lock, &mutexattr);
120 pthread_mutexattr_destroy (&mutexattr);
121
122 if (result == ENOMEM || result == EAGAIN)
123 {
124 dbus_free (pmutex);
125 return NULL;
126 }
127 else
128 {
129 PTHREAD_CHECK ("pthread_mutex_init", result);
130 }
131
132 return pmutex;
133 }
134
135 void
136 _dbus_platform_cmutex_free (DBusCMutex *mutex)
137 {
138 PTHREAD_CHECK ("pthread_mutex_destroy", pthread_mutex_destroy (&mutex->lock));
139 dbus_free (mutex);
140 }
141
142 void
143 _dbus_platform_rmutex_free (DBusRMutex *mutex)
144 {
145 PTHREAD_CHECK ("pthread_mutex_destroy", pthread_mutex_destroy (&mutex->lock));
146 dbus_free (mutex);
147 }
148
149 void
150 _dbus_platform_cmutex_lock (DBusCMutex *mutex)
151 {
152 PTHREAD_CHECK ("pthread_mutex_lock", pthread_mutex_lock (&mutex->lock));
153 }
154
155 void
156 _dbus_platform_rmutex_lock (DBusRMutex *mutex)
157 {
158 PTHREAD_CHECK ("pthread_mutex_lock", pthread_mutex_lock (&mutex->lock));
159 }
160
161 void
162 _dbus_platform_cmutex_unlock (DBusCMutex *mutex)
163 {
164 PTHREAD_CHECK ("pthread_mutex_unlock", pthread_mutex_unlock (&mutex->lock));
165 }
166
167 void
168 _dbus_platform_rmutex_unlock (DBusRMutex *mutex)
169 {
170 PTHREAD_CHECK ("pthread_mutex_unlock", pthread_mutex_unlock (&mutex->lock));
171 }
172
173 DBusCondVar *
174 _dbus_platform_condvar_new (void)
175 {
176 DBusCondVar *pcond;
177 pthread_condattr_t attr;
178 int result;
179
180 pcond = dbus_new (DBusCondVar, 1);
181 if (pcond == NULL)
182 return NULL;
183
184 pthread_condattr_init (&attr);
185 #ifdef HAVE_MONOTONIC_CLOCK
186 if (have_monotonic_clock)
187 pthread_condattr_setclock (&attr, CLOCK_MONOTONIC);
188 #endif
189
190 result = pthread_cond_init (&pcond->cond, &attr);
191 pthread_condattr_destroy (&attr);
192
193 if (result == EAGAIN || result == ENOMEM)
194 {
195 dbus_free (pcond);
196 return NULL;
197 }
198 else
199 {
200 PTHREAD_CHECK ("pthread_cond_init", result);
201 }
202
203 return pcond;
204 }
205
206 void
207 _dbus_platform_condvar_free (DBusCondVar *cond)
208 {
209 PTHREAD_CHECK ("pthread_cond_destroy", pthread_cond_destroy (&cond->cond));
210 dbus_free (cond);
211 }
212
213 void
214 _dbus_platform_condvar_wait (DBusCondVar *cond,
215 DBusCMutex *mutex)
216 {
217 PTHREAD_CHECK ("pthread_cond_wait", pthread_cond_wait (&cond->cond, &mutex->lock));
218 }
219
220 dbus_bool_t
221 _dbus_platform_condvar_wait_timeout (DBusCondVar *cond,
222 DBusCMutex *mutex,
223 int timeout_milliseconds)
224 {
225 struct timeval time_now;
226 struct timespec end_time;
227 int result;
228
229 #ifdef HAVE_MONOTONIC_CLOCK
230 if (have_monotonic_clock)
231 {
232 struct timespec monotonic_timer;
233 clock_gettime (CLOCK_MONOTONIC,&monotonic_timer);
234 time_now.tv_sec = monotonic_timer.tv_sec;
235 time_now.tv_usec = monotonic_timer.tv_nsec / 1000;
236 }
237 else
238 /* This else falls through to gettimeofday */
239 #endif
240 gettimeofday (&time_now, NULL);
241
242 end_time.tv_sec = time_now.tv_sec + timeout_milliseconds / 1000;
243 end_time.tv_nsec = (time_now.tv_usec + (timeout_milliseconds % 1000) * 1000) * 1000;
244 if (end_time.tv_nsec > 1000*1000*1000)
245 {
246 end_time.tv_sec += 1;
247 end_time.tv_nsec -= 1000*1000*1000;
248 }
249
250 result = pthread_cond_timedwait (&cond->cond, &mutex->lock, &end_time);
251
252 if (result != ETIMEDOUT)
253 {
254 PTHREAD_CHECK ("pthread_cond_timedwait", result);
255 }
256
257 /* return true if we did not time out */
258 return result != ETIMEDOUT;
259 }
260
261 void
262 _dbus_platform_condvar_wake_one (DBusCondVar *cond)
263 {
264 PTHREAD_CHECK ("pthread_cond_signal", pthread_cond_signal (&cond->cond));
265 }
266
267 static void
268 check_monotonic_clock (void)
269 {
270 #ifdef HAVE_MONOTONIC_CLOCK
271 struct timespec dummy;
272 if (clock_getres (CLOCK_MONOTONIC, &dummy) == 0)
273 have_monotonic_clock = TRUE;
274 #endif
275 }
276
277 dbus_bool_t
278 _dbus_threads_init_platform_specific (void)
279 {
280 /* These have static variables, and we need to handle both the case
281 * where dbus_threads_init() has been called and when it hasn't;
282 * so initialize them before any threads are allowed to enter.
283 */
284 check_monotonic_clock ();
285 (void) _dbus_check_setuid ();
286
287 return TRUE;
288 }
289
290 static pthread_mutex_t init_mutex = PTHREAD_MUTEX_INITIALIZER;
291
292 void
293 _dbus_threads_lock_platform_specific (void)
294 {
295 pthread_mutex_lock (&init_mutex);
296 }
297
298 void
299 _dbus_threads_unlock_platform_specific (void)
300 {
301 pthread_mutex_unlock (&init_mutex);
302 }
NULL
#define NULL
A null pointer, defined appropriately for C or C++.
dbus_free
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:701
dbus_new
#define dbus_new(type, count)
Safe macro for using dbus_malloc().
Definition: dbus-memory.h:58
_dbus_check_setuid
dbus_bool_t _dbus_check_setuid(void)
NOTE: If you modify this function, please also consider making the corresponding change in GLib...
Definition: dbus-sysdeps-unix.c:4407
_dbus_threads_init_platform_specific
dbus_bool_t _dbus_threads_init_platform_specific(void)
Initialize threads as in dbus_threads_init_default(), appropriately for the platform.
Definition: dbus-sysdeps-pthread.c:278
DBusRMutex::lock
pthread_mutex_t lock
the lock
Definition: dbus-sysdeps-pthread.c:49
DBusCMutex::lock
pthread_mutex_t lock
the lock
Definition: dbus-sysdeps-pthread.c:53
DBusCMutex
Definition: dbus-sysdeps-pthread.c:52
_dbus_threads_unlock_platform_specific
void _dbus_threads_unlock_platform_specific(void)
Undo _dbus_threads_lock_platform_specific().
Definition: dbus-sysdeps-pthread.c:299
_dbus_threads_lock_platform_specific
void _dbus_threads_lock_platform_specific(void)
Lock a static mutex used to protect _dbus_threads_init_platform_specific().
Definition: dbus-sysdeps-pthread.c:293
dbus_bool_t
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
TRUE
#define TRUE
Expands to "1".
DBusCondVar
Definition: dbus-sysdeps-pthread.c:56
DBusCondVar::cond
pthread_cond_t cond
the condition
Definition: dbus-sysdeps-pthread.c:57
DBusRMutex
Definition: dbus-sysdeps-pthread.c:48