1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-gthread.c GThread integration
4 * Copyright (C) 2002 CodeFactory AB
6 * Licensed under the Academic Free License version 1.2
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
25 #include <dbus/dbus.h>
26 #include "dbus-glib.h"
28 /** @addtogroup DBusGLibInternals
32 static DBusMutex * dbus_gmutex_new (void);
33 static void dbus_gmutex_free (DBusMutex *mutex);
34 static dbus_bool_t dbus_gmutex_lock (DBusMutex *mutex);
35 static dbus_bool_t dbus_gmutex_unlock (DBusMutex *mutex);
38 static DBusCondVar* dbus_gcondvar_new (void);
39 static void dbus_gcondvar_free (DBusCondVar *cond);
40 static void dbus_gcondvar_wait (DBusCondVar *cond,
42 static dbus_bool_t dbus_gcondvar_wait_timeout (DBusCondVar *cond,
45 static void dbus_gcondvar_wake_one (DBusCondVar *cond);
46 static void dbus_gcondvar_wake_all (DBusCondVar *cond);
49 static const DBusThreadFunctions functions =
51 DBUS_THREAD_FUNCTIONS_MUTEX_NEW_MASK |
52 DBUS_THREAD_FUNCTIONS_MUTEX_FREE_MASK |
53 DBUS_THREAD_FUNCTIONS_MUTEX_LOCK_MASK |
54 DBUS_THREAD_FUNCTIONS_MUTEX_UNLOCK_MASK |
55 DBUS_THREAD_FUNCTIONS_CONDVAR_NEW_MASK |
56 DBUS_THREAD_FUNCTIONS_CONDVAR_FREE_MASK |
57 DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_MASK |
58 DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_TIMEOUT_MASK |
59 DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ONE_MASK|
60 DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ALL_MASK,
68 dbus_gcondvar_wait_timeout,
69 dbus_gcondvar_wake_one,
70 dbus_gcondvar_wake_all
74 dbus_gmutex_new (void)
78 mutex = g_mutex_new ();
80 return (DBusMutex *)mutex;
84 dbus_gmutex_free (DBusMutex *mutex)
86 g_mutex_free ((GMutex *)mutex);
90 dbus_gmutex_lock (DBusMutex *mutex)
92 g_mutex_lock ((GMutex *)mutex);
98 dbus_gmutex_unlock (DBusMutex *mutex)
100 g_mutex_unlock ((GMutex *)mutex);
106 dbus_gcondvar_new (void)
108 return (DBusCondVar*)g_cond_new ();
112 dbus_gcondvar_free (DBusCondVar *cond)
114 g_cond_free ((GCond *)cond);
118 dbus_gcondvar_wait (DBusCondVar *cond,
121 g_cond_wait ((GCond *)cond, (GMutex *)mutex);
125 dbus_gcondvar_wait_timeout (DBusCondVar *cond,
131 g_get_current_time (&now);
133 now.tv_sec += timeout_msec / 1000;
134 now.tv_usec += (timeout_msec % 1000) * 1000;
135 if (now.tv_usec > G_USEC_PER_SEC)
138 now.tv_usec -= G_USEC_PER_SEC;
141 return g_cond_timed_wait ((GCond *)cond, (GMutex *)mutex, &now);
145 dbus_gcondvar_wake_one (DBusCondVar *cond)
147 g_cond_signal ((GCond *)cond);
151 dbus_gcondvar_wake_all (DBusCondVar *cond)
153 g_cond_broadcast ((GCond *)cond);
156 /** @} End of internals */
158 /** @addtogroup DBusGLib
162 * Initializes the D-BUS thread system to use
163 * GLib threads. This function may only be called
164 * once and must be called prior to calling any
165 * other function in the D-BUS API.
168 dbus_gthread_init (void)
170 if (!g_thread_supported ())
171 g_error ("g_thread_init() must be called before dbus_threads_init()");
173 dbus_threads_init (&functions);
176 /** @} end of public API */