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