2003-08-16 Havoc Pennington <hp@pobox.com>
[platform/upstream/dbus.git] / glib / dbus-gthread.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-gthread.c  GThread integration
3  *
4  * Copyright (C) 2002  CodeFactory AB
5  *
6  * Licensed under the Academic Free License version 1.2
7  * 
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.
12  *
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.
17  * 
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
21  *
22  */
23
24 #include <glib.h>
25 #include <dbus/dbus.h>
26 #include "dbus-glib.h"
27
28 /** @addtogroup DBusGLibInternals
29  * @{
30  */
31
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);
36
37
38 static DBusCondVar* dbus_gcondvar_new          (void);
39 static void         dbus_gcondvar_free         (DBusCondVar *cond);
40 static void         dbus_gcondvar_wait         (DBusCondVar *cond,
41                                                 DBusMutex   *mutex);
42 static dbus_bool_t  dbus_gcondvar_wait_timeout (DBusCondVar *cond,
43                                                 DBusMutex   *mutex,
44                                                 int          timeout_msec);
45 static void         dbus_gcondvar_wake_one     (DBusCondVar *cond);
46 static void         dbus_gcondvar_wake_all     (DBusCondVar *cond);
47
48
49 static const DBusThreadFunctions functions =
50 {
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,
61   dbus_gmutex_new,
62   dbus_gmutex_free,
63   dbus_gmutex_lock,
64   dbus_gmutex_unlock,
65   dbus_gcondvar_new,
66   dbus_gcondvar_free,
67   dbus_gcondvar_wait,
68   dbus_gcondvar_wait_timeout,
69   dbus_gcondvar_wake_one,
70   dbus_gcondvar_wake_all
71 };
72
73 static DBusMutex *
74 dbus_gmutex_new (void)
75 {
76   GMutex *mutex;
77
78   mutex = g_mutex_new ();
79
80   return (DBusMutex *)mutex;
81 }
82
83 static void
84 dbus_gmutex_free (DBusMutex *mutex)
85 {
86   g_mutex_free ((GMutex *)mutex);
87 }
88
89 static dbus_bool_t
90 dbus_gmutex_lock (DBusMutex *mutex)
91 {
92   g_mutex_lock ((GMutex *)mutex);
93
94   return TRUE;
95 }
96
97 static dbus_bool_t
98 dbus_gmutex_unlock (DBusMutex *mutex)
99 {
100   g_mutex_unlock ((GMutex *)mutex);
101
102   return TRUE;
103 }
104
105 static DBusCondVar*
106 dbus_gcondvar_new (void)
107 {
108   return (DBusCondVar*)g_cond_new ();
109 }
110
111 static void
112 dbus_gcondvar_free (DBusCondVar *cond)
113 {
114   g_cond_free ((GCond *)cond);
115 }
116
117 static void
118 dbus_gcondvar_wait (DBusCondVar *cond,
119                     DBusMutex   *mutex)
120 {
121   g_cond_wait ((GCond *)cond, (GMutex *)mutex);
122 }
123
124 static dbus_bool_t
125 dbus_gcondvar_wait_timeout (DBusCondVar *cond,
126                             DBusMutex   *mutex,
127                             int         timeout_msec)
128 {
129   GTimeVal now;
130   
131   g_get_current_time (&now);
132
133   now.tv_sec += timeout_msec / 1000;
134   now.tv_usec += (timeout_msec % 1000) * 1000;
135   if (now.tv_usec > G_USEC_PER_SEC)
136     {
137       now.tv_sec += 1;
138       now.tv_usec -= G_USEC_PER_SEC;
139     }
140   
141   return g_cond_timed_wait ((GCond *)cond, (GMutex *)mutex, &now);
142 }
143
144 static void
145 dbus_gcondvar_wake_one (DBusCondVar *cond)
146 {
147   g_cond_signal ((GCond *)cond);
148 }
149
150 static void
151 dbus_gcondvar_wake_all (DBusCondVar *cond)
152 {
153   g_cond_broadcast ((GCond *)cond);
154 }
155
156 /** @} End of internals */
157
158 /** @addtogroup DBusGLib
159  * @{
160  */
161 /**
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.
166  */
167 void
168 dbus_gthread_init (void)
169 {
170   if (!g_thread_supported ())
171     g_error ("g_thread_init() must be called before dbus_threads_init()");
172     
173   dbus_threads_init (&functions);
174 }
175
176 /** @} end of public API */