Logo AND Algorithmique Numérique Distribuée

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