1 /* SimGrid's pthread interposer. Actual implementation of the symbols (see the comment in sthread.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>
10 #include <xbt/sysdep.h>
12 #include "src/internal_config.h"
13 #include "src/sthread/sthread.h"
18 #include <semaphore.h>
24 XBT_LOG_NEW_DEFAULT_CATEGORY(sthread, "pthread intercepter");
25 namespace sg4 = simgrid::s4u;
27 static sg4::Host* lilibeth = nullptr;
29 int sthread_main(int argc, char** argv, char** envp, int (*raw_main)(int, char**, char**))
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);
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();
40 XBT_DEBUG("sthread main() is starting in thread %s", id.str().c_str());
42 sg4::Engine e(&argc, argv);
43 auto* zone = sg4::create_full_zone("world");
44 lilibeth = zone->create_host("Lilibeth", 1e15);
47 /* Launch the user's main() on an actor */
49 sg4::ActorPtr main_actor = sg4::Actor::create("tid 0", lilibeth, raw_main, argc, argv, envp);
51 XBT_INFO("Starting the simulation.");
52 sg4::Engine::get_instance()->run();
54 XBT_INFO("All threads exited. Terminating the simulation.");
59 struct sthread_mutex {
63 static void thread_create_wrapper(void* (*user_function)(void*), void* param)
74 int sthread_create(unsigned long int* thread, const void* /*pthread_attr_t* attr*/, void* (*start_routine)(void*),
79 XBT_VERB("Create thread %d", TID);
83 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
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);
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 static_cast<sg4::Mutex*>(mutex->mutex)->lock();
117 int sthread_mutex_trylock(sthread_mutex_t* mutex)
119 return static_cast<sg4::Mutex*>(mutex->mutex)->try_lock();
122 int sthread_mutex_unlock(sthread_mutex_t* mutex)
124 static_cast<sg4::Mutex*>(mutex->mutex)->unlock();
127 int sthread_mutex_destroy(sthread_mutex_t* mutex)
129 intrusive_ptr_release(static_cast<sg4::Mutex*>(mutex->mutex));
133 int sthread_gettimeofday(struct timeval* 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)
145 void sthread_sleep(double seconds)
147 simgrid::s4u::this_actor::sleep_for(seconds);
151 int sem_init(sem_t *sem, int pshared, unsigned int value) {
154 res=raw_sem_init(sem,pshared,value);
158 int sem_wait(sem_t *sem) {
161 res = raw_sem_wait(sem);
165 int sem_post(sem_t *sem) {
166 return raw_sem_post(sem);
169 int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr) {
170 *cond = sg_cond_init();
174 int pthread_cond_signal(pthread_cond_t *cond) {
175 sg_cond_notify_one(*cond);
179 int pthread_cond_broadcast(pthread_cond_t *cond) {
180 sg_cond_notify_all(*cond);
184 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) {
185 sg_cond_wait(*cond, *mutex);
189 int pthread_cond_destroy(pthread_cond_t *cond) {
190 sg_cond_destroy(*cond);