1 #include "win_thread.h"
6 win_thread_create(win_thread_t* thread,pfn_start_routine_t start_routine,void* param){
8 *thread = xbt_new0(s_win_thread_t,1);
13 (*thread)->handle = CreateThread(NULL,NULL,start_routine,param,0,&((*thread)->id));
15 if(!((*thread)->handle)){
25 win_thread_exit(win_thread_t* thread,unsigned long exit_code)
28 ExitThread(exit_code);
37 win_thread_self(void){
38 return GetCurrentThreadId();
42 win_thread_mutex_init(win_thread_mutex_t* mutex)
44 *mutex = xbt_new0(s_win_thread_mutex_t,1);
49 /* initialize the critical section object */
50 InitializeCriticalSection(&((*mutex)->lock));
56 win_thread_mutex_lock(win_thread_mutex_t* mutex){
58 EnterCriticalSection (&((*mutex)->lock));
63 win_thread_mutex_unlock(win_thread_mutex_t* mutex){
65 LeaveCriticalSection (&(*mutex)->lock);
70 win_thread_mutex_destroy(win_thread_mutex_t* mutex){
72 DeleteCriticalSection(&((*mutex)->lock));
81 win_thread_cond_init(win_thread_cond_t* cond){
83 *cond = xbt_new0(s_win_thread_cond_t,1);
88 memset(&((*cond)->waiters_count_lock),0,sizeof(CRITICAL_SECTION));
90 /* initialize the critical section object */
91 InitializeCriticalSection(&((*cond)->waiters_count_lock));
93 (*cond)->waiters_count = 0;
95 /* Create an auto-reset event */
96 (*cond)->events[SIGNAL] = CreateEvent (NULL, FALSE, FALSE, NULL);
98 if(!(*cond)->events[SIGNAL]){
99 DeleteCriticalSection(&((*cond)->waiters_count_lock));
104 /* Create a manual-reset event. */
105 (*cond)->events[BROADCAST] = CreateEvent (NULL, TRUE, FALSE,NULL);
107 if(!(*cond)->events[BROADCAST]){
109 DeleteCriticalSection(&((*cond)->waiters_count_lock));
111 if(!CloseHandle((*cond)->events[SIGNAL]))
123 win_thread_cond_wait(win_thread_cond_t* cond,win_thread_mutex_t* mutex){
125 unsigned long wait_result;
128 /* lock the threads counter and increment it */
129 EnterCriticalSection (&((*cond)->waiters_count_lock));
130 (*cond)->waiters_count++;
131 LeaveCriticalSection (&((*cond)->waiters_count_lock));
134 /* unlock the mutex associate with the condition */
135 LeaveCriticalSection (&(*mutex)->lock);
137 /* wait for a signal (broadcast or no) */
138 wait_result = WaitForMultipleObjects (2, (*cond)->events, FALSE, INFINITE);
140 if(WAIT_FAILED == wait_result)
143 /* we have a signal lock the condition */
144 EnterCriticalSection (&((*cond)->waiters_count_lock));
145 (*cond)->waiters_count--;
147 /* it's the last waiter or it's a broadcast ? */
148 is_last_waiter = ((wait_result == WAIT_OBJECT_0 + BROADCAST - 1) && ((*cond)->waiters_count == 0));
150 LeaveCriticalSection (&((*cond)->waiters_count_lock));
152 /* yes it's the last waiter or it's a broadcast
153 * only reset the manual event (the automatic event is reset in the WaitForMultipleObjects() function
158 if(!ResetEvent ((*cond)->events[BROADCAST]))
162 /* relock the mutex associated with the condition in accordance with the posix thread specification */
163 EnterCriticalSection (&(*mutex)->lock);
170 win_thread_cond_signal(win_thread_cond_t* cond)
174 EnterCriticalSection (&((*cond)->waiters_count_lock));
175 have_waiters = (*cond)->waiters_count > 0;
176 LeaveCriticalSection (&((*cond)->waiters_count_lock));
180 if(!SetEvent((*cond)->events[SIGNAL]))
188 win_thread_cond_broadcast(win_thread_cond_t* cond)
192 EnterCriticalSection (&((*cond)->waiters_count_lock));
193 have_waiters = (*cond)->waiters_count > 0;
194 LeaveCriticalSection (&((*cond)->waiters_count_lock));
197 SetEvent((*cond)->events[BROADCAST]);
205 win_thread_cond_destroy(win_thread_cond_t* cond)
209 if(!CloseHandle((*cond)->events[SIGNAL]))
212 if(!CloseHandle((*cond)->events[BROADCAST]))
215 DeleteCriticalSection(&((*cond)->waiters_count_lock));