1 /* xbt/xbt_os_thread.h -- Thread portability layer */
3 /* Copyright (c) 2007-2012. The SimGrid Team.
4 * All rights reserved. */
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. */
9 #ifndef _XBT_OS_THREAD_H
10 #define _XBT_OS_THREAD_H
12 #include "simgrid_config.h" /* Windows or Posix */
13 #include "xbt/function_types.h"
17 /** @addtogroup XBT_thread
18 * @brief Thread portability layer
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.
25 /** \brief Thread data type (opaque structure) */
26 typedef struct xbt_os_thread_ *xbt_os_thread_t;
30 #ifdef _XBT_WIN32 /* defined if this is a windows system, 32bits or 64bits) */
32 typedef DWORD xbt_os_thread_key_t;
33 #else /* assume that every non-windows system is POSIX-compatible */
34 typedef pthread_key_t xbt_os_thread_key_t;
37 /** Calls pthread_atfork() if present, and raise an exception otherwise.
39 * The only known user of this wrapper is mmalloc_preinit(), but it is absolutely mandatory there:
40 * when used with tesh, mmalloc *must* be mutex protected and resistant to forks.
41 * This functionality is the only way to get it working (by ensuring that the mutex is consistently released on forks)
43 XBT_PUBLIC(int) xbt_os_thread_atfork(void (*prepare)(void),
47 XBT_PUBLIC(int) xbt_os_get_numcores(void);
49 XBT_PUBLIC(xbt_os_thread_t) xbt_os_thread_create(const char *name,
50 pvoid_f_pvoid_t start_routine,
54 XBT_PUBLIC(void) xbt_os_thread_exit(int *retcode);
55 XBT_PUBLIC(void) xbt_os_thread_detach(xbt_os_thread_t thread);
56 XBT_PUBLIC(xbt_os_thread_t) xbt_os_thread_self(void);
57 XBT_PUBLIC(const char *) xbt_os_thread_self_name(void);
58 XBT_PUBLIC(const char *) xbt_os_thread_name(xbt_os_thread_t);
59 XBT_PUBLIC(void) xbt_os_thread_set_extra_data(void *data);
60 XBT_PUBLIC(void *) xbt_os_thread_get_extra_data(void);
61 XBT_PUBLIC(void) xbt_os_thread_key_create(xbt_os_thread_key_t* key);
62 XBT_PUBLIC(void) xbt_os_thread_set_specific(xbt_os_thread_key_t key, void* value);
63 XBT_PUBLIC(void*) xbt_os_thread_get_specific(xbt_os_thread_key_t key);
64 /* xbt_os_thread_join frees the joined thread (ie the XBT wrapper around it, the OS frees the rest) */
65 XBT_PUBLIC(void) xbt_os_thread_join(xbt_os_thread_t thread,
66 void **thread_return);
67 XBT_PUBLIC(void) xbt_os_thread_yield(void);
68 XBT_PUBLIC(void) xbt_os_thread_cancel(xbt_os_thread_t thread);
69 XBT_PUBLIC(void *) xbt_os_thread_getparam(void);
70 XBT_PUBLIC(void) xbt_os_thread_setstacksize(int stack_size);
72 /** \brief Thread mutex data type (opaque structure) */
73 typedef struct xbt_os_mutex_ *xbt_os_mutex_t;
75 XBT_PUBLIC(xbt_os_mutex_t) xbt_os_mutex_init(void);
76 XBT_PUBLIC(void) xbt_os_mutex_acquire(xbt_os_mutex_t mutex);
77 XBT_PUBLIC(void) xbt_os_mutex_timedacquire(xbt_os_mutex_t mutex,
79 XBT_PUBLIC(void) xbt_os_mutex_release(xbt_os_mutex_t mutex);
80 XBT_PUBLIC(void) xbt_os_mutex_destroy(xbt_os_mutex_t mutex);
82 /** \brief Thread reentrant mutex data type (opaque structure) */
83 typedef struct xbt_os_rmutex_ *xbt_os_rmutex_t;
85 XBT_PUBLIC(xbt_os_rmutex_t) xbt_os_rmutex_init(void);
86 XBT_PUBLIC(void) xbt_os_rmutex_acquire(xbt_os_rmutex_t rmutex);
87 XBT_PUBLIC(void) xbt_os_rmutex_release(xbt_os_rmutex_t rmutex);
88 XBT_PUBLIC(void) xbt_os_rmutex_destroy(xbt_os_rmutex_t rmutex);
91 /** \brief Thread condition data type (opaque structure) */
92 typedef struct xbt_os_cond_ *xbt_os_cond_t;
94 XBT_PUBLIC(xbt_os_cond_t) xbt_os_cond_init(void);
95 XBT_PUBLIC(void) xbt_os_cond_wait(xbt_os_cond_t cond,
96 xbt_os_mutex_t mutex);
97 XBT_PUBLIC(void) xbt_os_cond_timedwait(xbt_os_cond_t cond,
98 xbt_os_mutex_t mutex, double delay);
99 XBT_PUBLIC(void) xbt_os_cond_signal(xbt_os_cond_t cond);
100 XBT_PUBLIC(void) xbt_os_cond_broadcast(xbt_os_cond_t cond);
101 XBT_PUBLIC(void) xbt_os_cond_destroy(xbt_os_cond_t cond);
103 /** \brief Semaphore data type (opaque structure) */
104 typedef struct xbt_os_sem_ *xbt_os_sem_t;
106 XBT_PUBLIC(xbt_os_sem_t) xbt_os_sem_init(unsigned int value);
107 XBT_PUBLIC(void) xbt_os_sem_acquire(xbt_os_sem_t sem);
108 XBT_PUBLIC(void) xbt_os_sem_timedacquire(xbt_os_sem_t sem, double timeout);
109 XBT_PUBLIC(void) xbt_os_sem_release(xbt_os_sem_t sem);
110 XBT_PUBLIC(void) xbt_os_sem_destroy(xbt_os_sem_t sem);
111 XBT_PUBLIC(void) xbt_os_sem_get_value(xbt_os_sem_t sem, int *svalue);
117 #endif /* _XBT_OS_THREAD_H */