1 #include "win_thread.h"
7 win_thread_create(win_thread_t* thread,pfn_start_routine_t start_routine,void* param){
9 *thread = xbt_new0(s_win_thread_t,1);
14 (*thread)->handle = CreateThread(NULL,NULL,start_routine,param,0,&((*thread)->id));
16 if(!((*thread)->handle)){
26 win_thread_exit(win_thread_t* thread,unsigned long exit_code)
28 win_thread_t ___thread = *thread;
30 CloseHandle(___thread->handle);
34 ExitThread(exit_code);
42 win_thread_self(void){
43 return GetCurrentThreadId();
47 win_thread_mutex_init(win_thread_mutex_t* mutex)
50 *mutex = xbt_new0(s_win_thread_mutex_t,1);
55 /* initialize the critical section object */
56 InitializeCriticalSection(&((*mutex)->lock));
61 win_thread_mutex_lock(win_thread_mutex_t* mutex){
63 EnterCriticalSection(&((*mutex)->lock));
69 win_thread_mutex_unlock(win_thread_mutex_t* mutex){
71 LeaveCriticalSection (&(*mutex)->lock);
76 win_thread_mutex_destroy(win_thread_mutex_t* mutex){
78 DeleteCriticalSection(&((*mutex)->lock));
87 win_thread_cond_init(win_thread_cond_t* cond){
89 *cond = xbt_new0(s_win_thread_cond_t,1);
94 memset(&((*cond)->waiters_count_lock),0,sizeof(CRITICAL_SECTION));
96 /* initialize the critical section object */
97 InitializeCriticalSection(&((*cond)->waiters_count_lock));
99 (*cond)->waiters_count = 0;
101 /* Create an auto-reset event */
102 (*cond)->events[SIGNAL] = CreateEvent (NULL, FALSE, FALSE, NULL);
104 if(!(*cond)->events[SIGNAL]){
105 DeleteCriticalSection(&((*cond)->waiters_count_lock));
110 /* Create a manual-reset event. */
111 (*cond)->events[BROADCAST] = CreateEvent (NULL, TRUE, FALSE,NULL);
113 if(!(*cond)->events[BROADCAST]){
115 DeleteCriticalSection(&((*cond)->waiters_count_lock));
117 if(!CloseHandle((*cond)->events[SIGNAL]))
128 win_thread_cond_wait(win_thread_cond_t* cond,win_thread_mutex_t* mutex){
130 unsigned long wait_result;
133 /* lock the threads counter and increment it */
134 EnterCriticalSection (&((*cond)->waiters_count_lock));
135 (*cond)->waiters_count++;
136 LeaveCriticalSection (&((*cond)->waiters_count_lock));
139 /* unlock the mutex associate with the condition */
140 LeaveCriticalSection (&(*mutex)->lock);
142 /* wait for a signal (broadcast or no) */
143 wait_result = WaitForMultipleObjects (2, (*cond)->events, FALSE, INFINITE);
145 if(WAIT_FAILED == wait_result)
148 /* we have a signal lock the condition */
149 EnterCriticalSection (&((*cond)->waiters_count_lock));
150 (*cond)->waiters_count--;
152 /* it's the last waiter or it's a broadcast ? */
153 is_last_waiter = ((wait_result == WAIT_OBJECT_0 + BROADCAST - 1) && ((*cond)->waiters_count == 0));
155 LeaveCriticalSection (&((*cond)->waiters_count_lock));
157 /* yes it's the last waiter or it's a broadcast
158 * only reset the manual event (the automatic event is reset in the WaitForMultipleObjects() function
163 if(!ResetEvent ((*cond)->events[BROADCAST]))
167 /* relock the mutex associated with the condition in accordance with the posix thread specification */
168 EnterCriticalSection (&(*mutex)->lock);
174 win_thread_cond_signal(win_thread_cond_t* cond)
178 EnterCriticalSection (&((*cond)->waiters_count_lock));
179 have_waiters = (*cond)->waiters_count > 0;
180 LeaveCriticalSection (&((*cond)->waiters_count_lock));
184 if(!SetEvent((*cond)->events[SIGNAL]))
193 win_thread_cond_broadcast(win_thread_cond_t* cond)
197 EnterCriticalSection (&((*cond)->waiters_count_lock));
198 have_waiters = (*cond)->waiters_count > 0;
199 LeaveCriticalSection (&((*cond)->waiters_count_lock));
202 SetEvent((*cond)->events[BROADCAST]);
210 win_thread_cond_destroy(win_thread_cond_t* cond)
214 if(!CloseHandle((*cond)->events[SIGNAL]))
217 if(!CloseHandle((*cond)->events[BROADCAST]))
220 DeleteCriticalSection(&((*cond)->waiters_count_lock));
229 win_thread_yield(void)