1 /* -*- mode: C; c-file-style: "gnu" -*- */
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "dbus-internals.h"
25 #include "dbus-sysdeps.h"
26 #include "dbus-threads.h"
31 DBusList *list; /**< list thread-local-stored events waiting on the cond variable */
32 CRITICAL_SECTION lock; /**< lock protecting the list */
35 static DWORD dbus_cond_event_tls = TLS_OUT_OF_INDEXES;
38 BOOL WINAPI DllMain (HINSTANCE hinstDLL,
42 /* We need this to free the TLS events on thread exit */
44 DllMain (HINSTANCE hinstDLL,
51 case DLL_THREAD_DETACH:
52 if (dbus_cond_event_tls != TLS_OUT_OF_INDEXES)
54 event = TlsGetValue(dbus_cond_event_tls);
56 TlsSetValue(dbus_cond_event_tls, NULL);
59 case DLL_PROCESS_DETACH:
60 if (dbus_cond_event_tls != TLS_OUT_OF_INDEXES)
62 event = TlsGetValue(dbus_cond_event_tls);
64 TlsSetValue(dbus_cond_event_tls, NULL);
66 TlsFree(dbus_cond_event_tls);
76 _dbus_windows_mutex_new (void)
79 handle = CreateMutex (NULL, FALSE, NULL);
80 return (DBusMutex *) handle;
84 _dbus_windows_mutex_free (DBusMutex *mutex)
86 CloseHandle ((HANDLE *) mutex);
90 _dbus_windows_mutex_lock (DBusMutex *mutex)
92 return WaitForSingleObject ((HANDLE *) mutex, INFINITE) != WAIT_FAILED;
96 _dbus_windows_mutex_unlock (DBusMutex *mutex)
98 return ReleaseMutex ((HANDLE *) mutex) != 0;
102 _dbus_windows_condvar_new (void)
106 cond = dbus_new (DBusCondVar, 1);
112 InitializeCriticalSection (&cond->lock);
113 return (DBusCondVar *) cond;
117 _dbus_windows_condvar_free (DBusCondVar *cond)
119 DeleteCriticalSection (&cond->lock);
120 _dbus_list_clear (&cond->list);
125 _dbus_condvar_wait_win32 (DBusCondVar *cond,
131 HANDLE event = TlsGetValue (dbus_cond_event_tls);
135 event = CreateEvent (0, FALSE, FALSE, NULL);
138 TlsSetValue (dbus_cond_event_tls, event);
141 EnterCriticalSection (&cond->lock);
143 /* The event must not be signaled. Check this */
144 _dbus_assert (WaitForSingleObject (event, 0) == WAIT_TIMEOUT);
146 ret = _dbus_list_append (&cond->list, event);
148 LeaveCriticalSection (&cond->lock);
151 return FALSE; /* Prepend failed */
153 _dbus_mutex_unlock (mutex);
154 retval = WaitForSingleObject (event, milliseconds);
155 _dbus_mutex_lock (mutex);
157 if (retval == WAIT_TIMEOUT)
159 EnterCriticalSection (&cond->lock);
160 _dbus_list_remove (&cond->list, event);
162 /* In the meantime we could have been signaled, so we must again
163 * wait for the signal, this time with no timeout, to reset
164 * it. retval is set again to honour the late arrival of the
166 retval = WaitForSingleObject (event, 0);
168 LeaveCriticalSection (&cond->lock);
171 #ifndef DBUS_DISABLE_ASSERT
172 EnterCriticalSection (&cond->lock);
174 /* Now event must not be inside the array, check this */
175 _dbus_assert (_dbus_list_remove (cond->list, event) == FALSE);
177 LeaveCriticalSection (&cond->lock);
178 #endif /* !G_DISABLE_ASSERT */
180 return retval != WAIT_TIMEOUT;
184 _dbus_windows_condvar_wait (DBusCondVar *cond,
187 _dbus_condvar_wait_win32 (cond, mutex, INFINITE);
191 _dbus_windows_condvar_wait_timeout (DBusCondVar *cond,
193 int timeout_milliseconds)
195 return _dbus_condvar_wait_win32 (cond, mutex, timeout_milliseconds);
199 _dbus_windows_condvar_wake_one (DBusCondVar *cond)
201 EnterCriticalSection (&cond->lock);
203 if (cond->list != NULL)
204 SetEvent (_dbus_list_pop_first (&cond->list));
206 LeaveCriticalSection (&cond->lock);
210 _dbus_windows_condvar_wake_all (DBusCondVar *cond)
212 EnterCriticalSection (&cond->lock);
214 while (cond->list != NULL)
215 SetEvent (_dbus_list_pop_first (&cond->list));
217 LeaveCriticalSection (&cond->lock);
220 static const DBusThreadFunctions windows_functions =
222 DBUS_THREAD_FUNCTIONS_MUTEX_NEW_MASK |
223 DBUS_THREAD_FUNCTIONS_MUTEX_FREE_MASK |
224 DBUS_THREAD_FUNCTIONS_MUTEX_LOCK_MASK |
225 DBUS_THREAD_FUNCTIONS_MUTEX_UNLOCK_MASK |
226 DBUS_THREAD_FUNCTIONS_CONDVAR_NEW_MASK |
227 DBUS_THREAD_FUNCTIONS_CONDVAR_FREE_MASK |
228 DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_MASK |
229 DBUS_THREAD_FUNCTIONS_CONDVAR_WAIT_TIMEOUT_MASK |
230 DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ONE_MASK|
231 DBUS_THREAD_FUNCTIONS_CONDVAR_WAKE_ALL_MASK,
232 _dbus_windows_mutex_new,
233 _dbus_windows_mutex_free,
234 _dbus_windows_mutex_lock,
235 _dbus_windows_mutex_unlock,
236 _dbus_windows_condvar_new,
237 _dbus_windows_condvar_free,
238 _dbus_windows_condvar_wait,
239 _dbus_windows_condvar_wait_timeout,
240 _dbus_windows_condvar_wake_one,
241 _dbus_windows_condvar_wake_all
245 _dbus_threads_init_platform_specific (void)
247 /* We reuse this over several generations, because we can't
248 * free the events once they are in use
250 if (dbus_cond_event_tls == TLS_OUT_OF_INDEXES)
252 dbus_cond_event_tls = TlsAlloc ();
253 if (dbus_cond_event_tls == TLS_OUT_OF_INDEXES)
257 return dbus_threads_init (&windows_functions);