Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
On windows pthread_atfork is not implemented
[simgrid.git] / src / xbt / xbt_os_thread.c
index cdafb51..3cb6dbb 100644 (file)
@@ -23,7 +23,6 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_sync_os, xbt,
 /* ********************************* PTHREAD IMPLEMENTATION ************************************ */
 #ifdef HAVE_PTHREAD_H
 
-#include <pthread.h>
 #include <semaphore.h>
 
 #ifdef HAVE_MUTEX_TIMEDLOCK
@@ -138,7 +137,11 @@ void xbt_os_thread_mod_postexit(void)
 int xbt_os_thread_atfork(void (*prepare)(void),
                          void (*parent)(void), void (*child)(void))
 {
+#ifdef WIN32
+       xbt_die("Function pthread_atfork not implemented");
+#else
   return pthread_atfork(prepare, parent, child);
+#endif
 }
 
 static void *wrapper_start_routine(void *s)
@@ -150,7 +153,7 @@ static void *wrapper_start_routine(void *s)
     THROWF(system_error, errcode,
            "pthread_setspecific failed for xbt_self_thread_key");
 
-  void *res = (*(t->start_routine)) (t->param);
+  void *res = t->start_routine(t->param);
   if (t->detached)
     xbt_os_thread_free_thread_data(t);
   return res;
@@ -219,6 +222,24 @@ xbt_os_thread_t xbt_os_thread_self(void)
   return res;
 }
 
+void xbt_os_thread_key_create(xbt_os_thread_key_t* key) {
+
+  int errcode;
+  if ((errcode = pthread_key_create(key, NULL)))
+    THROWF(system_error, errcode, "pthread_key_create failed");
+}
+
+void xbt_os_thread_set_specific(xbt_os_thread_key_t key, void* value) {
+
+  int errcode;
+  if ((errcode = pthread_setspecific(key, value)))
+    THROWF(system_error, errcode, "pthread_setspecific failed");
+}
+
+void* xbt_os_thread_get_specific(xbt_os_thread_key_t key) {
+  return pthread_getspecific(key);
+}
+
 void xbt_os_thread_detach(xbt_os_thread_t thread)
 {
   thread->detached = 1;
@@ -478,7 +499,7 @@ xbt_os_sem_t xbt_os_sem_init(unsigned int value)
   if ((res->ps == (sem_t *) SEM_FAILED) && (errno == ENAMETOOLONG)) {
     /* Old darwins only allow 13 chars. Did you create *that* amount of semaphores? */
     res->name[13] = '\0';
-    res->ps = sem_open(res->name, O_CREAT, 0644, 1);
+    res->ps = sem_open(res->name, O_CREAT, 0644, value);
   }
   if ((res->ps == (sem_t *) SEM_FAILED))
     THROWF(system_error, errno, "sem_open() failed: %s", strerror(errno));
@@ -712,8 +733,7 @@ void xbt_os_thread_join(xbt_os_thread_t thread, void **thread_return)
 
   CloseHandle(thread->handle);
 
-  if (thread->name)
-    free(thread->name);
+  free(thread->name);
 
   free(thread);
 }
@@ -726,6 +746,21 @@ void xbt_os_thread_exit(int *retval)
     ExitThread(0);
 }
 
+void xbt_os_thread_key_create(xbt_os_thread_key_t* key) {
+
+  *key = TlsAlloc();
+}
+
+void xbt_os_thread_set_specific(xbt_os_thread_key_t key, void* value) {
+
+  if (!TlsSetValue(key, value))
+    THROWF(system_error, (int) GetLastError(), "TlsSetValue() failed");
+}
+
+void* xbt_os_thread_get_specific(xbt_os_thread_key_t key) {
+  return TlsGetValue(key);
+}
+
 void xbt_os_thread_detach(xbt_os_thread_t thread)
 {
   THROW_UNIMPLEMENTED;
@@ -1147,7 +1182,7 @@ void *xbt_os_thread_get_extra_data(void)
 
 xbt_os_rmutex_t xbt_os_rmutex_init(void)
 {
-  xbt_os_rmutex_t rmutex = xbt_new0(struct xbt_os_rmutex_, 0);
+  xbt_os_rmutex_t rmutex = xbt_new0(struct xbt_os_rmutex_, 1);
   rmutex->mutex = xbt_os_mutex_init();
   rmutex->owner = NULL;
   rmutex->count = 0;