27#include "dbus-init-win.h"
28#include "dbus-internals.h"
29#include "dbus-sysdeps.h"
30#include "dbus-sysdeps-win.h"
31#include "dbus-threads.h"
38#ifdef DBUS_DISABLE_ASSERT
39#define THREAD_CHECK_TRUE(func_name, result_or_call) \
40 do { if (!(result_or_call)) { } } while (0)
42#define THREAD_CHECK_TRUE(func_name, result_or_call) do { \
43 if (!(result_or_call)) { \
44 _dbus_warn_check_failed ("thread function %s failed (windows error code=%ld) in %s", \
45 func_name, GetLastError (), _DBUS_FUNCTION_NAME); \
51static dbus_bool_t global_init_done =
FALSE;
52static CRITICAL_SECTION init_lock;
56_dbus_threads_windows_init_global (
void)
60 _dbus_threads_windows_ensure_ctor_linked ();
62 InitializeCriticalSection (&init_lock);
63 global_init_done =
TRUE;
71static DWORD dbus_cond_event_tls = TLS_OUT_OF_INDEXES;
74static HMODULE dbus_dll_hmodule;
77_dbus_win_get_dll_hmodule (
void)
79 return dbus_dll_hmodule;
85#define hinst_t HINSTANCE
88BOOL WINAPI DllMain (hinst_t, DWORD, LPVOID);
92DllMain (hinst_t hinstDLL,
99 case DLL_PROCESS_ATTACH:
100 dbus_dll_hmodule = hinstDLL;
102 case DLL_THREAD_DETACH:
103 if (dbus_cond_event_tls != TLS_OUT_OF_INDEXES)
105 event = TlsGetValue(dbus_cond_event_tls);
107 TlsSetValue(dbus_cond_event_tls,
NULL);
110 case DLL_PROCESS_DETACH:
111 if (dbus_cond_event_tls != TLS_OUT_OF_INDEXES)
113 event = TlsGetValue(dbus_cond_event_tls);
115 TlsSetValue(dbus_cond_event_tls,
NULL);
117 TlsFree(dbus_cond_event_tls);
127_dbus_platform_cmutex_new (
void)
131 THREAD_CHECK_TRUE (
"CreateMutex", handle);
136_dbus_platform_rmutex_new (
void)
140 THREAD_CHECK_TRUE (
"CreateMutex", handle);
145_dbus_win_rmutex_named_new (
const char *name)
148 handle = CreateMutex (
NULL,
FALSE, name);
149 THREAD_CHECK_TRUE (
"CreateMutex", handle);
156 THREAD_CHECK_TRUE (
"CloseHandle", CloseHandle ((HANDLE *) mutex));
162 THREAD_CHECK_TRUE (
"CloseHandle", CloseHandle ((HANDLE *) mutex));
168 THREAD_CHECK_TRUE (
"WaitForSingleObject", WaitForSingleObject ((HANDLE *) mutex, INFINITE) == WAIT_OBJECT_0);
174 THREAD_CHECK_TRUE (
"WaitForSingleObject", WaitForSingleObject ((HANDLE *) mutex, INFINITE) == WAIT_OBJECT_0);
178_dbus_platform_cmutex_unlock (
DBusCMutex *mutex)
180 THREAD_CHECK_TRUE (
"ReleaseMutex", ReleaseMutex ((HANDLE *) mutex));
184_dbus_platform_rmutex_unlock (
DBusRMutex *mutex)
186 THREAD_CHECK_TRUE (
"ReleaseMutex", ReleaseMutex ((HANDLE *) mutex));
190_dbus_platform_condvar_new (
void)
200 InitializeCriticalSection (&cond->
lock);
207 DeleteCriticalSection (&cond->
lock);
219 HANDLE
event = TlsGetValue (dbus_cond_event_tls);
226 TlsSetValue (dbus_cond_event_tls, event);
229 EnterCriticalSection (&cond->
lock);
232 _dbus_assert (WaitForSingleObject (event, 0) == WAIT_TIMEOUT);
236 LeaveCriticalSection (&cond->
lock);
241 _dbus_platform_cmutex_unlock (mutex);
242 retval = WaitForSingleObject (event, milliseconds);
243 _dbus_platform_cmutex_lock (mutex);
245 if (retval == WAIT_TIMEOUT)
247 EnterCriticalSection (&cond->
lock);
254 retval = WaitForSingleObject (event, 0);
256 LeaveCriticalSection (&cond->
lock);
259#ifndef DBUS_DISABLE_ASSERT
260 EnterCriticalSection (&cond->
lock);
265 LeaveCriticalSection (&cond->
lock);
268 return retval != WAIT_TIMEOUT;
275 _dbus_condvar_wait_win32 (cond, mutex, INFINITE);
279_dbus_platform_condvar_wait_timeout (
DBusCondVar *cond,
281 int timeout_milliseconds)
283 return _dbus_condvar_wait_win32 (cond, mutex, timeout_milliseconds);
289 EnterCriticalSection (&cond->
lock);
300 LeaveCriticalSection (&cond->
lock);
309 if (dbus_cond_event_tls == TLS_OUT_OF_INDEXES)
311 dbus_cond_event_tls = TlsAlloc ();
312 if (dbus_cond_event_tls == TLS_OUT_OF_INDEXES)
323 EnterCriticalSection (&init_lock);
330 LeaveCriticalSection (&init_lock);
333#ifdef DBUS_ENABLE_VERBOSE_MODE
335_dbus_print_thread (
void)
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
dbus_bool_t _dbus_list_remove(DBusList **list, void *data)
Removes a value from the list.
void * _dbus_list_pop_first(DBusList **list)
Removes the first value in the list and returns it.
void _dbus_list_clear(DBusList **list)
Frees all links in the list and sets the list head to NULL.
dbus_bool_t _dbus_list_append(DBusList **list, void *data)
Appends a value to the list.
#define NULL
A null pointer, defined appropriately for C or C++.
#define TRUE
Expands to "1".
#define FALSE
Expands to "0".
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
#define dbus_new(type, count)
Safe macro for using dbus_malloc().
unsigned long _dbus_pid_for_log(void)
The only reason this is separate from _dbus_getpid() is to allow it on Windows for logging but not fo...
void _dbus_threads_lock_platform_specific(void)
Lock a static mutex used to protect _dbus_threads_init_platform_specific().
void _dbus_threads_unlock_platform_specific(void)
Undo _dbus_threads_lock_platform_specific().
dbus_bool_t _dbus_threads_init_platform_specific(void)
Initialize threads as in dbus_threads_init_default(), appropriately for the platform.
CRITICAL_SECTION lock
lock protecting the list
DBusList * list
list thread-local-stored events waiting on the cond variable