Logo AND Algorithmique Numérique Distribuée

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