From 84a277aaf6a5232eb0166be05eb8053992a17da3 Mon Sep 17 00:00:00 2001 From: cherierm Date: Thu, 8 Mar 2007 15:20:29 +0000 Subject: [PATCH 1/1] wint_hread update git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@3209 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- src/xbt/win_thread.c | 45 +++++++++++++++++++++++++++----------------- src/xbt/win_thread.h | 6 ++++++ 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/src/xbt/win_thread.c b/src/xbt/win_thread.c index ba25922084..0e6e3c467c 100644 --- a/src/xbt/win_thread.c +++ b/src/xbt/win_thread.c @@ -1,6 +1,7 @@ #include "win_thread.h" #include +#include int win_thread_create(win_thread_t* thread,pfn_start_routine_t start_routine,void* param){ @@ -24,11 +25,15 @@ win_thread_create(win_thread_t* thread,pfn_start_routine_t start_routine,void* p int win_thread_exit(win_thread_t* thread,unsigned long exit_code) { - - ExitThread(exit_code); - - free(*thread); - *thread = NULL; + win_thread_t ___thread = *thread; + + CloseHandle(___thread->handle); + free(___thread); + ___thread = NULL; + + ExitThread(exit_code); + + return 0; } @@ -41,6 +46,7 @@ win_thread_self(void){ int win_thread_mutex_init(win_thread_mutex_t* mutex) { + *mutex = xbt_new0(s_win_thread_mutex_t,1); if(!*mutex) @@ -48,14 +54,14 @@ win_thread_mutex_init(win_thread_mutex_t* mutex) /* initialize the critical section object */ InitializeCriticalSection(&((*mutex)->lock)); - return 0; } int win_thread_mutex_lock(win_thread_mutex_t* mutex){ - EnterCriticalSection (&((*mutex)->lock)); + EnterCriticalSection(&((*mutex)->lock)); + return 0; } @@ -68,7 +74,7 @@ win_thread_mutex_unlock(win_thread_mutex_t* mutex){ int win_thread_mutex_destroy(win_thread_mutex_t* mutex){ - + DeleteCriticalSection(&((*mutex)->lock)); free(*mutex); @@ -79,12 +85,12 @@ win_thread_mutex_destroy(win_thread_mutex_t* mutex){ int win_thread_cond_init(win_thread_cond_t* cond){ - + *cond = xbt_new0(s_win_thread_cond_t,1); if(!*cond) return 1; - + memset(&((*cond)->waiters_count_lock),0,sizeof(CRITICAL_SECTION)); /* initialize the critical section object */ @@ -115,7 +121,6 @@ win_thread_cond_init(win_thread_cond_t* cond){ return 1; } - return 0; } @@ -124,7 +129,7 @@ win_thread_cond_wait(win_thread_cond_t* cond,win_thread_mutex_t* mutex){ unsigned long wait_result; int is_last_waiter; - + /* lock the threads counter and increment it */ EnterCriticalSection (&((*cond)->waiters_count_lock)); (*cond)->waiters_count++; @@ -162,7 +167,6 @@ win_thread_cond_wait(win_thread_cond_t* cond,win_thread_mutex_t* mutex){ /* relock the mutex associated with the condition in accordance with the posix thread specification */ EnterCriticalSection (&(*mutex)->lock); - return 0; } @@ -170,7 +174,7 @@ int win_thread_cond_signal(win_thread_cond_t* cond) { int have_waiters; - + EnterCriticalSection (&((*cond)->waiters_count_lock)); have_waiters = (*cond)->waiters_count > 0; LeaveCriticalSection (&((*cond)->waiters_count_lock)); @@ -179,6 +183,7 @@ win_thread_cond_signal(win_thread_cond_t* cond) if(!SetEvent((*cond)->events[SIGNAL])) return 1; + } return 0; @@ -188,13 +193,13 @@ int win_thread_cond_broadcast(win_thread_cond_t* cond) { int have_waiters; - + EnterCriticalSection (&((*cond)->waiters_count_lock)); have_waiters = (*cond)->waiters_count > 0; LeaveCriticalSection (&((*cond)->waiters_count_lock)); if (have_waiters) - SetEvent((*cond)->events[BROADCAST]); + SetEvent((*cond)->events[BROADCAST]); return 0; } @@ -205,7 +210,7 @@ int win_thread_cond_destroy(win_thread_cond_t* cond) { int result = 0; - + if(!CloseHandle((*cond)->events[SIGNAL])) result= 1; @@ -220,4 +225,10 @@ win_thread_cond_destroy(win_thread_cond_t* cond) return result; } +void +win_thread_yield(void) +{ + Sleep(0); +} + diff --git a/src/xbt/win_thread.h b/src/xbt/win_thread.h index f946434c5e..f6c47e6d9f 100644 --- a/src/xbt/win_thread.h +++ b/src/xbt/win_thread.h @@ -177,6 +177,12 @@ win_thread_cond_broadcast(win_thread_cond_t* cond); int win_thread_cond_destroy(win_thread_cond_t* cond); +/* + * Forces the current thread to relinquish use of its processor. + */ +void +win_thread_yield(void); + #ifdef __cplusplus extern } #endif -- 2.20.1