Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' into S4U
[simgrid.git] / include / xbt / xbt_os_thread.h
1 /* xbt/xbt_os_thread.h -- Thread portability layer                          */
2
3 /* Copyright (c) 2007-2015. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #ifndef _XBT_OS_THREAD_H
10 #define _XBT_OS_THREAD_H
11
12 #include "simgrid_config.h" /* Windows or Posix */
13 #include "xbt/function_types.h"
14
15 SG_BEGIN_DECL()
16
17 /** @addtogroup XBT_thread
18  *  @brief Thread portability layer
19  * 
20  *  This section describes the thread portability layer. It defines types and 
21  *  functions very close to the pthread API, but it's portable to windows too.
22  * 
23  *  @{
24  */
25   /** \brief Thread data type (opaque structure) */
26 typedef struct xbt_os_thread_ *xbt_os_thread_t;
27
28
29 #ifdef _XBT_WIN32 /* defined if this is a windows system, 32bits or 64bits) */
30 #include <windef.h>
31 typedef DWORD xbt_os_thread_key_t;
32 #else /* assume that every non-windows system is POSIX-compatible */
33
34 #include <pthread.h>
35 typedef pthread_key_t xbt_os_thread_key_t;
36 #endif
37
38 /** Calls pthread_atfork() if present, and raise an exception otherwise.
39  *
40  * The only known user of this wrapper is mmalloc_preinit(), but it is absolutely mandatory there:
41  * when used with tesh, mmalloc *must* be mutex protected and resistant to forks.
42  * This functionality is the only way to get it working (by ensuring that the mutex is consistently released on forks)
43  */
44 XBT_PUBLIC(int) xbt_os_thread_atfork(void (*prepare)(void),
45                                      void (*parent)(void),
46                                      void (*child)(void));
47
48 XBT_PUBLIC(int) xbt_os_get_numcores(void);
49
50 XBT_PUBLIC(xbt_os_thread_t) xbt_os_thread_create(const char *name,
51                                                  pvoid_f_pvoid_t start_routine,
52                                                  void *param,
53                                                  void *data);
54
55 //#define CORE_BINDING  //Uncomment this to enable binding of threads to physical cores. Only Linux.
56 #ifdef CORE_BINDING
57 XBT_PUBLIC(int) xbt_os_thread_bind(xbt_os_thread_t thread, int cpu);
58 #endif
59
60 XBT_PUBLIC(void) xbt_os_thread_exit(int *retcode);
61 XBT_PUBLIC(void) xbt_os_thread_detach(xbt_os_thread_t thread);
62 XBT_PUBLIC(xbt_os_thread_t) xbt_os_thread_self(void);
63 XBT_PUBLIC(const char *) xbt_os_thread_self_name(void);
64 XBT_PUBLIC(const char *) xbt_os_thread_name(xbt_os_thread_t);
65 XBT_PUBLIC(void) xbt_os_thread_set_extra_data(void *data);
66 XBT_PUBLIC(void *) xbt_os_thread_get_extra_data(void);
67 XBT_PUBLIC(void) xbt_os_thread_key_create(xbt_os_thread_key_t* key);
68 XBT_PUBLIC(void) xbt_os_thread_set_specific(xbt_os_thread_key_t key, void* value);
69 XBT_PUBLIC(void*) xbt_os_thread_get_specific(xbt_os_thread_key_t key);
70   /* xbt_os_thread_join frees the joined thread (ie the XBT wrapper around it, the OS frees the rest) */
71 XBT_PUBLIC(void) xbt_os_thread_join(xbt_os_thread_t thread,
72                                     void **thread_return);
73 XBT_PUBLIC(void) xbt_os_thread_yield(void);
74 XBT_PUBLIC(void) xbt_os_thread_cancel(xbt_os_thread_t thread);
75 XBT_PUBLIC(void *) xbt_os_thread_getparam(void);
76 XBT_PUBLIC(void) xbt_os_thread_setstacksize(int stack_size);
77 XBT_PUBLIC(void) xbt_os_thread_setguardsize(int guard_size);
78
79   /** \brief Thread mutex data type (opaque structure) */
80 typedef struct xbt_os_mutex_ *xbt_os_mutex_t;
81
82 XBT_PUBLIC(xbt_os_mutex_t) xbt_os_mutex_init(void);
83 XBT_PUBLIC(void) xbt_os_mutex_acquire(xbt_os_mutex_t mutex);
84 XBT_PUBLIC(void) xbt_os_mutex_timedacquire(xbt_os_mutex_t mutex,
85                                            double delay);
86 XBT_PUBLIC(void) xbt_os_mutex_release(xbt_os_mutex_t mutex);
87 XBT_PUBLIC(void) xbt_os_mutex_destroy(xbt_os_mutex_t mutex);
88
89 /** \brief Thread reentrant mutex data type (opaque structure) */
90 typedef struct xbt_os_rmutex_ *xbt_os_rmutex_t;
91
92 XBT_PUBLIC(xbt_os_rmutex_t) xbt_os_rmutex_init(void);
93 XBT_PUBLIC(void) xbt_os_rmutex_acquire(xbt_os_rmutex_t rmutex);
94 XBT_PUBLIC(void) xbt_os_rmutex_release(xbt_os_rmutex_t rmutex);
95 XBT_PUBLIC(void) xbt_os_rmutex_destroy(xbt_os_rmutex_t rmutex);
96
97
98   /** \brief Thread condition data type (opaque structure) */
99 typedef struct xbt_os_cond_ *xbt_os_cond_t;
100
101 XBT_PUBLIC(xbt_os_cond_t) xbt_os_cond_init(void);
102 XBT_PUBLIC(void) xbt_os_cond_wait(xbt_os_cond_t cond,
103                                   xbt_os_mutex_t mutex);
104 XBT_PUBLIC(void) xbt_os_cond_timedwait(xbt_os_cond_t cond,
105                                        xbt_os_mutex_t mutex, double delay);
106 XBT_PUBLIC(void) xbt_os_cond_signal(xbt_os_cond_t cond);
107 XBT_PUBLIC(void) xbt_os_cond_broadcast(xbt_os_cond_t cond);
108 XBT_PUBLIC(void) xbt_os_cond_destroy(xbt_os_cond_t cond);
109
110    /** \brief Semaphore data type (opaque structure) */
111 typedef struct xbt_os_sem_ *xbt_os_sem_t;
112
113 XBT_PUBLIC(xbt_os_sem_t) xbt_os_sem_init(unsigned int value);
114 XBT_PUBLIC(void) xbt_os_sem_acquire(xbt_os_sem_t sem);
115 XBT_PUBLIC(void) xbt_os_sem_timedacquire(xbt_os_sem_t sem, double timeout);
116 XBT_PUBLIC(void) xbt_os_sem_release(xbt_os_sem_t sem);
117 XBT_PUBLIC(void) xbt_os_sem_destroy(xbt_os_sem_t sem);
118 XBT_PUBLIC(void) xbt_os_sem_get_value(xbt_os_sem_t sem, int *svalue);
119
120
121 /** @} */
122
123 SG_END_DECL()
124 #endif                          /* _XBT_OS_THREAD_H */