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-2019. 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 /****** mutex related functions ******/
47 typedef struct xbt_os_mutex_ {
54 xbt_os_mutex_t xbt_os_mutex_init(void)
56 pthread_mutexattr_t Attr;
57 pthread_mutexattr_init(&Attr);
58 pthread_mutexattr_settype(&Attr, PTHREAD_MUTEX_RECURSIVE);
60 xbt_os_mutex_t res = xbt_new(s_xbt_os_mutex_t, 1);
61 int errcode = pthread_mutex_init(&(res->m), &Attr);
62 xbt_assert(errcode==0, "pthread_mutex_init() failed: %s", strerror(errcode));
67 void xbt_os_mutex_acquire(xbt_os_mutex_t mutex)
69 int errcode = pthread_mutex_lock(&(mutex->m));
70 xbt_assert(errcode==0, "pthread_mutex_lock(%p) failed: %s", mutex, strerror(errcode));
73 void xbt_os_mutex_release(xbt_os_mutex_t mutex)
75 int errcode = pthread_mutex_unlock(&(mutex->m));
76 xbt_assert(errcode==0, "pthread_mutex_unlock(%p) failed: %s", mutex, strerror(errcode));
79 void xbt_os_mutex_destroy(xbt_os_mutex_t mutex)
84 int errcode = pthread_mutex_destroy(&(mutex->m));
85 xbt_assert(errcode == 0, "pthread_mutex_destroy(%p) failed: %s", mutex, strerror(errcode));