00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <glib.h>
00025 #include <dbus/dbus.h>
00026 #include <dbus/dbus-glib.h>
00027 #include <dbus/dbus-glib-lowlevel.h>
00028
00033 static DBusMutex * dbus_gmutex_new (void);
00034 static void dbus_gmutex_free (DBusMutex *mutex);
00035 static dbus_bool_t dbus_gmutex_lock (DBusMutex *mutex);
00036 static dbus_bool_t dbus_gmutex_unlock (DBusMutex *mutex);
00037
00038
00039 static DBusCondVar* dbus_gcondvar_new (void);
00040 static void dbus_gcondvar_free (DBusCondVar *cond);
00041 static void dbus_gcondvar_wait (DBusCondVar *cond,
00042 DBusMutex *mutex);
00043 static dbus_bool_t dbus_gcondvar_wait_timeout (DBusCondVar *cond,
00044 DBusMutex *mutex,
00045 int timeout_msec);
00046 static void dbus_gcondvar_wake_one (DBusCondVar *cond);
00047 static void dbus_gcondvar_wake_all (DBusCondVar *cond);
00048
00049
00050 static const DBusThreadFunctions functions =
00051 {
00052 DBUS_THREAD_FUNCTIONS_MUTEX_NEW_MASK |
00053 DBUS_THREAD_FUNCTIONS_MUTEX_FREE_MASK |
00054 DBUS_THREAD_FUNCTIONS_MUTEX_LOCK_MASK |
00055 DBUS_THREAD_FUNCTIONS_MUTEX_UNLOCK_MASK |
00056 DBUS_THREAD_FUNCTIONS_CONDVAR_NEW_MASK |
00057 DBUS_THREAD_FUNCTIONS_CONDVAR_FREE_MASK |
00058 DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_MASK |
00059 DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_TIMEOUT_MASK |
00060 DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ONE_MASK|
00061 DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ALL_MASK,
00062 dbus_gmutex_new,
00063 dbus_gmutex_free,
00064 dbus_gmutex_lock,
00065 dbus_gmutex_unlock,
00066 dbus_gcondvar_new,
00067 dbus_gcondvar_free,
00068 dbus_gcondvar_wait,
00069 dbus_gcondvar_wait_timeout,
00070 dbus_gcondvar_wake_one,
00071 dbus_gcondvar_wake_all
00072 };
00073
00074 static DBusMutex *
00075 dbus_gmutex_new (void)
00076 {
00077 GMutex *mutex;
00078
00079 mutex = g_mutex_new ();
00080
00081 return (DBusMutex *)mutex;
00082 }
00083
00084 static void
00085 dbus_gmutex_free (DBusMutex *mutex)
00086 {
00087 g_mutex_free ((GMutex *)mutex);
00088 }
00089
00090 static dbus_bool_t
00091 dbus_gmutex_lock (DBusMutex *mutex)
00092 {
00093 g_mutex_lock ((GMutex *)mutex);
00094
00095 return TRUE;
00096 }
00097
00098 static dbus_bool_t
00099 dbus_gmutex_unlock (DBusMutex *mutex)
00100 {
00101 g_mutex_unlock ((GMutex *)mutex);
00102
00103 return TRUE;
00104 }
00105
00106 static DBusCondVar*
00107 dbus_gcondvar_new (void)
00108 {
00109 return (DBusCondVar*)g_cond_new ();
00110 }
00111
00112 static void
00113 dbus_gcondvar_free (DBusCondVar *cond)
00114 {
00115 g_cond_free ((GCond *)cond);
00116 }
00117
00118 static void
00119 dbus_gcondvar_wait (DBusCondVar *cond,
00120 DBusMutex *mutex)
00121 {
00122 g_cond_wait ((GCond *)cond, (GMutex *)mutex);
00123 }
00124
00125 static dbus_bool_t
00126 dbus_gcondvar_wait_timeout (DBusCondVar *cond,
00127 DBusMutex *mutex,
00128 int timeout_msec)
00129 {
00130 GTimeVal now;
00131
00132 g_get_current_time (&now);
00133
00134 now.tv_sec += timeout_msec / 1000;
00135 now.tv_usec += (timeout_msec % 1000) * 1000;
00136 if (now.tv_usec > G_USEC_PER_SEC)
00137 {
00138 now.tv_sec += 1;
00139 now.tv_usec -= G_USEC_PER_SEC;
00140 }
00141
00142 return g_cond_timed_wait ((GCond *)cond, (GMutex *)mutex, &now);
00143 }
00144
00145 static void
00146 dbus_gcondvar_wake_one (DBusCondVar *cond)
00147 {
00148 g_cond_signal ((GCond *)cond);
00149 }
00150
00151 static void
00152 dbus_gcondvar_wake_all (DBusCondVar *cond)
00153 {
00154 g_cond_broadcast ((GCond *)cond);
00155 }
00156
00168 void
00169 dbus_g_thread_init (void)
00170 {
00171 if (!g_thread_supported ())
00172 g_error ("g_thread_init() must be called before dbus_threads_init()");
00173
00174 dbus_threads_init (&functions);
00175 }
00176