Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Make sure that sthread_inside_simgrid is initially 1 to protect all lib constructors
[simgrid.git] / src / sthread / sthread_impl.cpp
1 /* SimGrid's pthread interposer. Actual implementation of the symbols (see the comment in sthread.h) */
2
3 #include "smpi/smpi.h"
4 #include <simgrid/actor.h>
5 #include <simgrid/s4u/Actor.hpp>
6 #include <simgrid/s4u/Engine.hpp>
7 #include <simgrid/s4u/Mutex.hpp>
8 #include <simgrid/s4u/NetZone.hpp>
9 #include <xbt/base.h>
10 #include <xbt/sysdep.h>
11
12 #include "src/internal_config.h"
13 #include "src/sthread/sthread.h"
14
15 #include <dlfcn.h>
16 #include <pthread.h>
17 #include <semaphore.h>
18 #include <sstream>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <thread>
22
23 XBT_LOG_NEW_DEFAULT_CATEGORY(sthread, "pthread intercepter");
24 namespace sg4 = simgrid::s4u;
25
26 static sg4::Host* lilibeth = NULL;
27
28 int sthread_main(int argc, char** argv, char** envp, int (*raw_main)(int, char**, char**))
29 {
30   std::ostringstream id;
31   id << std::this_thread::get_id();
32
33   XBT_INFO("sthread main() is starting in thread %s", id.str().c_str());
34
35   sg4::Engine e(&argc, argv);
36   auto* zone = sg4::create_full_zone("world");
37   lilibeth   = zone->create_host("Lilibeth", 1e15);
38   zone->seal();
39
40   /* Launch the user's main() on an actor */
41   sthread_inside_simgrid   = 0;
42   sg4::ActorPtr main_actor = sg4::Actor::create("tid 0", lilibeth, raw_main, argc, argv, envp);
43
44   XBT_INFO("sthread main() is launching the simulation");
45   sg4::Engine::get_instance()->run();
46
47   return 0;
48 }
49 struct sthread_mutex {
50   s4u_Mutex* mutex;
51 };
52
53 static void thread_create_wrapper(void* (*user_function)(void*), void* param)
54 {
55 #if HAVE_SMPI
56   if (SMPI_is_inited())
57     SMPI_thread_create();
58 #endif
59   sthread_inside_simgrid = 0;
60   user_function(param);
61   sthread_inside_simgrid = 1;
62 }
63
64 int sthread_create(unsigned long int* thread, const /*pthread_attr_t*/ void* attr, void* (*start_routine)(void*),
65                    void* arg)
66 {
67   static int TID = 1;
68
69   TID++;
70   int rank = 0;
71 #if HAVE_SMPI
72   if (SMPI_is_inited())
73     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
74 #endif
75   char name[100];
76   sprintf(name, "%d:%d", rank, TID);
77   sg4::ActorPtr actor = sg4::Actor::init(name, lilibeth);
78   actor->start(thread_create_wrapper, start_routine, arg);
79
80   intrusive_ptr_add_ref(actor.get());
81   *thread = reinterpret_cast<unsigned long>(actor.get());
82   return 0;
83 }
84
85 int sthread_mutex_init(sthread_mutex_t* mutex, const /*pthread_mutexattr_t*/ void* attr)
86 {
87   auto m = sg4::Mutex::create();
88   intrusive_ptr_add_ref(m.get());
89
90   mutex->mutex = m.get();
91   return 0;
92 }
93
94 int sthread_mutex_lock(sthread_mutex_t* mutex)
95 {
96   static_cast<sg4::Mutex*>(mutex->mutex)->lock();
97   return 0;
98 }
99
100 int sthread_mutex_trylock(sthread_mutex_t* mutex)
101 {
102   return static_cast<sg4::Mutex*>(mutex->mutex)->try_lock();
103 }
104
105 int sthread_mutex_unlock(sthread_mutex_t* mutex)
106 {
107   static_cast<sg4::Mutex*>(mutex->mutex)->unlock();
108   return 0;
109 }
110 int sthread_mutex_destroy(sthread_mutex_t* mutex)
111 {
112   intrusive_ptr_release(static_cast<sg4::Mutex*>(mutex->mutex));
113   return 0;
114 }
115
116 #if 0
117 int sem_init(sem_t *sem, int pshared, unsigned int value) {
118         int res;
119
120         res=raw_sem_init(sem,pshared,value);
121         return res;
122 }
123
124 int sem_wait(sem_t *sem) {
125         int res;
126
127         res = raw_sem_wait(sem);
128         return res;
129 }
130
131 int sem_post(sem_t *sem) {
132         return raw_sem_post(sem);
133 }
134
135 int pthread_join(pthread_t thread, void **retval) {
136         sg_actor_join(thread, -1);
137     return 0;
138 }
139
140 int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr) {
141     *cond = sg_cond_init();
142     return 0;
143 }
144
145 int pthread_cond_signal(pthread_cond_t *cond) {
146         sg_cond_notify_one(*cond);
147     return 0;
148 }
149
150 int pthread_cond_broadcast(pthread_cond_t *cond) {
151         sg_cond_notify_all(*cond);
152     return 0;
153 }
154
155 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) {
156         sg_cond_wait(*cond, *mutex);
157     return 0;
158 }
159
160 int pthread_cond_destroy(pthread_cond_t *cond) {
161         sg_cond_destroy(*cond);
162     return 0;
163 }
164 #endif