8bc55a57c11aa212d032ed7a798e104c34075769
[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 2.0
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/dbus-glib.h>
27 #include <dbus/dbus-glib-lowlevel.h>
28
29 /** @addtogroup DBusGLibInternals
30  * @{
31  */
32
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);
37
38
39 static DBusCondVar* dbus_gcondvar_new          (void);
40 static void         dbus_gcondvar_free         (DBusCondVar *cond);
41 static void         dbus_gcondvar_wait         (DBusCondVar *cond,
42                                                 DBusMutex   *mutex);
43 static dbus_bool_t  dbus_gcondvar_wait_timeout (DBusCondVar *cond,
44                                                 DBusMutex   *mutex,
45                                                 int          timeout_msec);
46 static void         dbus_gcondvar_wake_one     (DBusCondVar *cond);
47 static void         dbus_gcondvar_wake_all     (DBusCondVar *cond);
48
49
50 static const DBusThreadFunctions functions =
51 {
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,
62   dbus_gmutex_new,
63   dbus_gmutex_free,
64   dbus_gmutex_lock,
65   dbus_gmutex_unlock,
66   dbus_gcondvar_new,
67   dbus_gcondvar_free,
68   dbus_gcondvar_wait,
69   dbus_gcondvar_wait_timeout,
70   dbus_gcondvar_wake_one,
71   dbus_gcondvar_wake_all
72 };
73
74 static DBusMutex *
75 dbus_gmutex_new (void)
76 {
77   GMutex *mutex;
78
79   mutex = g_mutex_new ();
80
81   return (DBusMutex *)mutex;
82 }
83
84 static void
85 dbus_gmutex_free (DBusMutex *mutex)
86 {
87   g_mutex_free ((GMutex *)mutex);
88 }
89
90 static dbus_bool_t
91 dbus_gmutex_lock (DBusMutex *mutex)
92 {
93   g_mutex_lock ((GMutex *)mutex);
94
95   return TRUE;
96 }
97
98 static dbus_bool_t
99 dbus_gmutex_unlock (DBusMutex *mutex)
100 {
101   g_mutex_unlock ((GMutex *)mutex);
102
103   return TRUE;
104 }
105
106 static DBusCondVar*
107 dbus_gcondvar_new (void)
108 {
109   return (DBusCondVar*)g_cond_new ();
110 }
111
112 static void
113 dbus_gcondvar_free (DBusCondVar *cond)
114 {
115   g_cond_free ((GCond *)cond);
116 }
117
118 static void
119 dbus_gcondvar_wait (DBusCondVar *cond,
120                     DBusMutex   *mutex)
121 {
122   g_cond_wait ((GCond *)cond, (GMutex *)mutex);
123 }
124
125 static dbus_bool_t
126 dbus_gcondvar_wait_timeout (DBusCondVar *cond,
127                             DBusMutex   *mutex,
128                             int         timeout_msec)
129 {
130   GTimeVal now;
131   
132   g_get_current_time (&now);
133
134   now.tv_sec += timeout_msec / 1000;
135   now.tv_usec += (timeout_msec % 1000) * 1000;
136   if (now.tv_usec > G_USEC_PER_SEC)
137     {
138       now.tv_sec += 1;
139       now.tv_usec -= G_USEC_PER_SEC;
140     }
141   
142   return g_cond_timed_wait ((GCond *)cond, (GMutex *)mutex, &now);
143 }
144
145 static void
146 dbus_gcondvar_wake_one (DBusCondVar *cond)
147 {
148   g_cond_signal ((GCond *)cond);
149 }
150
151 static void
152 dbus_gcondvar_wake_all (DBusCondVar *cond)
153 {
154   g_cond_broadcast ((GCond *)cond);
155 }
156
157 /** @} End of internals */
158
159 /** @addtogroup DBusGLib
160  * @{
161  */
162 /**
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.
167  */
168 void
169 dbus_g_thread_init (void)
170 {
171   if (!g_thread_supported ())
172     g_error ("g_thread_init() must be called before dbus_threads_init()");
173     
174   dbus_threads_init (&functions);
175 }
176
177 /** @} end of public API */