Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Implement thread factory with std::thread, and cleanups
[simgrid.git] / src / xbt / xbt_os_thread.c
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   */
4
5 /* Copyright (c) 2007-2019. The SimGrid Team. All rights reserved.          */
6
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. */
9
10 #include "src/internal_config.h"
11 #if HAVE_PTHREAD_SETAFFINITY
12 #define _GNU_SOURCE
13 #include <sched.h>
14 #endif
15
16 #include <pthread.h>
17
18 #if defined(__FreeBSD__)
19 #include "pthread_np.h"
20 #define cpu_set_t cpuset_t
21 #endif
22
23 #include <limits.h>
24 #include <semaphore.h>
25 #include <errno.h>
26
27 #if defined(_WIN32)
28 #include <windows.h>
29 #elif defined(__MACH__) && defined(__APPLE__)
30 #include <stdint.h>
31 #include <sys/types.h>
32 #include <sys/sysctl.h>
33 #else
34 #include <unistd.h>
35 #endif
36
37 #include "xbt/sysdep.h"
38 #include "xbt/ex.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 */
43
44 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_sync_os, xbt, "Synchronization mechanism (OS-level)");
45
46 typedef struct xbt_os_thread_ {
47   pthread_t t;
48   void *param;
49   pvoid_f_pvoid_t start_routine;
50 } s_xbt_os_thread_t;
51 static xbt_os_thread_t main_thread = NULL;
52
53 /* thread-specific data containing the xbt_os_thread_t structure */
54 static int thread_mod_inited = 0;
55
56 /* frees the xbt_os_thread_t corresponding to the current thread */
57 static void xbt_os_thread_free_thread_data(xbt_os_thread_t thread)
58 {
59   if (thread == main_thread)    /* just killed main thread */
60     main_thread = NULL;
61   free(thread);
62 }
63
64 void xbt_os_thread_mod_preinit(void)
65 {
66   if (thread_mod_inited)
67     return;
68
69   main_thread = xbt_new(s_xbt_os_thread_t, 1);
70   main_thread->param = NULL;
71   main_thread->start_routine = NULL;
72
73   thread_mod_inited = 1;
74 }
75
76 void xbt_os_thread_mod_postexit(void)
77 {
78   free(main_thread);
79   main_thread = NULL;
80   thread_mod_inited = 0;
81 }
82
83 /** Calls pthread_atfork() if present, and raise an exception otherwise.
84  *
85  * The only known user of this wrapper is mmalloc_preinit(), but it is absolutely mandatory there:
86  * when used with tesh, mmalloc *must* be mutex protected and resistant to forks.
87  * This functionality is the only way to get it working (by ensuring that the mutex is consistently released on forks)
88  */
89
90 /* this function is critical to tesh+mmalloc, don't mess with it */
91 int xbt_os_thread_atfork(void (*prepare)(void), void (*parent)(void), void (*child)(void))
92 {
93   return pthread_atfork(prepare, parent, child);
94 }
95
96 static void *wrapper_start_routine(void *s)
97 {
98   xbt_os_thread_t t = s;
99
100   return t->start_routine(t->param);
101 }
102
103 xbt_os_thread_t xbt_os_thread_create(pvoid_f_pvoid_t start_routine, void* param)
104 {
105   xbt_os_thread_t res_thread = xbt_new(s_xbt_os_thread_t, 1);
106   res_thread->start_routine = start_routine;
107   res_thread->param = param;
108
109   int errcode = pthread_create(&(res_thread->t), NULL, wrapper_start_routine, res_thread);
110   xbt_assert(errcode == 0, "pthread_create failed: %s", strerror(errcode));
111
112   return res_thread;
113 }
114
115 /** Bind the thread to the given core, if possible.
116  *
117  * If pthread_setaffinity_np is not usable on that (non-gnu) platform, this function does nothing.
118  */
119 int xbt_os_thread_bind(XBT_ATTRIB_UNUSED xbt_os_thread_t thread, XBT_ATTRIB_UNUSED int cpu)
120 {
121   int errcode = 0;
122 #if HAVE_PTHREAD_SETAFFINITY
123   pthread_t pthread = thread->t;
124   cpu_set_t cpuset;
125   CPU_ZERO(&cpuset);
126   CPU_SET(cpu, &cpuset);
127   errcode = pthread_setaffinity_np(pthread, sizeof(cpu_set_t), &cpuset);
128 #endif
129   return errcode;
130 }
131
132 void xbt_os_thread_join(xbt_os_thread_t thread, void **thread_return)
133 {
134   int errcode = pthread_join(thread->t, thread_return);
135
136   xbt_assert(errcode==0, "pthread_join failed: %s", strerror(errcode));
137   xbt_os_thread_free_thread_data(thread);
138 }
139
140 /****** mutex related functions ******/
141 typedef struct xbt_os_mutex_ {
142   pthread_mutex_t m;
143 } s_xbt_os_mutex_t;
144
145 #include <time.h>
146 #include <math.h>
147
148 xbt_os_mutex_t xbt_os_mutex_init(void)
149 {
150   pthread_mutexattr_t Attr;
151   pthread_mutexattr_init(&Attr);
152   pthread_mutexattr_settype(&Attr, PTHREAD_MUTEX_RECURSIVE);
153
154   xbt_os_mutex_t res = xbt_new(s_xbt_os_mutex_t, 1);
155   int errcode = pthread_mutex_init(&(res->m), &Attr);
156   xbt_assert(errcode==0, "pthread_mutex_init() failed: %s", strerror(errcode));
157
158   return res;
159 }
160
161 void xbt_os_mutex_acquire(xbt_os_mutex_t mutex)
162 {
163   int errcode = pthread_mutex_lock(&(mutex->m));
164   xbt_assert(errcode==0, "pthread_mutex_lock(%p) failed: %s", mutex, strerror(errcode));
165 }
166
167 void xbt_os_mutex_release(xbt_os_mutex_t mutex)
168 {
169   int errcode = pthread_mutex_unlock(&(mutex->m));
170   xbt_assert(errcode==0, "pthread_mutex_unlock(%p) failed: %s", mutex, strerror(errcode));
171 }
172
173 void xbt_os_mutex_destroy(xbt_os_mutex_t mutex)
174 {
175   if (!mutex)
176     return;
177
178   int errcode = pthread_mutex_destroy(&(mutex->m));
179   xbt_assert(errcode == 0, "pthread_mutex_destroy(%p) failed: %s", mutex, strerror(errcode));
180   free(mutex);
181 }