1 /* SimGrid's pthread interposer. Actual implementation of the symbols (see the comment in sthread.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>
11 #include <xbt/sysdep.h>
13 #include "src/internal_config.h"
14 #include "src/sthread/sthread.h"
19 #include <semaphore.h>
25 XBT_LOG_NEW_DEFAULT_CATEGORY(sthread, "pthread intercepter");
26 namespace sg4 = simgrid::s4u;
28 static sg4::Host* lilibeth = nullptr;
30 int sthread_main(int argc, char** argv, char** envp, int (*raw_main)(int, char**, char**))
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);
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();
41 XBT_DEBUG("sthread main() is starting in thread %s", id.str().c_str());
43 sg4::Engine e(&argc, argv);
44 auto* zone = sg4::create_full_zone("world");
45 lilibeth = zone->create_host("Lilibeth", 1e15);
48 /* Launch the user's main() on an actor */
50 sg4::ActorPtr main_actor = sg4::Actor::create("main thread", lilibeth, raw_main, argc, argv, envp);
52 XBT_INFO("Starting the simulation.");
53 sg4::Engine::get_instance()->run();
55 XBT_INFO("All threads exited. Terminating the simulation.");
60 struct sthread_mutex {
64 static void thread_create_wrapper(void* (*user_function)(void*), void* param)
75 int sthread_create(unsigned long int* thread, const void* /*pthread_attr_t* attr*/, void* (*start_routine)(void*),
80 XBT_VERB("Create thread %d", TID);
84 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
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);
89 intrusive_ptr_add_ref(actor.get());
90 *thread = reinterpret_cast<unsigned long>(actor.get());
93 int sthread_join(sthread_t thread, void** /*retval*/)
95 sg4::ActorPtr actor(reinterpret_cast<sg4::Actor*>(thread));
97 intrusive_ptr_release(actor.get());
102 int sthread_mutex_init(sthread_mutex_t* mutex, const void* /*pthread_mutexattr_t* attr*/)
104 auto m = sg4::Mutex::create();
105 intrusive_ptr_add_ref(m.get());
107 mutex->mutex = m.get();
111 int sthread_mutex_lock(sthread_mutex_t* mutex)
113 /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */
114 if (mutex->mutex == nullptr)
115 sthread_mutex_init(mutex, nullptr);
117 static_cast<sg4::Mutex*>(mutex->mutex)->lock();
121 int sthread_mutex_trylock(sthread_mutex_t* mutex)
123 /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */
124 if (mutex->mutex == nullptr)
125 sthread_mutex_init(mutex, nullptr);
127 return static_cast<sg4::Mutex*>(mutex->mutex)->try_lock();
130 int sthread_mutex_unlock(sthread_mutex_t* mutex)
132 /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */
133 if (mutex->mutex == nullptr)
134 sthread_mutex_init(mutex, nullptr);
136 static_cast<sg4::Mutex*>(mutex->mutex)->unlock();
139 int sthread_mutex_destroy(sthread_mutex_t* mutex)
141 /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */
142 if (mutex->mutex == nullptr)
143 sthread_mutex_init(mutex, nullptr);
145 intrusive_ptr_release(static_cast<sg4::Mutex*>(mutex->mutex));
149 int sthread_gettimeofday(struct timeval* 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)
161 void sthread_sleep(double seconds)
163 simgrid::s4u::this_actor::sleep_for(seconds);
167 int sem_init(sem_t *sem, int pshared, unsigned int value) {
170 res=raw_sem_init(sem,pshared,value);
174 int sem_wait(sem_t *sem) {
177 res = raw_sem_wait(sem);
181 int sem_post(sem_t *sem) {
182 return raw_sem_post(sem);
185 int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr) {
186 *cond = sg_cond_init();
190 int pthread_cond_signal(pthread_cond_t *cond) {
191 sg_cond_notify_one(*cond);
195 int pthread_cond_broadcast(pthread_cond_t *cond) {
196 sg_cond_notify_all(*cond);
200 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) {
201 sg_cond_wait(*cond, *mutex);
205 int pthread_cond_destroy(pthread_cond_t *cond) {
206 sg_cond_destroy(*cond);