+
+
+/** @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 defined(__APPLE__) && defined(__MACH__)
+ 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;
+ xbt_os_thread_t owner;
+ int count;
+} s_xbt_os_rmutex_t;
+
+void xbt_os_thread_set_extra_data(void *data)
+{
+ xbt_os_thread_self()->extra_data = data;
+}
+
+void *xbt_os_thread_get_extra_data(void)
+{
+ xbt_os_thread_t self = xbt_os_thread_self();
+ return self? self->extra_data : NULL;
+}
+
+xbt_os_rmutex_t xbt_os_rmutex_init(void)
+{
+ xbt_os_rmutex_t rmutex = xbt_new0(struct xbt_os_rmutex_, 1);
+ rmutex->mutex = xbt_os_mutex_init();
+ rmutex->owner = NULL;
+ rmutex->count = 0;
+ return rmutex;
+}
+
+void xbt_os_rmutex_acquire(xbt_os_rmutex_t rmutex)
+{
+ xbt_os_thread_t self = xbt_os_thread_self();
+
+ if (self == NULL) {
+ /* the thread module is not initialized yet */
+ rmutex->owner = NULL;
+ return;
+ }
+
+ if (self != rmutex->owner) {
+ xbt_os_mutex_acquire(rmutex->mutex);
+ rmutex->owner = self;
+ rmutex->count = 1;
+ } else {
+ rmutex->count++;
+ }
+}
+
+void xbt_os_rmutex_release(xbt_os_rmutex_t rmutex)
+{
+ if (rmutex->owner == NULL) {
+ /* the thread module was not initialized */
+ return;
+ }
+
+ xbt_assert(rmutex->owner == xbt_os_thread_self());
+
+ if (--rmutex->count == 0) {
+ rmutex->owner = NULL;
+ xbt_os_mutex_release(rmutex->mutex);
+ }
+}
+
+void xbt_os_rmutex_destroy(xbt_os_rmutex_t rmutex)
+{
+ xbt_os_mutex_destroy(rmutex->mutex);
+ xbt_free(rmutex);
+}