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"
33 static dbus_bool_t global_init_done = FALSE;
34 static CRITICAL_SECTION init_lock;
36 /* Called from C++ code in dbus-init-win.cpp. */
38 _dbus_threads_windows_init_global (void)
40 /* this ensures that the object that acts as our global constructor
41 * actually gets linked in when we're linked statically */
42 _dbus_threads_windows_ensure_ctor_linked ();
44 InitializeCriticalSection (&init_lock);
45 global_init_done = TRUE;
49 DBusList *list; /**< list thread-local-stored events waiting on the cond variable */
50 CRITICAL_SECTION lock; /**< lock protecting the list */
53 static DWORD dbus_cond_event_tls = TLS_OUT_OF_INDEXES;
56 static HMODULE dbus_dll_hmodule;
59 _dbus_win_get_dll_hmodule (void)
61 return dbus_dll_hmodule;
65 #define hinst_t HANDLE
67 #define hinst_t HINSTANCE
70 BOOL WINAPI DllMain (hinst_t, DWORD, LPVOID);
72 /* We need this to free the TLS events on thread exit */
74 DllMain (hinst_t hinstDLL,
81 case DLL_PROCESS_ATTACH:
82 dbus_dll_hmodule = hinstDLL;
84 case DLL_THREAD_DETACH:
85 if (dbus_cond_event_tls != TLS_OUT_OF_INDEXES)
87 event = TlsGetValue(dbus_cond_event_tls);
89 TlsSetValue(dbus_cond_event_tls, NULL);
92 case DLL_PROCESS_DETACH:
93 if (dbus_cond_event_tls != TLS_OUT_OF_INDEXES)
95 event = TlsGetValue(dbus_cond_event_tls);
97 TlsSetValue(dbus_cond_event_tls, NULL);
99 TlsFree(dbus_cond_event_tls);
109 _dbus_platform_cmutex_new (void)
112 handle = CreateMutex (NULL, FALSE, NULL);
113 return (DBusCMutex *) handle;
117 _dbus_platform_rmutex_new (void)
120 handle = CreateMutex (NULL, FALSE, NULL);
121 return (DBusRMutex *) handle;
125 _dbus_platform_cmutex_free (DBusCMutex *mutex)
127 CloseHandle ((HANDLE *) mutex);
131 _dbus_platform_rmutex_free (DBusRMutex *mutex)
133 CloseHandle ((HANDLE *) mutex);
137 _dbus_platform_cmutex_lock (DBusCMutex *mutex)
139 WaitForSingleObject ((HANDLE *) mutex, INFINITE);
143 _dbus_platform_rmutex_lock (DBusRMutex *mutex)
145 WaitForSingleObject ((HANDLE *) mutex, INFINITE);
149 _dbus_platform_cmutex_unlock (DBusCMutex *mutex)
151 ReleaseMutex ((HANDLE *) mutex);
155 _dbus_platform_rmutex_unlock (DBusRMutex *mutex)
157 ReleaseMutex ((HANDLE *) mutex);
161 _dbus_platform_condvar_new (void)
165 cond = dbus_new (DBusCondVar, 1);
171 InitializeCriticalSection (&cond->lock);
176 _dbus_platform_condvar_free (DBusCondVar *cond)
178 DeleteCriticalSection (&cond->lock);
179 _dbus_list_clear (&cond->list);
184 _dbus_condvar_wait_win32 (DBusCondVar *cond,
190 HANDLE event = TlsGetValue (dbus_cond_event_tls);
194 event = CreateEvent (0, FALSE, FALSE, NULL);
197 TlsSetValue (dbus_cond_event_tls, event);
200 EnterCriticalSection (&cond->lock);
202 /* The event must not be signaled. Check this */
203 _dbus_assert (WaitForSingleObject (event, 0) == WAIT_TIMEOUT);
205 ret = _dbus_list_append (&cond->list, event);
207 LeaveCriticalSection (&cond->lock);
210 return FALSE; /* Prepend failed */
212 _dbus_platform_cmutex_unlock (mutex);
213 retval = WaitForSingleObject (event, milliseconds);
214 _dbus_platform_cmutex_lock (mutex);
216 if (retval == WAIT_TIMEOUT)
218 EnterCriticalSection (&cond->lock);
219 _dbus_list_remove (&cond->list, event);
221 /* In the meantime we could have been signaled, so we must again
222 * wait for the signal, this time with no timeout, to reset
223 * it. retval is set again to honour the late arrival of the
225 retval = WaitForSingleObject (event, 0);
227 LeaveCriticalSection (&cond->lock);
230 #ifndef DBUS_DISABLE_ASSERT
231 EnterCriticalSection (&cond->lock);
233 /* Now event must not be inside the array, check this */
234 _dbus_assert (_dbus_list_remove (&cond->list, event) == FALSE);
236 LeaveCriticalSection (&cond->lock);
237 #endif /* !G_DISABLE_ASSERT */
239 return retval != WAIT_TIMEOUT;
243 _dbus_platform_condvar_wait (DBusCondVar *cond,
246 _dbus_condvar_wait_win32 (cond, mutex, INFINITE);
250 _dbus_platform_condvar_wait_timeout (DBusCondVar *cond,
252 int timeout_milliseconds)
254 return _dbus_condvar_wait_win32 (cond, mutex, timeout_milliseconds);
258 _dbus_platform_condvar_wake_one (DBusCondVar *cond)
260 EnterCriticalSection (&cond->lock);
262 if (cond->list != NULL)
264 SetEvent (_dbus_list_pop_first (&cond->list));
265 /* Avoid live lock by pushing the waiter to the mutex lock
266 instruction, which is fair. If we don't do this, we could
267 acquire the condition variable again before the waiter has a
268 chance itself, leading to starvation. */
271 LeaveCriticalSection (&cond->lock);
275 _dbus_threads_init_platform_specific (void)
277 /* We reuse this over several generations, because we can't
278 * free the events once they are in use
280 if (dbus_cond_event_tls == TLS_OUT_OF_INDEXES)
282 dbus_cond_event_tls = TlsAlloc ();
283 if (dbus_cond_event_tls == TLS_OUT_OF_INDEXES)
291 _dbus_threads_lock_platform_specific (void)
293 _dbus_assert (global_init_done);
294 EnterCriticalSection (&init_lock);
298 _dbus_threads_unlock_platform_specific (void)
300 _dbus_assert (global_init_done);
301 LeaveCriticalSection (&init_lock);