1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-sysdeps-pthread.c Implements threads using Windows threads (internal to libdbus)
4 * Copyright (C) 2006 Red Hat, Inc.
6 * Licensed under the Academic Free License version 2.1
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.
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.
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
25 #include "dbus-internals.h"
26 #include "dbus-sysdeps.h"
27 #include "dbus-sysdeps-win.h"
28 #include "dbus-threads.h"
29 #include "dbus-list.h"
34 DBusList *list; /**< list thread-local-stored events waiting on the cond variable */
35 CRITICAL_SECTION lock; /**< lock protecting the list */
38 static DWORD dbus_cond_event_tls = TLS_OUT_OF_INDEXES;
41 static HMODULE dbus_dll_hmodule;
44 _dbus_win_get_dll_hmodule (void)
46 return dbus_dll_hmodule;
49 BOOL WINAPI DllMain (HINSTANCE hinstDLL,
53 /* We need this to free the TLS events on thread exit */
55 DllMain (HINSTANCE hinstDLL,
62 case DLL_PROCESS_ATTACH:
63 dbus_dll_hmodule = hinstDLL;
65 case DLL_THREAD_DETACH:
66 if (dbus_cond_event_tls != TLS_OUT_OF_INDEXES)
68 event = TlsGetValue(dbus_cond_event_tls);
70 TlsSetValue(dbus_cond_event_tls, NULL);
73 case DLL_PROCESS_DETACH:
74 if (dbus_cond_event_tls != TLS_OUT_OF_INDEXES)
76 event = TlsGetValue(dbus_cond_event_tls);
78 TlsSetValue(dbus_cond_event_tls, NULL);
80 TlsFree(dbus_cond_event_tls);
90 _dbus_windows_mutex_new (void)
93 handle = CreateMutex (NULL, FALSE, NULL);
94 return (DBusMutex *) handle;
98 _dbus_windows_mutex_free (DBusMutex *mutex)
100 CloseHandle ((HANDLE *) mutex);
104 _dbus_windows_mutex_lock (DBusMutex *mutex)
106 return WaitForSingleObject ((HANDLE *) mutex, INFINITE) != WAIT_FAILED;
110 _dbus_windows_mutex_unlock (DBusMutex *mutex)
112 return ReleaseMutex ((HANDLE *) mutex) != 0;
116 _dbus_windows_condvar_new (void)
120 cond = dbus_new (DBusCondVar, 1);
126 InitializeCriticalSection (&cond->lock);
127 return (DBusCondVar *) cond;
131 _dbus_windows_condvar_free (DBusCondVar *cond)
133 DeleteCriticalSection (&cond->lock);
134 _dbus_list_clear (&cond->list);
139 _dbus_condvar_wait_win32 (DBusCondVar *cond,
145 HANDLE event = TlsGetValue (dbus_cond_event_tls);
149 event = CreateEvent (0, FALSE, FALSE, NULL);
152 TlsSetValue (dbus_cond_event_tls, event);
155 EnterCriticalSection (&cond->lock);
157 /* The event must not be signaled. Check this */
158 _dbus_assert (WaitForSingleObject (event, 0) == WAIT_TIMEOUT);
160 ret = _dbus_list_append (&cond->list, event);
162 LeaveCriticalSection (&cond->lock);
165 return FALSE; /* Prepend failed */
167 _dbus_mutex_unlock (mutex);
168 retval = WaitForSingleObject (event, milliseconds);
169 _dbus_mutex_lock (mutex);
171 if (retval == WAIT_TIMEOUT)
173 EnterCriticalSection (&cond->lock);
174 _dbus_list_remove (&cond->list, event);
176 /* In the meantime we could have been signaled, so we must again
177 * wait for the signal, this time with no timeout, to reset
178 * it. retval is set again to honour the late arrival of the
180 retval = WaitForSingleObject (event, 0);
182 LeaveCriticalSection (&cond->lock);
185 #ifndef DBUS_DISABLE_ASSERT
186 EnterCriticalSection (&cond->lock);
188 /* Now event must not be inside the array, check this */
189 _dbus_assert (_dbus_list_remove (&cond->list, event) == FALSE);
191 LeaveCriticalSection (&cond->lock);
192 #endif /* !G_DISABLE_ASSERT */
194 return retval != WAIT_TIMEOUT;
198 _dbus_windows_condvar_wait (DBusCondVar *cond,
201 _dbus_condvar_wait_win32 (cond, mutex, INFINITE);
205 _dbus_windows_condvar_wait_timeout (DBusCondVar *cond,
207 int timeout_milliseconds)
209 return _dbus_condvar_wait_win32 (cond, mutex, timeout_milliseconds);
213 _dbus_windows_condvar_wake_one (DBusCondVar *cond)
215 EnterCriticalSection (&cond->lock);
217 if (cond->list != NULL)
218 SetEvent (_dbus_list_pop_first (&cond->list));
220 LeaveCriticalSection (&cond->lock);
224 _dbus_windows_condvar_wake_all (DBusCondVar *cond)
226 EnterCriticalSection (&cond->lock);
228 while (cond->list != NULL)
229 SetEvent (_dbus_list_pop_first (&cond->list));
231 LeaveCriticalSection (&cond->lock);
234 static const DBusThreadFunctions windows_functions =
236 DBUS_THREAD_FUNCTIONS_MUTEX_NEW_MASK |
237 DBUS_THREAD_FUNCTIONS_MUTEX_FREE_MASK |
238 DBUS_THREAD_FUNCTIONS_MUTEX_LOCK_MASK |
239 DBUS_THREAD_FUNCTIONS_MUTEX_UNLOCK_MASK |
240 DBUS_THREAD_FUNCTIONS_CONDVAR_NEW_MASK |
241 DBUS_THREAD_FUNCTIONS_CONDVAR_FREE_MASK |
242 DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_MASK |
243 DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_TIMEOUT_MASK |
244 DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ONE_MASK|
245 DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ALL_MASK,
246 _dbus_windows_mutex_new,
247 _dbus_windows_mutex_free,
248 _dbus_windows_mutex_lock,
249 _dbus_windows_mutex_unlock,
250 _dbus_windows_condvar_new,
251 _dbus_windows_condvar_free,
252 _dbus_windows_condvar_wait,
253 _dbus_windows_condvar_wait_timeout,
254 _dbus_windows_condvar_wake_one,
255 _dbus_windows_condvar_wake_all
259 _dbus_threads_init_platform_specific (void)
261 /* We reuse this over several generations, because we can't
262 * free the events once they are in use
264 if (dbus_cond_event_tls == TLS_OUT_OF_INDEXES)
266 dbus_cond_event_tls = TlsAlloc ();
267 if (dbus_cond_event_tls == TLS_OUT_OF_INDEXES)
271 return dbus_threads_init (&windows_functions);