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
24 #include "dbus-internals.h"
25 #include "dbus-sysdeps.h"
26 #include "dbus-threads.h"
27 #include "dbus-list.h"
32 DBusList *list; /**< list thread-local-stored events waiting on the cond variable */
33 CRITICAL_SECTION lock; /**< lock protecting the list */
36 static DWORD dbus_cond_event_tls = TLS_OUT_OF_INDEXES;
39 BOOL WINAPI DllMain (HINSTANCE hinstDLL,
43 /* We need this to free the TLS events on thread exit */
45 DllMain (HINSTANCE hinstDLL,
52 case DLL_THREAD_DETACH:
53 if (dbus_cond_event_tls != TLS_OUT_OF_INDEXES)
55 event = TlsGetValue(dbus_cond_event_tls);
57 TlsSetValue(dbus_cond_event_tls, NULL);
60 case DLL_PROCESS_DETACH:
61 if (dbus_cond_event_tls != TLS_OUT_OF_INDEXES)
63 event = TlsGetValue(dbus_cond_event_tls);
65 TlsSetValue(dbus_cond_event_tls, NULL);
67 TlsFree(dbus_cond_event_tls);
77 _dbus_windows_mutex_new (void)
80 handle = CreateMutex (NULL, FALSE, NULL);
81 return (DBusMutex *) handle;
85 _dbus_windows_mutex_free (DBusMutex *mutex)
87 CloseHandle ((HANDLE *) mutex);
91 _dbus_windows_mutex_lock (DBusMutex *mutex)
93 return WaitForSingleObject ((HANDLE *) mutex, INFINITE) != WAIT_FAILED;
97 _dbus_windows_mutex_unlock (DBusMutex *mutex)
99 return ReleaseMutex ((HANDLE *) mutex) != 0;
103 _dbus_windows_condvar_new (void)
107 cond = dbus_new (DBusCondVar, 1);
113 InitializeCriticalSection (&cond->lock);
114 return (DBusCondVar *) cond;
118 _dbus_windows_condvar_free (DBusCondVar *cond)
120 DeleteCriticalSection (&cond->lock);
121 _dbus_list_clear (&cond->list);
126 _dbus_condvar_wait_win32 (DBusCondVar *cond,
132 HANDLE event = TlsGetValue (dbus_cond_event_tls);
136 event = CreateEvent (0, FALSE, FALSE, NULL);
139 TlsSetValue (dbus_cond_event_tls, event);
142 EnterCriticalSection (&cond->lock);
144 /* The event must not be signaled. Check this */
145 _dbus_assert (WaitForSingleObject (event, 0) == WAIT_TIMEOUT);
147 ret = _dbus_list_append (&cond->list, event);
149 LeaveCriticalSection (&cond->lock);
152 return FALSE; /* Prepend failed */
154 _dbus_mutex_unlock (mutex);
155 retval = WaitForSingleObject (event, milliseconds);
156 _dbus_mutex_lock (mutex);
158 if (retval == WAIT_TIMEOUT)
160 EnterCriticalSection (&cond->lock);
161 _dbus_list_remove (&cond->list, event);
163 /* In the meantime we could have been signaled, so we must again
164 * wait for the signal, this time with no timeout, to reset
165 * it. retval is set again to honour the late arrival of the
167 retval = WaitForSingleObject (event, 0);
169 LeaveCriticalSection (&cond->lock);
172 #ifndef DBUS_DISABLE_ASSERT
173 EnterCriticalSection (&cond->lock);
175 /* Now event must not be inside the array, check this */
176 _dbus_assert (_dbus_list_remove (&cond->list, event) == FALSE);
178 LeaveCriticalSection (&cond->lock);
179 #endif /* !G_DISABLE_ASSERT */
181 return retval != WAIT_TIMEOUT;
185 _dbus_windows_condvar_wait (DBusCondVar *cond,
188 _dbus_condvar_wait_win32 (cond, mutex, INFINITE);
192 _dbus_windows_condvar_wait_timeout (DBusCondVar *cond,
194 int timeout_milliseconds)
196 return _dbus_condvar_wait_win32 (cond, mutex, timeout_milliseconds);
200 _dbus_windows_condvar_wake_one (DBusCondVar *cond)
202 EnterCriticalSection (&cond->lock);
204 if (cond->list != NULL)
205 SetEvent (_dbus_list_pop_first (&cond->list));
207 LeaveCriticalSection (&cond->lock);
211 _dbus_windows_condvar_wake_all (DBusCondVar *cond)
213 EnterCriticalSection (&cond->lock);
215 while (cond->list != NULL)
216 SetEvent (_dbus_list_pop_first (&cond->list));
218 LeaveCriticalSection (&cond->lock);
221 static const DBusThreadFunctions windows_functions =
223 DBUS_THREAD_FUNCTIONS_MUTEX_NEW_MASK |
224 DBUS_THREAD_FUNCTIONS_MUTEX_FREE_MASK |
225 DBUS_THREAD_FUNCTIONS_MUTEX_LOCK_MASK |
226 DBUS_THREAD_FUNCTIONS_MUTEX_UNLOCK_MASK |
227 DBUS_THREAD_FUNCTIONS_CONDVAR_NEW_MASK |
228 DBUS_THREAD_FUNCTIONS_CONDVAR_FREE_MASK |
229 DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_MASK |
230 DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_TIMEOUT_MASK |
231 DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ONE_MASK|
232 DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ALL_MASK,
233 _dbus_windows_mutex_new,
234 _dbus_windows_mutex_free,
235 _dbus_windows_mutex_lock,
236 _dbus_windows_mutex_unlock,
237 _dbus_windows_condvar_new,
238 _dbus_windows_condvar_free,
239 _dbus_windows_condvar_wait,
240 _dbus_windows_condvar_wait_timeout,
241 _dbus_windows_condvar_wake_one,
242 _dbus_windows_condvar_wake_all
246 _dbus_threads_init_platform_specific (void)
248 /* We reuse this over several generations, because we can't
249 * free the events once they are in use
251 if (dbus_cond_event_tls == TLS_OUT_OF_INDEXES)
253 dbus_cond_event_tls = TlsAlloc ();
254 if (dbus_cond_event_tls == TLS_OUT_OF_INDEXES)
258 return dbus_threads_init (&windows_functions);