1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-sysdeps-pthread.c Implements threads using pthreads (internal to libdbus)
4 * Copyright (C) 2002, 2003, 2006 Red Hat, Inc.
6 * Licensed under the Academic Free License version 2.1
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.
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.
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
24 #include "dbus-internals.h"
25 #include "dbus-sysdeps.h"
26 #include "dbus-threads.h"
40 #define DBUS_MUTEX(m) ((DBusMutex*) m)
41 #define DBUS_MUTEX_PTHREAD(m) ((DBusMutexPThread*) m)
43 #define DBUS_COND_VAR(c) ((DBusCondVar*) c)
44 #define DBUS_COND_VAR_PTHREAD(c) ((DBusCondVarPThread*) c)
47 #define PTHREAD_CHECK(func_name, result_or_call) do { \
48 int tmp = (result_or_call); \
50 _dbus_warn_check_failed ("pthread function %s failed with %d %s in %s\n", \
51 func_name, tmp, strerror(tmp), _DBUS_FUNCTION_NAME); \
56 _dbus_pthread_mutex_new (void)
58 DBusMutexPThread *pmutex;
61 pmutex = dbus_new (DBusMutexPThread, 1);
65 result = pthread_mutex_init (&pmutex->lock, NULL);
67 if (result == ENOMEM || result == EAGAIN)
74 PTHREAD_CHECK ("pthread_mutex_init", result);
77 return DBUS_MUTEX (pmutex);
81 _dbus_pthread_mutex_free (DBusMutex *mutex)
83 DBusMutexPThread *pmutex = DBUS_MUTEX_PTHREAD (mutex);
85 PTHREAD_CHECK ("pthread_mutex_destroy", pthread_mutex_destroy (&pmutex->lock));
91 _dbus_pthread_mutex_lock (DBusMutex *mutex)
93 DBusMutexPThread *pmutex = DBUS_MUTEX_PTHREAD (mutex);
95 PTHREAD_CHECK ("pthread_mutex_lock", pthread_mutex_lock (&pmutex->lock));
101 _dbus_pthread_mutex_unlock (DBusMutex *mutex)
103 DBusMutexPThread *pmutex = DBUS_MUTEX_PTHREAD (mutex);
105 PTHREAD_CHECK ("pthread_mutex_unlock", pthread_mutex_unlock (&pmutex->lock));
111 _dbus_pthread_condvar_new (void)
113 DBusCondVarPThread *pcond;
116 pcond = dbus_new (DBusCondVarPThread, 1);
120 result = pthread_cond_init (&pcond->cond, NULL);
122 if (result == EAGAIN || result == ENOMEM)
129 PTHREAD_CHECK ("pthread_cond_init", result);
132 return DBUS_COND_VAR (pcond);
136 _dbus_pthread_condvar_free (DBusCondVar *cond)
138 DBusCondVarPThread *pcond = DBUS_COND_VAR_PTHREAD (cond);
140 PTHREAD_CHECK ("pthread_cond_destroy", pthread_cond_destroy (&pcond->cond));
146 _dbus_pthread_condvar_wait (DBusCondVar *cond,
149 DBusMutexPThread *pmutex = DBUS_MUTEX_PTHREAD (mutex);
150 DBusCondVarPThread *pcond = DBUS_COND_VAR_PTHREAD (cond);
152 PTHREAD_CHECK ("pthread_cond_wait", pthread_cond_wait (&pcond->cond, &pmutex->lock));
156 _dbus_pthread_condvar_wait_timeout (DBusCondVar *cond,
158 int timeout_milliseconds)
160 DBusMutexPThread *pmutex = DBUS_MUTEX_PTHREAD (mutex);
161 DBusCondVarPThread *pcond = DBUS_COND_VAR_PTHREAD (cond);
162 struct timeval time_now;
163 struct timespec end_time;
166 gettimeofday (&time_now, NULL);
168 end_time.tv_sec = time_now.tv_sec + timeout_milliseconds / 1000;
169 end_time.tv_nsec = (time_now.tv_usec + (timeout_milliseconds % 1000) * 1000) * 1000;
170 if (end_time.tv_nsec > 1000*1000*1000)
172 end_time.tv_sec += 1;
173 end_time.tv_nsec -= 1000*1000*1000;
176 result = pthread_cond_timedwait (&pcond->cond, &pmutex->lock, &end_time);
178 if (result != ETIMEDOUT)
180 PTHREAD_CHECK ("pthread_cond_timedwait", result);
183 /* return true if we did not time out */
184 return result != ETIMEDOUT;
188 _dbus_pthread_condvar_wake_one (DBusCondVar *cond)
190 DBusCondVarPThread *pcond = DBUS_COND_VAR_PTHREAD (cond);
192 PTHREAD_CHECK ("pthread_cond_signal", pthread_cond_signal (&pcond->cond));
196 _dbus_pthread_condvar_wake_all (DBusCondVar *cond)
198 DBusCondVarPThread *pcond = DBUS_COND_VAR_PTHREAD (cond);
200 PTHREAD_CHECK ("pthread_cond_broadcast", pthread_cond_broadcast (&pcond->cond));
203 static const DBusThreadFunctions pthread_functions =
205 DBUS_THREAD_FUNCTIONS_MUTEX_NEW_MASK |
206 DBUS_THREAD_FUNCTIONS_MUTEX_FREE_MASK |
207 DBUS_THREAD_FUNCTIONS_MUTEX_LOCK_MASK |
208 DBUS_THREAD_FUNCTIONS_MUTEX_UNLOCK_MASK |
209 DBUS_THREAD_FUNCTIONS_CONDVAR_NEW_MASK |
210 DBUS_THREAD_FUNCTIONS_CONDVAR_FREE_MASK |
211 DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_MASK |
212 DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_TIMEOUT_MASK |
213 DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ONE_MASK|
214 DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ALL_MASK,
215 _dbus_pthread_mutex_new,
216 _dbus_pthread_mutex_free,
217 _dbus_pthread_mutex_lock,
218 _dbus_pthread_mutex_unlock,
219 _dbus_pthread_condvar_new,
220 _dbus_pthread_condvar_free,
221 _dbus_pthread_condvar_wait,
222 _dbus_pthread_condvar_wait_timeout,
223 _dbus_pthread_condvar_wake_one,
224 _dbus_pthread_condvar_wake_all
228 _dbus_threads_init_platform_specific (void)
230 return dbus_threads_init (&pthread_functions);