Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
The default alias name is now build from the name of the host of the process and...
[simgrid.git] / src / xbt / xbt_os_thread.c
index c7e40d3..46196d6 100644 (file)
@@ -103,6 +103,13 @@ void xbt_os_thread_mod_exit(void) {
 
 //   if ((errcode=pthread_key_delete(xbt_self_thread_key)))
 //     THROW0(system_error,errcode,"pthread_key_delete failed for xbt_self_thread_key");
+   free(main_thread->exception);
+   free(main_thread);
+   main_thread = NULL;
+   thread_mod_inited=0;
+#ifndef HAVE_SEM_WAIT
+   xbt_os_mutex_destroy(next_sem_ID_lock);
+#endif
 }
 
 static void * wrapper_start_routine(void *s) {
@@ -155,6 +162,9 @@ xbt_os_thread_join(xbt_os_thread_t thread,void ** thread_return) {
    if (thread->exception)
      free(thread->exception);
 
+   if (thread->name)
+     free(thread->name);
+   
    if (thread == main_thread) /* just killed main thread */
      main_thread = NULL;
 
@@ -229,7 +239,7 @@ void xbt_os_mutex_timedacquire(xbt_os_mutex_t mutex, double delay) {
        case ETIMEDOUT:
         THROW1(timeout_error,0,"mutex %p not ready",mutex);    
        default:
-        THROW2(system_error,errcode,"xbt_mutex_tryacquire(%p) failed: %s",mutex, strerror(errcode));
+        THROW2(system_error,errcode,"xbt_mutex_timedacquire(%p) failed: %s",mutex, strerror(errcode));
       }
 
                
@@ -441,7 +451,7 @@ void xbt_os_sem_timedacquire(xbt_os_sem_t sem, double delay) {
        case ETIMEDOUT:
         THROW1(timeout_error,0,"semaphore %p not ready",sem);
        default:
-        THROW2(system_error,errcode,"xbt_sem_tryacquire(%p) failed: %s",sem, strerror(errcode));
+        THROW2(system_error,errcode,"xbt_os_sem_timedacquire(%p) failed: %s",sem, strerror(errcode));
       }
       
    } else {
@@ -557,14 +567,15 @@ void xbt_os_thread_mod_exit(void) {
 
 static DWORD WINAPI  wrapper_start_routine(void *s) {
   xbt_os_thread_t t = (xbt_os_thread_t)s;
-  void* rv;
+  DWORD* rv;
 
     if(!TlsSetValue(xbt_self_thread_key,t))
      THROW0(system_error,(int)GetLastError(),"TlsSetValue of data describing the created thread failed");
 
-   rv = (*(t->start_routine))(t->param);
+   rv = (DWORD*)((t->start_routine)(t->param));
 
-   return *((DWORD*)rv);
+   return rv ? *rv : 0;
+   
 }
 
 
@@ -611,7 +622,10 @@ xbt_os_thread_join(xbt_os_thread_t thread,void ** thread_return) {
        }
 
        CloseHandle(thread->handle);
-       free(thread->name);
+       
+       if(thread->name)
+               free(thread->name);
+       
        free(thread);
 }
 
@@ -636,7 +650,8 @@ void xbt_os_thread_yield(void) {
     Sleep(0);
 }
 void xbt_os_thread_cancel(xbt_os_thread_t t) {
-   THROW_UNIMPLEMENTED;
+  if(!TerminateThread(t->handle,0))
+               THROW0(system_error,(int)GetLastError(), "TerminateThread failed");
 }
 
 /****** mutex related functions ******/
@@ -655,15 +670,9 @@ xbt_os_mutex_t xbt_os_mutex_init(void) {
 }
 
 void xbt_os_mutex_acquire(xbt_os_mutex_t mutex) {
-
    EnterCriticalSection(& mutex->lock);
 }
 
-void xbt_os_mutex_tryacquire(xbt_os_mutex_t mutex)
-{
-       TryEnterCriticalSection(&mutex->lock);
-}
-
 void xbt_os_mutex_timedacquire(xbt_os_mutex_t mutex, double delay) {
        THROW_UNIMPLEMENTED;
 }
@@ -780,7 +789,7 @@ void xbt_os_cond_timedwait(xbt_os_cond_t cond, xbt_os_mutex_t mutex, double dela
    if (delay < 0) {
       xbt_os_cond_wait(cond,mutex);
    } else {
-         DEBUG3("xbt_cond_timedwait(%p,%p,%ul)",&(cond->events),&(mutex->lock),end);
+         DEBUG3("xbt_cond_timedwait(%p,%p,%lu)",&(cond->events),&(mutex->lock),end);
 
    /* lock the threads counter and increment it */
    EnterCriticalSection (& cond->waiters_count_lock);
@@ -873,6 +882,10 @@ typedef struct xbt_os_sem_ {
    CRITICAL_SECTION value_lock;  /* protect access to value of the semaphore  */
 }s_xbt_os_sem_t ;
 
+#ifndef INT_MAX
+# define INT_MAX 32767 /* let's be safe by underestimating this value: this is for 16bits only */
+#endif
+
 xbt_os_sem_t
 xbt_os_sem_init(unsigned int value)
 {