Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Implement pthread_barrier calls in sthread, and test them in McMini
[simgrid.git] / src / sthread / sthread.h
1 /* Copyright (c) 2002-2023. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 /* SimGrid's pthread interposer. Intercepts most of the pthread and semaphore calls to model-check them.
7  *
8  * Intercepting on pthread is somewhat complicated by the fact that pthread is used everywhere in the system headers.
9  * To reduce definition conflicts, our redefinitions of the pthread symbols (in sthread.c) load as little headers as
10  * possible. Thus, the actual implementations are separated in another file (sthread_impl.cpp) and are used as black
11  * boxes by our redefinitions.
12  *
13  * The sthread_* symbols are those actual implementations, used in the pthread_* redefinitions. */
14
15 #ifndef SIMGRID_STHREAD_H
16 #define SIMGRID_STHREAD_H
17
18 #include "xbt/base.h"
19 #include <sys/time.h>
20
21 #if defined(__cplusplus)
22 extern "C" {
23 #endif
24
25 // Launch the simulation. The old main function (passed as a parameter) is launched as an actor
26 int sthread_main(int argc, char** argv, char** envp, int (*raw_main)(int, char**, char**));
27 XBT_PUBLIC void sthread_enable(void);  // Start intercepting all pthread calls
28 XBT_PUBLIC void sthread_disable(void); // Stop intercepting all pthread calls
29
30 typedef unsigned long int sthread_t;
31 int sthread_create(sthread_t* thread, const /*pthread_attr_t*/ void* attr, void* (*start_routine)(void*), void* arg);
32 int sthread_join(sthread_t thread, void** retval);
33
34 typedef struct {
35   unsigned recursive : 1;
36   unsigned errorcheck : 1;
37   unsigned robust : 1;
38 } sthread_mutexattr_t;
39
40 int sthread_mutexattr_init(sthread_mutexattr_t* attr);
41 int sthread_mutexattr_settype(sthread_mutexattr_t* attr, int type);
42 int sthread_mutexattr_gettype(const sthread_mutexattr_t* attr, int* type);
43 int sthread_mutexattr_getrobust(const sthread_mutexattr_t* attr, int* robustness);
44 int sthread_mutexattr_setrobust(sthread_mutexattr_t* attr, int robustness);
45
46 typedef struct {
47   void* mutex;
48 } sthread_mutex_t;
49 int sthread_mutex_init(sthread_mutex_t* mutex, const sthread_mutexattr_t* attr);
50 int sthread_mutex_lock(sthread_mutex_t* mutex);
51 int sthread_mutex_trylock(sthread_mutex_t* mutex);
52 int sthread_mutex_unlock(sthread_mutex_t* mutex);
53 int sthread_mutex_destroy(sthread_mutex_t* mutex);
54
55 typedef struct {
56   unsigned unused : 1;
57 } sthread_barrierattr_t;
58
59 typedef struct {
60   void* barrier;
61 } sthread_barrier_t;
62 int sthread_barrier_init(sthread_barrier_t* barrier, const sthread_barrierattr_t* attr, unsigned count);
63 int sthread_barrier_wait(sthread_barrier_t* barrier);
64 int sthread_barrier_destroy(sthread_barrier_t* barrier);
65
66 typedef struct {
67   void* sem;
68 } sthread_sem_t;
69 int sthread_sem_init(sthread_sem_t* sem, int pshared, unsigned int value);
70 int sthread_sem_destroy(sthread_sem_t* sem);
71 int sthread_sem_post(sthread_sem_t* sem);
72 int sthread_sem_wait(sthread_sem_t* sem);
73 int sthread_sem_trywait(sthread_sem_t* sem);
74 int sthread_sem_timedwait(sthread_sem_t* sem, const struct timespec* abs_timeout);
75
76 int sthread_gettimeofday(struct timeval* tv);
77 unsigned int sthread_sleep(double seconds);
78 int sthread_usleep(double seconds);
79
80 int sthread_access_begin(void* objaddr, const char* objname, const char* file, int line, const char* function);
81 void sthread_access_end(void* objaddr, const char* objname, const char* file, int line, const char* function);
82
83 #if defined(__cplusplus)
84 }
85 #endif
86
87 #endif