Logo AND Algorithmique Numérique Distribuée

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