1 /* xbt_os_thread -- portability layer over the pthread API */
2 /* Used in RL to get win/lin portability, and in SG when CONTEXT_THREAD */
3 /* in SG, when using HAVE_UCONTEXT_CONTEXTS, xbt_os_thread_stub is used instead */
5 /* Copyright (c) 2007-2015. The SimGrid Team.
6 * All rights reserved. */
8 /* This program is free software; you can redistribute it and/or modify it
9 * under the terms of the license (GNU LGPL) which comes with this package. */
11 #include "src/internal_config.h"
12 #ifdef HAVE_PTHREAD_SETAFFINITY
19 #include <semaphore.h>
23 #elif defined(__MACH__) && defined(__APPLE__)
25 #include <sys/types.h>
26 #include <sys/sysctl.h>
31 #include "xbt/sysdep.h"
33 #include "src/xbt/ex_interface.h" /* We play crude games with exceptions */
34 #include "src/portable.h"
35 #include "xbt/xbt_os_time.h" /* Portable time facilities */
36 #include "xbt/xbt_os_thread.h" /* This module */
37 #include "src/xbt_modinter.h" /* Initialization/finalization of this module */
39 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_sync_os, xbt, "Synchronization mechanism (OS-level)");
42 /* use named sempahore when sem_init() does not work */
44 static int next_sem_ID = 0;
45 static xbt_os_mutex_t next_sem_ID_lock;
48 typedef struct xbt_os_thread_ {
53 pvoid_f_pvoid_t start_routine;
54 xbt_running_ctx_t *running_ctx;
57 static xbt_os_thread_t main_thread = NULL;
59 /* thread-specific data containing the xbt_os_thread_t structure */
60 static pthread_key_t xbt_self_thread_key;
61 static int thread_mod_inited = 0;
63 /* defaults attribute for pthreads */
64 //FIXME: find where to put this
65 static pthread_attr_t thread_attr;
67 /* frees the xbt_os_thread_t corresponding to the current thread */
68 static void xbt_os_thread_free_thread_data(xbt_os_thread_t thread)
70 if (thread == main_thread) /* just killed main thread */
73 free(thread->running_ctx);
78 /* callback: context fetching */
79 static xbt_running_ctx_t *_os_thread_get_running_ctx(void)
81 return xbt_os_thread_self()->running_ctx;
84 /* callback: termination */
85 static void _os_thread_ex_terminate(xbt_ex_t * e)
89 /* FIXME: there should be a configuration variable to choose to kill everyone or only this one */
92 void xbt_os_thread_mod_preinit(void)
94 if (thread_mod_inited)
97 int errcode = pthread_key_create(&xbt_self_thread_key, NULL);
98 xbt_assert(errcode == 0, "pthread_key_create failed for xbt_self_thread_key");
100 main_thread = xbt_new(s_xbt_os_thread_t, 1);
101 main_thread->name = NULL;
102 main_thread->detached = 0;
103 main_thread->name = (char *) "main";
104 main_thread->param = NULL;
105 main_thread->start_routine = NULL;
106 main_thread->running_ctx = xbt_new(xbt_running_ctx_t, 1);
107 main_thread->extra_data = NULL;
108 XBT_RUNNING_CTX_INITIALIZE(main_thread->running_ctx);
110 if ((errcode = pthread_setspecific(xbt_self_thread_key, main_thread)))
111 THROWF(system_error, errcode,
112 "Impossible to set the SimGrid identity descriptor to the main thread (pthread_setspecific failed)");
114 __xbt_running_ctx_fetch = _os_thread_get_running_ctx;
115 __xbt_ex_terminate = _os_thread_ex_terminate;
117 pthread_attr_init(&thread_attr);
119 thread_mod_inited = 1;
121 #ifndef HAVE_SEM_INIT
122 next_sem_ID_lock = xbt_os_mutex_init();
126 void xbt_os_thread_mod_postexit(void)
128 /* FIXME: don't try to free our key on shutdown.
129 Valgrind detects no leak if we don't, and whine if we try to */
132 // if ((errcode=pthread_key_delete(xbt_self_thread_key)))
133 // THROWF(system_error,errcode,"pthread_key_delete failed for xbt_self_thread_key");
134 free(main_thread->running_ctx);
137 thread_mod_inited = 0;
138 #ifndef HAVE_SEM_INIT
139 xbt_os_mutex_destroy(next_sem_ID_lock);
142 /* Restore the default exception setup */
143 __xbt_running_ctx_fetch = &__xbt_ex_ctx_default;
144 __xbt_ex_terminate = &__xbt_ex_terminate_default;
147 /* this function is critical to tesh+mmalloc, don't mess with it */
148 int xbt_os_thread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void))
150 return pthread_atfork(prepare, parent, child);
153 static void *wrapper_start_routine(void *s)
155 xbt_os_thread_t t = s;
157 int errcode = pthread_setspecific(xbt_self_thread_key, t);
158 xbt_assert(errcode == 0, "pthread_setspecific failed for xbt_self_thread_key");
160 void *res = t->start_routine(t->param);
162 xbt_os_thread_free_thread_data(t);
167 xbt_os_thread_t xbt_os_thread_create(const char *name, pvoid_f_pvoid_t start_routine, void *param, void *extra_data)
169 xbt_os_thread_t res_thread = xbt_new(s_xbt_os_thread_t, 1);
170 res_thread->detached = 0;
171 res_thread->name = xbt_strdup(name);
172 res_thread->start_routine = start_routine;
173 res_thread->param = param;
174 res_thread->running_ctx = xbt_new(xbt_running_ctx_t, 1);
175 XBT_RUNNING_CTX_INITIALIZE(res_thread->running_ctx);
176 res_thread->extra_data = extra_data;
178 int errcode = pthread_create(&(res_thread->t), &thread_attr, wrapper_start_routine, res_thread);
179 xbt_assert(errcode == 0, "pthread_create failed: %s", strerror(errcode));
184 int xbt_os_thread_bind(xbt_os_thread_t thread, int cpu){
186 #ifdef HAVE_PTHREAD_SETAFFINITY
187 pthread_t pthread = thread->t;
190 CPU_SET(cpu, &cpuset);
191 errcode = pthread_setaffinity_np(pthread, sizeof(cpu_set_t), &cpuset);
196 void xbt_os_thread_setstacksize(int stack_size)
198 size_t alignment[] = {
200 #ifdef PTHREAD_STACK_MIN
206 xbt_assert(stack_size >= 0, "stack size %d is negative, maybe it exceeds MAX_INT?", stack_size);
208 size_t sz = stack_size;
209 int res = pthread_attr_setstacksize(&thread_attr, sz);
211 for (int i = 0; res == EINVAL && alignment[i] > 0; i++) {
212 /* Invalid size, try again with next multiple of alignment[i]. */
213 size_t rem = sz % alignment[i];
214 if (rem != 0 || sz == 0) {
215 size_t sz2 = sz - rem + alignment[i];
216 XBT_DEBUG("pthread_attr_setstacksize failed for %zd, try again with %zd", sz, sz2);
218 res = pthread_attr_setstacksize(&thread_attr, sz);
223 XBT_WARN("invalid stack size (maybe too big): %zd", sz);
225 XBT_WARN("unknown error %d in pthread stacksize setting: %zd", res, sz);
228 void xbt_os_thread_setguardsize(int guard_size)
231 THROW_UNIMPLEMENTED; //pthread_attr_setguardsize is not implemented in pthread.h on windows
233 size_t sz = guard_size;
234 int res = pthread_attr_setguardsize(&thread_attr, sz);
236 XBT_WARN("pthread_attr_setguardsize failed (%d) for size: %zd", res, sz);
240 const char *xbt_os_thread_name(xbt_os_thread_t t)
245 const char *xbt_os_thread_self_name(void)
247 xbt_os_thread_t me = xbt_os_thread_self();
248 return me ? me->name : "main";
251 void xbt_os_thread_join(xbt_os_thread_t thread, void **thread_return)
253 int errcode = pthread_join(thread->t, thread_return);
255 xbt_assert(errcode==0, "pthread_join failed: %s", strerror(errcode));
256 xbt_os_thread_free_thread_data(thread);
259 void xbt_os_thread_exit(int *retval)
261 pthread_exit(retval);
264 xbt_os_thread_t xbt_os_thread_self(void)
266 if (!thread_mod_inited)
269 return pthread_getspecific(xbt_self_thread_key);
272 void xbt_os_thread_key_create(xbt_os_thread_key_t* key)
274 int errcode = pthread_key_create(key, NULL);
275 xbt_assert(errcode==0 , "pthread_key_create failed");
278 void xbt_os_thread_set_specific(xbt_os_thread_key_t key, void* value) {
280 int errcode = pthread_setspecific(key, value);
281 xbt_assert(errcode==0, "pthread_setspecific failed");
284 void* xbt_os_thread_get_specific(xbt_os_thread_key_t key) {
285 return pthread_getspecific(key);
288 void xbt_os_thread_detach(xbt_os_thread_t thread)
290 thread->detached = 1;
291 pthread_detach(thread->t);
295 void xbt_os_thread_yield(void)
300 void xbt_os_thread_cancel(xbt_os_thread_t t)
302 pthread_cancel(t->t);
305 /****** mutex related functions ******/
306 typedef struct xbt_os_mutex_ {
307 /* KEEP IT IN SYNC WITH xbt_thread.c */
314 xbt_os_mutex_t xbt_os_mutex_init(void)
316 xbt_os_mutex_t res = xbt_new(s_xbt_os_mutex_t, 1);
317 int errcode = pthread_mutex_init(&(res->m), NULL);
318 xbt_assert(errcode==0, "pthread_mutex_init() failed: %s", strerror(errcode));
323 void xbt_os_mutex_acquire(xbt_os_mutex_t mutex)
325 int errcode = pthread_mutex_lock(&(mutex->m));
326 xbt_assert(errcode==0, "pthread_mutex_lock(%p) failed: %s", mutex, strerror(errcode));
330 void xbt_os_mutex_timedacquire(xbt_os_mutex_t mutex, double delay)
335 xbt_os_mutex_acquire(mutex);
337 } else if (delay == 0) {
338 errcode = pthread_mutex_trylock(&(mutex->m));
344 THROWF(timeout_error, 0, "mutex %p not ready", mutex);
346 THROWF(system_error, errcode,
347 "xbt_os_mutex_timedacquire(%p) failed: %s", mutex,
354 #ifdef HAVE_MUTEX_TIMEDLOCK
355 struct timespec ts_end;
356 double end = delay + xbt_os_time();
358 ts_end.tv_sec = (time_t) floor(end);
359 ts_end.tv_nsec = (long) ((end - ts_end.tv_sec) * 1000000000);
360 XBT_DEBUG("pthread_mutex_timedlock(%p,%p)", &(mutex->m), &ts_end);
362 int errcode = pthread_mutex_timedlock(&(mutex->m), &ts_end);
364 #else /* reimplement it since those lazy libc dudes didn't (Mac OSX, hu?) */
365 double start = xbt_os_time();
367 errcode = pthread_mutex_trylock(&(mutex->m));
368 if (errcode == EBUSY)
369 xbt_os_thread_yield();
370 } while (errcode == EBUSY && xbt_os_time() - start < delay);
372 if (errcode == EBUSY)
375 #endif /* HAVE_MUTEX_TIMEDLOCK */
382 THROWF(timeout_error, delay,
383 "mutex %p wasn't signaled before timeout (%f)", mutex, delay);
386 THROWF(system_error, errcode,
387 "pthread_mutex_timedlock(%p,%f) failed: %s", mutex, delay,
393 void xbt_os_mutex_release(xbt_os_mutex_t mutex)
395 int errcode = pthread_mutex_unlock(&(mutex->m));
396 xbt_assert(errcode==0, "pthread_mutex_unlock(%p) failed: %s", mutex, strerror(errcode));
399 void xbt_os_mutex_destroy(xbt_os_mutex_t mutex)
404 int errcode = pthread_mutex_destroy(&(mutex->m));
405 xbt_assert(errcode == 0, "pthread_mutex_destroy(%p) failed: %s", mutex, strerror(errcode));
409 /***** condition related functions *****/
410 typedef struct xbt_os_cond_ {
411 /* KEEP IT IN SYNC WITH xbt_thread.c */
415 xbt_os_cond_t xbt_os_cond_init(void)
417 xbt_os_cond_t res = xbt_new(s_xbt_os_cond_t, 1);
418 int errcode = pthread_cond_init(&(res->c), NULL);
419 xbt_assert(errcode==0, "pthread_cond_init() failed: %s", strerror(errcode));
423 void xbt_os_cond_wait(xbt_os_cond_t cond, xbt_os_mutex_t mutex)
425 int errcode = pthread_cond_wait(&(cond->c), &(mutex->m));
426 xbt_assert(errcode==0, "pthread_cond_wait(%p,%p) failed: %s", cond, mutex, strerror(errcode));
430 void xbt_os_cond_timedwait(xbt_os_cond_t cond, xbt_os_mutex_t mutex, double delay)
433 struct timespec ts_end;
434 double end = delay + xbt_os_time();
437 xbt_os_cond_wait(cond, mutex);
439 ts_end.tv_sec = (time_t) floor(end);
440 ts_end.tv_nsec = (long) ((end - ts_end.tv_sec) * 1000000000);
441 XBT_DEBUG("pthread_cond_timedwait(%p,%p,%p)", &(cond->c), &(mutex->m), &ts_end);
442 switch ((errcode = pthread_cond_timedwait(&(cond->c), &(mutex->m), &ts_end))) {
446 THROWF(timeout_error, errcode,
447 "condition %p (mutex %p) wasn't signaled before timeout (%f)",
450 THROWF(system_error, errcode, "pthread_cond_timedwait(%p,%p,%f) failed: %s", cond, mutex, delay, strerror(errcode));
455 void xbt_os_cond_signal(xbt_os_cond_t cond)
457 int errcode = pthread_cond_signal(&(cond->c));
458 xbt_assert(errcode==0, "pthread_cond_signal(%p) failed: %s", cond, strerror(errcode));
461 void xbt_os_cond_broadcast(xbt_os_cond_t cond)
463 int errcode = pthread_cond_broadcast(&(cond->c));
464 xbt_assert(errcode==0, "pthread_cond_broadcast(%p) failed: %s", cond, strerror(errcode));
467 void xbt_os_cond_destroy(xbt_os_cond_t cond)
472 int errcode = pthread_cond_destroy(&(cond->c));
473 xbt_assert(errcode==0, "pthread_cond_destroy(%p) failed: %s", cond, strerror(errcode));
477 typedef struct xbt_os_sem_ {
478 #ifndef HAVE_SEM_INIT
486 #define SEM_FAILED (-1)
489 xbt_os_sem_t xbt_os_sem_init(unsigned int value)
491 xbt_os_sem_t res = xbt_new(s_xbt_os_sem_t, 1);
493 /* On some systems (MAC OS X), only the stub of sem_init is to be found.
494 * Any attempt to use it leads to ENOSYS (function not implemented).
495 * If such a prehistoric system is detected, do the job with sem_open instead
498 if (sem_init(&(res->s), 0, value) != 0)
499 THROWF(system_error, errno, "sem_init() failed: %s", strerror(errno));
502 #else /* damn, no sem_init(). Reimplement it */
504 xbt_os_mutex_acquire(next_sem_ID_lock);
505 res->name = bprintf("/%d", ++next_sem_ID);
506 xbt_os_mutex_release(next_sem_ID_lock);
508 res->ps = sem_open(res->name, O_CREAT, 0644, value);
509 if ((res->ps == (sem_t *) SEM_FAILED) && (errno == ENAMETOOLONG)) {
510 /* Old darwins only allow 13 chars. Did you create *that* amount of semaphores? */
511 res->name[13] = '\0';
512 res->ps = sem_open(res->name, O_CREAT, 0644, value);
514 if (res->ps == (sem_t *) SEM_FAILED)
515 THROWF(system_error, errno, "sem_open() failed: %s", strerror(errno));
517 /* Remove the name from the semaphore namespace: we never join on it */
518 if (sem_unlink(res->name) < 0)
519 THROWF(system_error, errno, "sem_unlink() failed: %s",
527 void xbt_os_sem_acquire(xbt_os_sem_t sem)
529 if (sem_wait(sem->ps) < 0)
530 THROWF(system_error, errno, "sem_wait() failed: %s", strerror(errno));
533 void xbt_os_sem_timedacquire(xbt_os_sem_t sem, double delay)
538 xbt_os_sem_acquire(sem);
539 } else if (delay == 0) {
540 errcode = sem_trywait(sem->ps);
546 THROWF(timeout_error, 0, "semaphore %p not ready", sem);
548 THROWF(system_error, errcode,
549 "xbt_os_sem_timedacquire(%p) failed: %s", sem,
555 struct timespec ts_end;
556 double end = delay + xbt_os_time();
558 ts_end.tv_sec = (time_t) floor(end);
559 ts_end.tv_nsec = (long) ((end - ts_end.tv_sec) * 1000000000);
560 XBT_DEBUG("sem_timedwait(%p,%p)", sem->ps, &ts_end);
561 errcode = sem_timedwait(sem->s, &ts_end);
563 #else /* Okay, reimplement this function then */
564 double start = xbt_os_time();
566 errcode = sem_trywait(sem->ps);
567 if (errcode == EBUSY)
568 xbt_os_thread_yield();
569 } while (errcode == EBUSY && xbt_os_time() - start < delay);
571 if (errcode == EBUSY)
580 THROWF(timeout_error, delay,
581 "semaphore %p wasn't signaled before timeout (%f)", sem,
585 THROWF(system_error, errcode, "sem_timedwait(%p,%f) failed: %s", sem,
586 delay, strerror(errcode));
591 void xbt_os_sem_release(xbt_os_sem_t sem)
593 if (sem_post(sem->ps) < 0)
594 THROWF(system_error, errno, "sem_post() failed: %s", strerror(errno));
597 void xbt_os_sem_destroy(xbt_os_sem_t sem)
600 if (sem_destroy(sem->ps) < 0)
601 THROWF(system_error, errno, "sem_destroy() failed: %s",
604 if (sem_close(sem->ps) < 0)
605 THROWF(system_error, errno, "sem_close() failed: %s", strerror(errno));
612 void xbt_os_sem_get_value(xbt_os_sem_t sem, int *svalue)
614 if (sem_getvalue(&(sem->s), svalue) < 0)
615 THROWF(system_error, errno, "sem_getvalue() failed: %s",
620 /** @brief Returns the amount of cores on the current host */
621 int xbt_os_get_numcores(void) {
624 GetSystemInfo(&sysinfo);
625 return sysinfo.dwNumberOfProcessors;
626 #elif defined(__APPLE__) && defined(__MACH__)
631 nm[0] = CTL_HW; nm[1] = HW_AVAILCPU;
632 sysctl(nm, 2, &count, &len, NULL, 0);
636 sysctl(nm, 2, &count, &len, NULL, 0);
637 if(count < 1) { count = 1; }
641 return sysconf(_SC_NPROCESSORS_ONLN);
646 /***** reentrant mutexes *****/
647 typedef struct xbt_os_rmutex_ {
648 xbt_os_mutex_t mutex;
649 xbt_os_thread_t owner;
653 void xbt_os_thread_set_extra_data(void *data)
655 xbt_os_thread_self()->extra_data = data;
658 void *xbt_os_thread_get_extra_data(void)
660 xbt_os_thread_t thread = xbt_os_thread_self();
662 return xbt_os_thread_self()->extra_data;
667 xbt_os_rmutex_t xbt_os_rmutex_init(void)
669 xbt_os_rmutex_t rmutex = xbt_new0(struct xbt_os_rmutex_, 1);
670 rmutex->mutex = xbt_os_mutex_init();
671 rmutex->owner = NULL;
676 void xbt_os_rmutex_acquire(xbt_os_rmutex_t rmutex)
678 xbt_os_thread_t self = xbt_os_thread_self();
681 /* the thread module is not initialized yet */
682 rmutex->owner = NULL;
686 if (self != rmutex->owner) {
687 xbt_os_mutex_acquire(rmutex->mutex);
688 rmutex->owner = self;
695 void xbt_os_rmutex_release(xbt_os_rmutex_t rmutex)
697 if (rmutex->owner == NULL) {
698 /* the thread module was not initialized */
702 xbt_assert(rmutex->owner == xbt_os_thread_self());
704 if (--rmutex->count == 0) {
705 rmutex->owner = NULL;
706 xbt_os_mutex_release(rmutex->mutex);
710 void xbt_os_rmutex_destroy(xbt_os_rmutex_t rmutex)
712 xbt_os_mutex_destroy(rmutex->mutex);