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