Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
document that we really need xbt_os_thread_atfork (for tesh+mmalloc)
[simgrid.git] / src / xbt / xbt_os_thread.c
index e8c86e3..08e82d5 100644 (file)
@@ -74,8 +74,7 @@ static xbt_running_ctx_t *_os_thread_get_running_ctx(void)
 static void _os_thread_ex_terminate(xbt_ex_t * e)
 {
   xbt_ex_display(e);
-
-  abort();
+  xbt_abort();
   /* FIXME: there should be a configuration variable to choose to kill everyone or only this one */
 }
 
@@ -134,10 +133,15 @@ void xbt_os_thread_mod_postexit(void)
   __xbt_ex_terminate = &__xbt_ex_terminate_default;
 }
 
+/* this function is critical to tesh+mmalloc, don't mess with it */
 int xbt_os_thread_atfork(void (*prepare)(void),
                          void (*parent)(void), void (*child)(void))
 {
+#ifdef WIN32
+  THROW_UNIMPLEMENTED; //pthread_atfork is not implemented in pthread.h on windows
+#else
   return pthread_atfork(prepare, parent, child);
+#endif
 }
 
 static void *wrapper_start_routine(void *s)
@@ -149,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;
@@ -495,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));
@@ -638,7 +642,7 @@ typedef struct xbt_os_thread_ {
 #endif
 
 /* the default size of the stack of the threads (in bytes)*/
-#define XBT_DEFAULT_THREAD_STACK_SIZE  4096
+#define XBT_DEFAULT_THREAD_STACK_SIZE  4096
 
 /* key to the TLS containing the xbt_os_thread_t structure */
 static unsigned long xbt_self_thread_key;
@@ -729,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);
 }
@@ -1160,6 +1163,33 @@ void xbt_os_sem_get_value(xbt_os_sem_t sem, int *svalue)
 
 #endif
 
+
+/** @brief Returns the amount of cores on the current host */
+int xbt_os_get_numcores(void) {
+#ifdef WIN32
+    SYSTEM_INFO sysinfo;
+    GetSystemInfo(&sysinfo);
+    return sysinfo.dwNumberOfProcessors;
+#elif MACOS
+    int nm[2];
+    size_t len = 4;
+    uint32_t count;
+
+    nm[0] = CTL_HW; nm[1] = HW_AVAILCPU;
+    sysctl(nm, 2, &count, &len, NULL, 0);
+
+    if(count < 1) {
+        nm[1] = HW_NCPU;
+        sysctl(nm, 2, &count, &len, NULL, 0);
+        if(count < 1) { count = 1; }
+    }
+    return count;
+#else
+    return sysconf(_SC_NPROCESSORS_ONLN);
+#endif
+}
+
+
 /***** reentrant mutexes *****/
 typedef struct xbt_os_rmutex_ {
   xbt_os_mutex_t mutex;
@@ -1179,7 +1209,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;