Logo AND Algorithmique Numérique Distribuée

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