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"
35 static dbus_bool_t global_init_done = FALSE;
36 static CRITICAL_SECTION init_lock;
38 /* Called from C++ code in dbus-init-win.cpp. */
40 _dbus_threads_windows_init_global (void)
42 /* this ensures that the object that acts as our global constructor
43 * actually gets linked in when we're linked statically */
44 _dbus_threads_windows_ensure_ctor_linked ();
46 InitializeCriticalSection (&init_lock);
47 global_init_done = TRUE;
51 DBusList *list; /**< list thread-local-stored events waiting on the cond variable */
52 CRITICAL_SECTION lock; /**< lock protecting the list */
55 static DWORD dbus_cond_event_tls = TLS_OUT_OF_INDEXES;
58 static HMODULE dbus_dll_hmodule;
61 _dbus_win_get_dll_hmodule (void)
63 return dbus_dll_hmodule;
67 #define hinst_t HANDLE
69 #define hinst_t HINSTANCE
72 BOOL WINAPI DllMain (hinst_t, DWORD, LPVOID);
74 /* We need this to free the TLS events on thread exit */
76 DllMain (hinst_t hinstDLL,
83 case DLL_PROCESS_ATTACH:
84 dbus_dll_hmodule = hinstDLL;
86 case DLL_THREAD_DETACH:
87 if (dbus_cond_event_tls != TLS_OUT_OF_INDEXES)
89 event = TlsGetValue(dbus_cond_event_tls);
91 TlsSetValue(dbus_cond_event_tls, NULL);
94 case DLL_PROCESS_DETACH:
95 if (dbus_cond_event_tls != TLS_OUT_OF_INDEXES)
97 event = TlsGetValue(dbus_cond_event_tls);
99 TlsSetValue(dbus_cond_event_tls, NULL);
101 TlsFree(dbus_cond_event_tls);
111 _dbus_platform_cmutex_new (void)
114 handle = CreateMutex (NULL, FALSE, NULL);
115 return (DBusCMutex *) handle;
119 _dbus_platform_rmutex_new (void)
122 handle = CreateMutex (NULL, FALSE, NULL);
123 return (DBusRMutex *) handle;
127 _dbus_platform_cmutex_free (DBusCMutex *mutex)
129 CloseHandle ((HANDLE *) mutex);
133 _dbus_platform_rmutex_free (DBusRMutex *mutex)
135 CloseHandle ((HANDLE *) mutex);
139 _dbus_platform_cmutex_lock (DBusCMutex *mutex)
141 WaitForSingleObject ((HANDLE *) mutex, INFINITE);
145 _dbus_platform_rmutex_lock (DBusRMutex *mutex)
147 WaitForSingleObject ((HANDLE *) mutex, INFINITE);
151 _dbus_platform_cmutex_unlock (DBusCMutex *mutex)
153 ReleaseMutex ((HANDLE *) mutex);
157 _dbus_platform_rmutex_unlock (DBusRMutex *mutex)
159 ReleaseMutex ((HANDLE *) mutex);
163 _dbus_platform_condvar_new (void)
167 cond = dbus_new (DBusCondVar, 1);
173 InitializeCriticalSection (&cond->lock);
178 _dbus_platform_condvar_free (DBusCondVar *cond)
180 DeleteCriticalSection (&cond->lock);
181 _dbus_list_clear (&cond->list);
186 _dbus_condvar_wait_win32 (DBusCondVar *cond,
192 HANDLE event = TlsGetValue (dbus_cond_event_tls);
196 event = CreateEvent (0, FALSE, FALSE, NULL);
199 TlsSetValue (dbus_cond_event_tls, event);
202 EnterCriticalSection (&cond->lock);
204 /* The event must not be signaled. Check this */
205 _dbus_assert (WaitForSingleObject (event, 0) == WAIT_TIMEOUT);
207 ret = _dbus_list_append (&cond->list, event);
209 LeaveCriticalSection (&cond->lock);
212 return FALSE; /* Prepend failed */
214 _dbus_platform_cmutex_unlock (mutex);
215 retval = WaitForSingleObject (event, milliseconds);
216 _dbus_platform_cmutex_lock (mutex);
218 if (retval == WAIT_TIMEOUT)
220 EnterCriticalSection (&cond->lock);
221 _dbus_list_remove (&cond->list, event);
223 /* In the meantime we could have been signaled, so we must again
224 * wait for the signal, this time with no timeout, to reset
225 * it. retval is set again to honour the late arrival of the
227 retval = WaitForSingleObject (event, 0);
229 LeaveCriticalSection (&cond->lock);
232 #ifndef DBUS_DISABLE_ASSERT
233 EnterCriticalSection (&cond->lock);
235 /* Now event must not be inside the array, check this */
236 _dbus_assert (_dbus_list_remove (&cond->list, event) == FALSE);
238 LeaveCriticalSection (&cond->lock);
239 #endif /* !G_DISABLE_ASSERT */
241 return retval != WAIT_TIMEOUT;
245 _dbus_platform_condvar_wait (DBusCondVar *cond,
248 _dbus_condvar_wait_win32 (cond, mutex, INFINITE);
252 _dbus_platform_condvar_wait_timeout (DBusCondVar *cond,
254 int timeout_milliseconds)
256 return _dbus_condvar_wait_win32 (cond, mutex, timeout_milliseconds);
260 _dbus_platform_condvar_wake_one (DBusCondVar *cond)
262 EnterCriticalSection (&cond->lock);
264 if (cond->list != NULL)
266 SetEvent (_dbus_list_pop_first (&cond->list));
267 /* Avoid live lock by pushing the waiter to the mutex lock
268 instruction, which is fair. If we don't do this, we could
269 acquire the condition variable again before the waiter has a
270 chance itself, leading to starvation. */
273 LeaveCriticalSection (&cond->lock);
277 _dbus_threads_init_platform_specific (void)
279 /* We reuse this over several generations, because we can't
280 * free the events once they are in use
282 if (dbus_cond_event_tls == TLS_OUT_OF_INDEXES)
284 dbus_cond_event_tls = TlsAlloc ();
285 if (dbus_cond_event_tls == TLS_OUT_OF_INDEXES)
293 _dbus_threads_lock_platform_specific (void)
295 _dbus_assert (global_init_done);
296 EnterCriticalSection (&init_lock);
300 _dbus_threads_unlock_platform_specific (void)
302 _dbus_assert (global_init_done);
303 LeaveCriticalSection (&init_lock);
306 #ifdef DBUS_ENABLE_VERBOSE_MODE
308 _dbus_print_thread (void)
310 fprintf (stderr, "%lu: 0x%04lx: ", _dbus_pid_for_log (), GetCurrentThreadId ());