Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove useless wrapper around pthread_atfork().
[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 #include <limits.h>
19 #include <semaphore.h>
20 #include <errno.h>
21
22 #if defined(_WIN32)
23 #include <windows.h>
24 #elif defined(__MACH__) && defined(__APPLE__)
25 #include <stdint.h>
26 #include <sys/types.h>
27 #include <sys/sysctl.h>
28 #else
29 #include <unistd.h>
30 #endif
31
32 #include "xbt/sysdep.h"
33 #include "xbt/ex.h"
34 #include "src/internal_config.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 */
38
39 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(xbt_sync_os, xbt, "Synchronization mechanism (OS-level)");
40
41 typedef struct xbt_os_thread_ {
42   pthread_t t;
43   void *param;
44   pvoid_f_pvoid_t start_routine;
45 } s_xbt_os_thread_t;
46
47 /****** mutex related functions ******/
48 typedef struct xbt_os_mutex_ {
49   pthread_mutex_t m;
50 } s_xbt_os_mutex_t;
51
52 #include <time.h>
53 #include <math.h>
54
55 xbt_os_mutex_t xbt_os_mutex_init(void)
56 {
57   pthread_mutexattr_t Attr;
58   pthread_mutexattr_init(&Attr);
59   pthread_mutexattr_settype(&Attr, PTHREAD_MUTEX_RECURSIVE);
60
61   xbt_os_mutex_t res = xbt_new(s_xbt_os_mutex_t, 1);
62   int errcode = pthread_mutex_init(&(res->m), &Attr);
63   xbt_assert(errcode==0, "pthread_mutex_init() failed: %s", strerror(errcode));
64
65   return res;
66 }
67
68 void xbt_os_mutex_acquire(xbt_os_mutex_t mutex)
69 {
70   int errcode = pthread_mutex_lock(&(mutex->m));
71   xbt_assert(errcode==0, "pthread_mutex_lock(%p) failed: %s", mutex, strerror(errcode));
72 }
73
74 void xbt_os_mutex_release(xbt_os_mutex_t mutex)
75 {
76   int errcode = pthread_mutex_unlock(&(mutex->m));
77   xbt_assert(errcode==0, "pthread_mutex_unlock(%p) failed: %s", mutex, strerror(errcode));
78 }
79
80 void xbt_os_mutex_destroy(xbt_os_mutex_t mutex)
81 {
82   if (!mutex)
83     return;
84
85   int errcode = pthread_mutex_destroy(&(mutex->m));
86   xbt_assert(errcode == 0, "pthread_mutex_destroy(%p) failed: %s", mutex, strerror(errcode));
87   free(mutex);
88 }