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 2.0
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/dbus-glib.h>
27 #include <dbus/dbus-glib-lowlevel.h>
29 /** @addtogroup DBusGLibInternals
33 static DBusMutex * dbus_gmutex_new (void);
34 static void dbus_gmutex_free (DBusMutex *mutex);
35 static dbus_bool_t dbus_gmutex_lock (DBusMutex *mutex);
36 static dbus_bool_t dbus_gmutex_unlock (DBusMutex *mutex);
39 static DBusCondVar* dbus_gcondvar_new (void);
40 static void dbus_gcondvar_free (DBusCondVar *cond);
41 static void dbus_gcondvar_wait (DBusCondVar *cond,
43 static dbus_bool_t dbus_gcondvar_wait_timeout (DBusCondVar *cond,
46 static void dbus_gcondvar_wake_one (DBusCondVar *cond);
47 static void dbus_gcondvar_wake_all (DBusCondVar *cond);
50 static const DBusThreadFunctions functions =
52 DBUS_THREAD_FUNCTIONS_MUTEX_NEW_MASK |
53 DBUS_THREAD_FUNCTIONS_MUTEX_FREE_MASK |
54 DBUS_THREAD_FUNCTIONS_MUTEX_LOCK_MASK |
55 DBUS_THREAD_FUNCTIONS_MUTEX_UNLOCK_MASK |
56 DBUS_THREAD_FUNCTIONS_CONDVAR_NEW_MASK |
57 DBUS_THREAD_FUNCTIONS_CONDVAR_FREE_MASK |
58 DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_MASK |
59 DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_TIMEOUT_MASK |
60 DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ONE_MASK|
61 DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ALL_MASK,
69 dbus_gcondvar_wait_timeout,
70 dbus_gcondvar_wake_one,
71 dbus_gcondvar_wake_all
75 dbus_gmutex_new (void)
79 mutex = g_mutex_new ();
81 return (DBusMutex *)mutex;
85 dbus_gmutex_free (DBusMutex *mutex)
87 g_mutex_free ((GMutex *)mutex);
91 dbus_gmutex_lock (DBusMutex *mutex)
93 g_mutex_lock ((GMutex *)mutex);
99 dbus_gmutex_unlock (DBusMutex *mutex)
101 g_mutex_unlock ((GMutex *)mutex);
107 dbus_gcondvar_new (void)
109 return (DBusCondVar*)g_cond_new ();
113 dbus_gcondvar_free (DBusCondVar *cond)
115 g_cond_free ((GCond *)cond);
119 dbus_gcondvar_wait (DBusCondVar *cond,
122 g_cond_wait ((GCond *)cond, (GMutex *)mutex);
126 dbus_gcondvar_wait_timeout (DBusCondVar *cond,
132 g_get_current_time (&now);
134 now.tv_sec += timeout_msec / 1000;
135 now.tv_usec += (timeout_msec % 1000) * 1000;
136 if (now.tv_usec > G_USEC_PER_SEC)
139 now.tv_usec -= G_USEC_PER_SEC;
142 return g_cond_timed_wait ((GCond *)cond, (GMutex *)mutex, &now);
146 dbus_gcondvar_wake_one (DBusCondVar *cond)
148 g_cond_signal ((GCond *)cond);
152 dbus_gcondvar_wake_all (DBusCondVar *cond)
154 g_cond_broadcast ((GCond *)cond);
157 /** @} End of internals */
159 /** @addtogroup DBusGLib
163 * Initializes the D-BUS thread system to use
164 * GLib threads. This function may only be called
165 * once and must be called prior to calling any
166 * other function in the D-BUS API.
169 dbus_g_thread_init (void)
171 if (!g_thread_supported ())
172 g_error ("g_thread_init() must be called before dbus_threads_init()");
174 dbus_threads_init (&functions);
177 /** @} end of public API */