2006-12-13 Ralf Habacker <ralf.habacker@freenet.de>
[platform/upstream/dbus.git] / dbus / dbus-sysdeps-win-thread.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-sysdeps-pthread.c Implements threads using Windows threads (internal to libdbus)
3  * 
4  * Copyright (C) 2006  Red Hat, Inc.
5  *
6  * Licensed under the Academic Free License version 2.1
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 "dbus-internals.h"
25 #include "dbus-sysdeps.h"
26 #include "dbus-threads.h"
27 #include "dbus-list.h"
28
29 #include <windows.h>
30
31 struct DBusCondVar {
32   DBusList *list;        /**< list thread-local-stored events waiting on the cond variable */
33   CRITICAL_SECTION lock; /**< lock protecting the list */
34 };
35
36 static DWORD dbus_cond_event_tls = TLS_OUT_OF_INDEXES;
37
38
39 BOOL WINAPI DllMain (HINSTANCE hinstDLL,
40                      DWORD     fdwReason,
41                      LPVOID    lpvReserved);
42
43 /* We need this to free the TLS events on thread exit */
44 BOOL WINAPI
45 DllMain (HINSTANCE hinstDLL,
46          DWORD     fdwReason,
47          LPVOID    lpvReserved)
48 {
49   HANDLE event;
50   switch (fdwReason) 
51     { 
52     case DLL_THREAD_DETACH:
53       if (dbus_cond_event_tls != TLS_OUT_OF_INDEXES)
54         {
55           event = TlsGetValue(dbus_cond_event_tls);
56           CloseHandle (event);
57           TlsSetValue(dbus_cond_event_tls, NULL);
58         }
59       break;
60     case DLL_PROCESS_DETACH: 
61       if (dbus_cond_event_tls != TLS_OUT_OF_INDEXES)
62         {
63           event = TlsGetValue(dbus_cond_event_tls);
64           CloseHandle (event);
65           TlsSetValue(dbus_cond_event_tls, NULL);
66
67           TlsFree(dbus_cond_event_tls); 
68         }
69       break;
70     default: 
71       break; 
72     }
73   return TRUE;
74 }
75
76 static DBusMutex*
77 _dbus_windows_mutex_new (void)
78 {
79   HANDLE handle;
80   handle = CreateMutex (NULL, FALSE, NULL);
81   return (DBusMutex *) handle;
82 }
83
84 static void
85 _dbus_windows_mutex_free (DBusMutex *mutex)
86 {
87   CloseHandle ((HANDLE *) mutex);
88 }
89
90 static dbus_bool_t
91 _dbus_windows_mutex_lock (DBusMutex *mutex)
92 {
93   return WaitForSingleObject ((HANDLE *) mutex, INFINITE) != WAIT_FAILED;
94 }
95
96 static dbus_bool_t
97 _dbus_windows_mutex_unlock (DBusMutex *mutex)
98 {
99   return ReleaseMutex ((HANDLE *) mutex) != 0;
100 }
101
102 static DBusCondVar *
103 _dbus_windows_condvar_new (void)
104 {
105   DBusCondVar *cond;
106     
107   cond = dbus_new (DBusCondVar, 1);
108   if (cond == NULL)
109     return NULL;
110   
111   cond->list = NULL;
112   
113   InitializeCriticalSection (&cond->lock);
114   return (DBusCondVar *) cond;
115 }
116
117 static void
118 _dbus_windows_condvar_free (DBusCondVar *cond)
119 {
120   DeleteCriticalSection (&cond->lock);
121   _dbus_list_clear (&cond->list);
122   dbus_free (cond);
123 }
124
125 static dbus_bool_t
126 _dbus_condvar_wait_win32 (DBusCondVar *cond,
127                           DBusMutex *mutex,
128                           int milliseconds)
129 {
130   DWORD retval;
131   dbus_bool_t ret;
132   HANDLE event = TlsGetValue (dbus_cond_event_tls);
133
134   if (!event)
135     {
136       event = CreateEvent (0, FALSE, FALSE, NULL);
137       if (event == 0)
138         return FALSE;
139       TlsSetValue (dbus_cond_event_tls, event);
140     }
141
142   EnterCriticalSection (&cond->lock);
143
144   /* The event must not be signaled. Check this */
145   _dbus_assert (WaitForSingleObject (event, 0) == WAIT_TIMEOUT);
146
147   ret = _dbus_list_append (&cond->list, event);
148   
149   LeaveCriticalSection (&cond->lock);
150   
151   if (!ret)
152     return FALSE; /* Prepend failed */
153
154   _dbus_mutex_unlock (mutex);
155   retval = WaitForSingleObject (event, milliseconds);
156   _dbus_mutex_lock (mutex);
157   
158   if (retval == WAIT_TIMEOUT)
159     {
160       EnterCriticalSection (&cond->lock);
161       _dbus_list_remove (&cond->list, event);
162
163       /* In the meantime we could have been signaled, so we must again
164        * wait for the signal, this time with no timeout, to reset
165        * it. retval is set again to honour the late arrival of the
166        * signal */
167       retval = WaitForSingleObject (event, 0);
168
169       LeaveCriticalSection (&cond->lock);
170     }
171
172 #ifndef DBUS_DISABLE_ASSERT
173   EnterCriticalSection (&cond->lock);
174
175   /* Now event must not be inside the array, check this */
176   _dbus_assert (_dbus_list_remove (&cond->list, event) == FALSE);
177
178   LeaveCriticalSection (&cond->lock);
179 #endif /* !G_DISABLE_ASSERT */
180
181   return retval != WAIT_TIMEOUT;
182 }
183
184 static void
185 _dbus_windows_condvar_wait (DBusCondVar *cond,
186                             DBusMutex   *mutex)
187 {
188   _dbus_condvar_wait_win32 (cond, mutex, INFINITE);
189 }
190
191 static dbus_bool_t
192 _dbus_windows_condvar_wait_timeout (DBusCondVar               *cond,
193                                      DBusMutex                 *mutex,
194                                      int                        timeout_milliseconds)
195 {
196   return _dbus_condvar_wait_win32 (cond, mutex, timeout_milliseconds);
197 }
198
199 static void
200 _dbus_windows_condvar_wake_one (DBusCondVar *cond)
201 {
202   EnterCriticalSection (&cond->lock);
203   
204   if (cond->list != NULL)
205     SetEvent (_dbus_list_pop_first (&cond->list));
206     
207   LeaveCriticalSection (&cond->lock);
208 }
209
210 static void
211 _dbus_windows_condvar_wake_all (DBusCondVar *cond)
212 {
213   EnterCriticalSection (&cond->lock);
214
215   while (cond->list != NULL)
216     SetEvent (_dbus_list_pop_first (&cond->list));
217   
218   LeaveCriticalSection (&cond->lock);
219 }
220
221 static const DBusThreadFunctions windows_functions =
222 {
223   DBUS_THREAD_FUNCTIONS_MUTEX_NEW_MASK |
224   DBUS_THREAD_FUNCTIONS_MUTEX_FREE_MASK |
225   DBUS_THREAD_FUNCTIONS_MUTEX_LOCK_MASK |
226   DBUS_THREAD_FUNCTIONS_MUTEX_UNLOCK_MASK |
227   DBUS_THREAD_FUNCTIONS_CONDVAR_NEW_MASK |
228   DBUS_THREAD_FUNCTIONS_CONDVAR_FREE_MASK |
229   DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_MASK |
230   DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_TIMEOUT_MASK |
231   DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ONE_MASK|
232   DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ALL_MASK,
233   _dbus_windows_mutex_new,
234   _dbus_windows_mutex_free,
235   _dbus_windows_mutex_lock,
236   _dbus_windows_mutex_unlock,
237   _dbus_windows_condvar_new,
238   _dbus_windows_condvar_free,
239   _dbus_windows_condvar_wait,
240   _dbus_windows_condvar_wait_timeout,
241   _dbus_windows_condvar_wake_one,
242   _dbus_windows_condvar_wake_all
243 };
244
245 dbus_bool_t
246 _dbus_threads_init_platform_specific (void)
247 {
248   /* We reuse this over several generations, because we can't
249    * free the events once they are in use
250    */
251   if (dbus_cond_event_tls == TLS_OUT_OF_INDEXES)
252     {
253       dbus_cond_event_tls = TlsAlloc ();
254       if (dbus_cond_event_tls == TLS_OUT_OF_INDEXES)
255         return FALSE;
256     }
257
258   return dbus_threads_init (&windows_functions);
259 }
260