Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Stop trying to build on native WIN32, it's broken anyway
[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 "xbt/string.hpp"
5 #include <simgrid/actor.h>
6 #include <simgrid/s4u/Actor.hpp>
7 #include <simgrid/s4u/Engine.hpp>
8 #include <simgrid/s4u/Mutex.hpp>
9 #include <simgrid/s4u/NetZone.hpp>
10 #include <xbt/base.h>
11 #include <xbt/sysdep.h>
12
13 #include "src/internal_config.h"
14 #include "src/sthread/sthread.h"
15
16 #include <cmath>
17 #include <dlfcn.h>
18 #include <pthread.h>
19 #include <semaphore.h>
20 #include <sstream>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string_view>
24 #include <thread>
25
26 XBT_LOG_NEW_DEFAULT_CATEGORY(sthread, "pthread intercepter");
27 namespace sg4 = simgrid::s4u;
28
29 static sg4::Host* lilibeth = nullptr;
30
31 int sthread_main(int argc, char** argv, char** envp, int (*raw_main)(int, char**, char**))
32 {
33   /* Do not intercept the main when run from SMPI: it will initialize the simulation properly */
34   for (int i = 0; envp[i] != nullptr; i++)
35     if (std::string_view(envp[i]).rfind("SMPI_GLOBAL_SIZE", 0) == 0)
36       return raw_main(argc, argv, envp);
37
38   /* If not in SMPI, the old main becomes an actor in a newly created simulation */
39   std::ostringstream id;
40   id << std::this_thread::get_id();
41
42   XBT_DEBUG("sthread main() is starting in thread %s", id.str().c_str());
43
44   sg4::Engine e(&argc, argv);
45   auto* zone = sg4::create_full_zone("world");
46   lilibeth   = zone->create_host("Lilibeth", 1e15);
47   zone->seal();
48
49   /* Launch the user's main() on an actor */
50   sthread_enable();
51   sg4::ActorPtr main_actor = sg4::Actor::create("main thread", lilibeth, raw_main, argc, argv, envp);
52
53   XBT_INFO("Starting the simulation.");
54   sg4::Engine::get_instance()->run();
55   sthread_disable();
56   XBT_INFO("All threads exited. Terminating the simulation.");
57
58   return 0;
59 }
60
61 struct sthread_mutex {
62   s4u_Mutex* mutex;
63 };
64
65 int sthread_create(unsigned long int* thread, const void* /*pthread_attr_t* attr*/, void* (*start_routine)(void*),
66                    void* arg)
67 {
68   static int TID = 0;
69   TID++;
70   XBT_VERB("Create thread %d", TID);
71   int rank = 0;
72 #if HAVE_SMPI
73   if (SMPI_is_inited())
74     MPI_Comm_rank(MPI_COMM_WORLD, &rank);
75 #endif
76   std::string name    = simgrid::xbt::string_printf("%d:%d", rank, TID);
77   sg4::ActorPtr actor = sg4::Actor::create(
78       name, lilibeth,
79       [](auto* user_function, auto* param) {
80 #if HAVE_SMPI
81         if (SMPI_is_inited())
82           SMPI_thread_create();
83 #endif
84         sthread_enable();
85         user_function(param);
86         sthread_disable();
87       },
88       start_routine, arg);
89
90   intrusive_ptr_add_ref(actor.get());
91   *thread = reinterpret_cast<unsigned long>(actor.get());
92   return 0;
93 }
94 int sthread_join(sthread_t thread, void** /*retval*/)
95 {
96   sg4::ActorPtr actor(reinterpret_cast<sg4::Actor*>(thread));
97   actor->join();
98   intrusive_ptr_release(actor.get());
99
100   return 0;
101 }
102
103 int sthread_mutex_init(sthread_mutex_t* mutex, const void* /*pthread_mutexattr_t* attr*/)
104 {
105   auto m = sg4::Mutex::create();
106   intrusive_ptr_add_ref(m.get());
107
108   mutex->mutex = m.get();
109   return 0;
110 }
111
112 int sthread_mutex_lock(sthread_mutex_t* mutex)
113 {
114   /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */
115   if (mutex->mutex == nullptr)
116     sthread_mutex_init(mutex, nullptr);
117
118   static_cast<sg4::Mutex*>(mutex->mutex)->lock();
119   return 0;
120 }
121
122 int sthread_mutex_trylock(sthread_mutex_t* mutex)
123 {
124   /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */
125   if (mutex->mutex == nullptr)
126     sthread_mutex_init(mutex, nullptr);
127
128   return static_cast<sg4::Mutex*>(mutex->mutex)->try_lock();
129 }
130
131 int sthread_mutex_unlock(sthread_mutex_t* mutex)
132 {
133   /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */
134   if (mutex->mutex == nullptr)
135     sthread_mutex_init(mutex, nullptr);
136
137   static_cast<sg4::Mutex*>(mutex->mutex)->unlock();
138   return 0;
139 }
140 int sthread_mutex_destroy(sthread_mutex_t* mutex)
141 {
142   /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */
143   if (mutex->mutex == nullptr)
144     sthread_mutex_init(mutex, nullptr);
145
146   intrusive_ptr_release(static_cast<sg4::Mutex*>(mutex->mutex));
147   return 0;
148 }
149
150 int sthread_gettimeofday(struct timeval* tv)
151 {
152   if (tv) {
153     double now   = simgrid::s4u::Engine::get_clock();
154     double secs  = trunc(now);
155     double usecs = (now - secs) * 1e6;
156     tv->tv_sec   = static_cast<time_t>(secs);
157     tv->tv_usec  = static_cast<decltype(tv->tv_usec)>(usecs); // suseconds_t
158   }
159   return 0;
160 }
161
162 void sthread_sleep(double seconds)
163 {
164   simgrid::s4u::this_actor::sleep_for(seconds);
165 }
166
167 #if 0
168 int sem_init(sem_t *sem, int pshared, unsigned int value) {
169         int res;
170
171         res=raw_sem_init(sem,pshared,value);
172         return res;
173 }
174
175 int sem_wait(sem_t *sem) {
176         int res;
177
178         res = raw_sem_wait(sem);
179         return res;
180 }
181
182 int sem_post(sem_t *sem) {
183         return raw_sem_post(sem);
184 }
185
186 int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr) {
187     *cond = sg_cond_init();
188     return 0;
189 }
190
191 int pthread_cond_signal(pthread_cond_t *cond) {
192         sg_cond_notify_one(*cond);
193     return 0;
194 }
195
196 int pthread_cond_broadcast(pthread_cond_t *cond) {
197         sg_cond_notify_all(*cond);
198     return 0;
199 }
200
201 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) {
202         sg_cond_wait(*cond, *mutex);
203     return 0;
204 }
205
206 int pthread_cond_destroy(pthread_cond_t *cond) {
207         sg_cond_destroy(*cond);
208     return 0;
209 }
210 #endif