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-2018. The SimGrid Team. All rights reserved. */
7 /* This program is free software; you can redistribute it and/or modify it
8 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include "src/internal_config.h"
11 #if HAVE_PTHREAD_SETAFFINITY
18 #if defined(__FreeBSD__)
19 #include "pthread_np.h"
20 #define cpu_set_t cpuset_t
24 #include <semaphore.h>
29 #elif defined(__MACH__) && defined(__APPLE__)
31 #include <sys/types.h>
32 #include <sys/sysctl.h>
37 #include "xbt/sysdep.h"
39 #include "src/internal_config.h"
40 #include "xbt/xbt_os_time.h" /* Portable time facilities */
41 #include "xbt/xbt_os_thread.h" /* This module */
42 #include "src/xbt_modinter.h" /* Initialization/finalization of this module */
44 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_sync_os, xbt, "Synchronization mechanism (OS-level)");
46 typedef struct xbt_os_thread_ {
50 pvoid_f_pvoid_t start_routine;
52 static xbt_os_thread_t main_thread = NULL;
54 /* thread-specific data containing the xbt_os_thread_t structure */
55 static pthread_key_t xbt_self_thread_key;
56 static int thread_mod_inited = 0;
58 /* defaults attribute for pthreads */
59 //FIXME: find where to put this
60 static pthread_attr_t thread_attr;
62 /* frees the xbt_os_thread_t corresponding to the current thread */
63 static void xbt_os_thread_free_thread_data(xbt_os_thread_t thread)
65 if (thread == main_thread) /* just killed main thread */
71 void xbt_os_thread_mod_preinit(void)
73 if (thread_mod_inited)
76 int errcode = pthread_key_create(&xbt_self_thread_key, NULL);
77 xbt_assert(errcode == 0, "pthread_key_create failed for xbt_self_thread_key");
79 main_thread = xbt_new(s_xbt_os_thread_t, 1);
80 main_thread->name = NULL;
81 main_thread->name = xbt_strdup("main");
82 main_thread->param = NULL;
83 main_thread->start_routine = NULL;
85 if ((errcode = pthread_setspecific(xbt_self_thread_key, main_thread)))
86 THROWF(system_error, errcode,
87 "Impossible to set the SimGrid identity descriptor to the main thread (pthread_setspecific failed)");
89 pthread_attr_init(&thread_attr);
91 thread_mod_inited = 1;
94 void xbt_os_thread_mod_postexit(void)
96 /* FIXME: don't try to free our key on shutdown.
97 Valgrind detects no leak if we don't, and whine if we try to */
100 // if ((errcode=pthread_key_delete(xbt_self_thread_key)))
101 // THROWF(system_error,errcode,"pthread_key_delete failed for xbt_self_thread_key");
102 free(main_thread->name);
105 thread_mod_inited = 0;
108 /** Calls pthread_atfork() if present, and raise an exception otherwise.
110 * The only known user of this wrapper is mmalloc_preinit(), but it is absolutely mandatory there:
111 * when used with tesh, mmalloc *must* be mutex protected and resistant to forks.
112 * This functionality is the only way to get it working (by ensuring that the mutex is consistently released on forks)
115 /* this function is critical to tesh+mmalloc, don't mess with it */
116 int xbt_os_thread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void))
118 return pthread_atfork(prepare, parent, child);
121 static void *wrapper_start_routine(void *s)
123 xbt_os_thread_t t = s;
125 int errcode = pthread_setspecific(xbt_self_thread_key, t);
126 xbt_assert(errcode == 0, "pthread_setspecific failed for xbt_self_thread_key");
128 return t->start_routine(t->param);
131 xbt_os_thread_t xbt_os_thread_create(const char* name, pvoid_f_pvoid_t start_routine, void* param)
133 xbt_os_thread_t res_thread = xbt_new(s_xbt_os_thread_t, 1);
134 res_thread->name = xbt_strdup(name);
135 res_thread->start_routine = start_routine;
136 res_thread->param = param;
138 int errcode = pthread_create(&(res_thread->t), &thread_attr, wrapper_start_routine, res_thread);
139 xbt_assert(errcode == 0, "pthread_create failed: %s", strerror(errcode));
144 /** Bind the thread to the given core, if possible.
146 * If pthread_setaffinity_np is not usable on that (non-gnu) platform, this function does nothing.
148 int xbt_os_thread_bind(XBT_ATTRIB_UNUSED xbt_os_thread_t thread, XBT_ATTRIB_UNUSED int cpu)
151 #if HAVE_PTHREAD_SETAFFINITY
152 pthread_t pthread = thread->t;
155 CPU_SET(cpu, &cpuset);
156 errcode = pthread_setaffinity_np(pthread, sizeof(cpu_set_t), &cpuset);
161 void xbt_os_thread_setstacksize(int stack_size)
163 size_t alignment[] = {
165 #ifdef PTHREAD_STACK_MIN
171 xbt_assert(stack_size >= 0, "stack size %d is negative, maybe it exceeds MAX_INT?", stack_size);
173 size_t sz = stack_size;
174 int res = pthread_attr_setstacksize(&thread_attr, sz);
176 for (int i = 0; res == EINVAL && alignment[i] > 0; i++) {
177 /* Invalid size, try again with next multiple of alignment[i]. */
178 size_t rem = sz % alignment[i];
179 if (rem != 0 || sz == 0) {
180 size_t sz2 = sz - rem + alignment[i];
181 XBT_DEBUG("pthread_attr_setstacksize failed for %zu, try again with %zu", sz, sz2);
183 res = pthread_attr_setstacksize(&thread_attr, sz);
188 XBT_WARN("invalid stack size (maybe too big): %zu", sz);
190 XBT_WARN("unknown error %d in pthread stacksize setting: %zu", res, sz);
193 void xbt_os_thread_setguardsize(int guard_size)
196 THROW_UNIMPLEMENTED; //pthread_attr_setguardsize is not implemented in pthread.h on windows
198 size_t sz = guard_size;
199 int res = pthread_attr_setguardsize(&thread_attr, sz);
201 XBT_WARN("pthread_attr_setguardsize failed (%d) for size: %zu", res, sz);
205 const char *xbt_os_thread_self_name(void)
207 xbt_os_thread_t me = xbt_os_thread_self();
208 return me ? (const char *)me->name : "main";
211 void xbt_os_thread_join(xbt_os_thread_t thread, void **thread_return)
213 int errcode = pthread_join(thread->t, thread_return);
215 xbt_assert(errcode==0, "pthread_join failed: %s", strerror(errcode));
216 xbt_os_thread_free_thread_data(thread);
219 void xbt_os_thread_exit(int *retval)
221 pthread_exit(retval);
224 xbt_os_thread_t xbt_os_thread_self(void )
226 if (!thread_mod_inited)
229 return pthread_getspecific(xbt_self_thread_key);
232 /****** mutex related functions ******/
233 typedef struct xbt_os_mutex_ {
240 xbt_os_mutex_t xbt_os_mutex_init(void)
242 pthread_mutexattr_t Attr;
243 pthread_mutexattr_init(&Attr);
244 pthread_mutexattr_settype(&Attr, PTHREAD_MUTEX_RECURSIVE);
246 xbt_os_mutex_t res = xbt_new(s_xbt_os_mutex_t, 1);
247 int errcode = pthread_mutex_init(&(res->m), &Attr);
248 xbt_assert(errcode==0, "pthread_mutex_init() failed: %s", strerror(errcode));
253 void xbt_os_mutex_acquire(xbt_os_mutex_t mutex)
255 int errcode = pthread_mutex_lock(&(mutex->m));
256 xbt_assert(errcode==0, "pthread_mutex_lock(%p) failed: %s", mutex, strerror(errcode));
259 void xbt_os_mutex_release(xbt_os_mutex_t mutex)
261 int errcode = pthread_mutex_unlock(&(mutex->m));
262 xbt_assert(errcode==0, "pthread_mutex_unlock(%p) failed: %s", mutex, strerror(errcode));
265 void xbt_os_mutex_destroy(xbt_os_mutex_t mutex)
270 int errcode = pthread_mutex_destroy(&(mutex->m));
271 xbt_assert(errcode == 0, "pthread_mutex_destroy(%p) failed: %s", mutex, strerror(errcode));