dbus-gthread.c

00001 /* -*- mode: C; c-file-style: "gnu" -*- */
00002 /* dbus-gthread.c  GThread integration
00003  *
00004  * Copyright (C) 2002  CodeFactory AB
00005  *
00006  * Licensed under the Academic Free License version 2.1
00007  * 
00008  * This program is free software; you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation; either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  * 
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021  *
00022  */
00023 
00024 /* #define G_DEBUG_LOCKS 1 */
00025 
00026 #include <glib.h>
00027 #include <dbus/dbus.h>
00028 #include <dbus/dbus-glib.h>
00029 #include <dbus/dbus-glib-lowlevel.h>
00030 
00035 static DBusMutex * dbus_gmutex_new        (void);
00036 static void        dbus_gmutex_free       (DBusMutex   *mutex);
00037 static dbus_bool_t dbus_gmutex_lock       (DBusMutex   *mutex);
00038 static dbus_bool_t dbus_gmutex_unlock     (DBusMutex   *mutex);
00039 
00040 
00041 static DBusCondVar* dbus_gcondvar_new          (void);
00042 static void         dbus_gcondvar_free         (DBusCondVar *cond);
00043 static void         dbus_gcondvar_wait         (DBusCondVar *cond,
00044                                                 DBusMutex   *mutex);
00045 static dbus_bool_t  dbus_gcondvar_wait_timeout (DBusCondVar *cond,
00046                                                 DBusMutex   *mutex,
00047                                                 int          timeout_msec);
00048 static void         dbus_gcondvar_wake_one     (DBusCondVar *cond);
00049 static void         dbus_gcondvar_wake_all     (DBusCondVar *cond);
00050 
00051 
00052 static const DBusThreadFunctions functions =
00053 {
00054   DBUS_THREAD_FUNCTIONS_MUTEX_NEW_MASK |
00055   DBUS_THREAD_FUNCTIONS_MUTEX_FREE_MASK |
00056   DBUS_THREAD_FUNCTIONS_MUTEX_LOCK_MASK |
00057   DBUS_THREAD_FUNCTIONS_MUTEX_UNLOCK_MASK |
00058   DBUS_THREAD_FUNCTIONS_CONDVAR_NEW_MASK |
00059   DBUS_THREAD_FUNCTIONS_CONDVAR_FREE_MASK |
00060   DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_MASK |
00061   DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_TIMEOUT_MASK |
00062   DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ONE_MASK|
00063   DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ALL_MASK,
00064   dbus_gmutex_new,
00065   dbus_gmutex_free,
00066   dbus_gmutex_lock,
00067   dbus_gmutex_unlock,
00068   dbus_gcondvar_new,
00069   dbus_gcondvar_free,
00070   dbus_gcondvar_wait,
00071   dbus_gcondvar_wait_timeout,
00072   dbus_gcondvar_wake_one,
00073   dbus_gcondvar_wake_all
00074 };
00075 
00076 static DBusMutex *
00077 dbus_gmutex_new (void)
00078 {
00079   GMutex *mutex;
00080 
00081   mutex = g_mutex_new ();
00082 
00083   return (DBusMutex *)mutex;
00084 }
00085 
00086 static void
00087 dbus_gmutex_free (DBusMutex *mutex)
00088 {
00089   g_mutex_free ((GMutex *)mutex);
00090 }
00091 
00092 static dbus_bool_t
00093 dbus_gmutex_lock (DBusMutex *mutex)
00094 {
00095   g_mutex_lock ((GMutex *)mutex);
00096 
00097   return TRUE;
00098 }
00099 
00100 static dbus_bool_t
00101 dbus_gmutex_unlock (DBusMutex *mutex)
00102 {
00103   g_mutex_unlock ((GMutex *)mutex);
00104 
00105   return TRUE;
00106 }
00107 
00108 static DBusCondVar*
00109 dbus_gcondvar_new (void)
00110 {
00111   return (DBusCondVar*)g_cond_new ();
00112 }
00113 
00114 static void
00115 dbus_gcondvar_free (DBusCondVar *cond)
00116 {
00117   g_cond_free ((GCond *)cond);
00118 }
00119 
00120 static void
00121 dbus_gcondvar_wait (DBusCondVar *cond,
00122                     DBusMutex   *mutex)
00123 {
00124   g_cond_wait ((GCond *)cond, (GMutex *)mutex);
00125 }
00126 
00127 static dbus_bool_t
00128 dbus_gcondvar_wait_timeout (DBusCondVar *cond,
00129                             DBusMutex   *mutex,
00130                             int         timeout_msec)
00131 {
00132   GTimeVal now;
00133   
00134   g_get_current_time (&now);
00135 
00136   now.tv_sec += timeout_msec / 1000;
00137   now.tv_usec += (timeout_msec % 1000) * 1000;
00138   if (now.tv_usec > G_USEC_PER_SEC)
00139     {
00140       now.tv_sec += 1;
00141       now.tv_usec -= G_USEC_PER_SEC;
00142     }
00143   
00144   return g_cond_timed_wait ((GCond *)cond, (GMutex *)mutex, &now);
00145 }
00146 
00147 static void
00148 dbus_gcondvar_wake_one (DBusCondVar *cond)
00149 {
00150   g_cond_signal ((GCond *)cond);
00151 }
00152 
00153 static void
00154 dbus_gcondvar_wake_all (DBusCondVar *cond)
00155 {
00156   g_cond_broadcast ((GCond *)cond);
00157 }
00158 
00170 void
00171 dbus_g_thread_init (void)
00172 {
00173   if (!g_thread_supported ())
00174     g_error ("g_thread_init() must be called before dbus_threads_init()");
00175     
00176   dbus_threads_init (&functions);
00177 }
00178 

Generated on Fri Feb 9 07:46:40 2007 for D-BUSGLibBindings by  doxygen 1.4.6