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"
32 _dbus_pthread_mutex_new (void)
34 pthread_mutex_t *retval;
36 retval = dbus_new (pthread_mutex_t, 1);
40 if (pthread_mutex_init (retval, NULL))
46 return (DBusMutex *) retval;
50 _dbus_pthread_mutex_free (DBusMutex *mutex)
52 pthread_mutex_destroy ((pthread_mutex_t *) mutex);
57 _dbus_pthread_mutex_lock (DBusMutex *mutex)
59 return pthread_mutex_lock ((pthread_mutex_t *) mutex) == 0;
63 _dbus_pthread_mutex_unlock (DBusMutex *mutex)
65 return pthread_mutex_unlock ((pthread_mutex_t *) mutex) == 0;
69 _dbus_pthread_condvar_new (void)
71 pthread_cond_t *retval;
73 retval = dbus_new (pthread_cond_t, 1);
77 if (pthread_cond_init (retval, NULL))
82 return (DBusCondVar *) retval;
86 _dbus_pthread_condvar_free (DBusCondVar *cond)
88 pthread_cond_destroy ((pthread_cond_t *) cond);
93 _dbus_pthread_condvar_wait (DBusCondVar *cond,
96 pthread_cond_wait ((pthread_cond_t *)cond,
97 (pthread_mutex_t *) mutex);
101 _dbus_pthread_condvar_wait_timeout (DBusCondVar *cond,
103 int timeout_milliseconds)
105 struct timeval time_now;
106 struct timespec end_time;
109 gettimeofday (&time_now, NULL);
111 end_time.tv_sec = time_now.tv_sec + timeout_milliseconds / 1000;
112 end_time.tv_nsec = (time_now.tv_usec + (timeout_milliseconds % 1000) * 1000) * 1000;
113 if (end_time.tv_nsec > 1000*1000*1000)
115 end_time.tv_sec += 1;
116 end_time.tv_nsec -= 1000*1000*1000;
119 result = pthread_cond_timedwait ((pthread_cond_t *) cond,
120 (pthread_mutex_t *) mutex,
123 /* return true if we did not time out */
124 return result != ETIMEDOUT;
128 _dbus_pthread_condvar_wake_one (DBusCondVar *cond)
130 pthread_cond_signal ((pthread_cond_t *)cond);
134 _dbus_pthread_condvar_wake_all (DBusCondVar *cond)
136 pthread_cond_broadcast ((pthread_cond_t *)cond);
139 static const DBusThreadFunctions pthread_functions =
141 DBUS_THREAD_FUNCTIONS_MUTEX_NEW_MASK |
142 DBUS_THREAD_FUNCTIONS_MUTEX_FREE_MASK |
143 DBUS_THREAD_FUNCTIONS_MUTEX_LOCK_MASK |
144 DBUS_THREAD_FUNCTIONS_MUTEX_UNLOCK_MASK |
145 DBUS_THREAD_FUNCTIONS_CONDVAR_NEW_MASK |
146 DBUS_THREAD_FUNCTIONS_CONDVAR_FREE_MASK |
147 DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_MASK |
148 DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_TIMEOUT_MASK |
149 DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ONE_MASK|
150 DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ALL_MASK,
151 _dbus_pthread_mutex_new,
152 _dbus_pthread_mutex_free,
153 _dbus_pthread_mutex_lock,
154 _dbus_pthread_mutex_unlock,
155 _dbus_pthread_condvar_new,
156 _dbus_pthread_condvar_free,
157 _dbus_pthread_condvar_wait,
158 _dbus_pthread_condvar_wait_timeout,
159 _dbus_pthread_condvar_wake_one,
160 _dbus_pthread_condvar_wake_all
164 _dbus_threads_init_platform_specific (void)
166 return dbus_threads_init (&pthread_functions);