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>
23 #include <string_view>
26 XBT_LOG_NEW_DEFAULT_CATEGORY(sthread, "pthread intercepter");
27 namespace sg4 = simgrid::s4u;
29 static sg4::Host* lilibeth = nullptr;
31 int sthread_main(int argc, char** argv, char** envp, int (*raw_main)(int, char**, char**))
33 /* Do not intercept the main when run from SMPI: it will initialize the simulation properly */
34 for (int i = 0; envp[i] != nullptr; i++)
35 if (std::string_view(envp[i]).rfind("SMPI_GLOBAL_SIZE", 0) == 0)
36 return raw_main(argc, argv, envp);
38 /* If not in SMPI, the old main becomes an actor in a newly created simulation */
39 std::ostringstream id;
40 id << std::this_thread::get_id();
42 XBT_DEBUG("sthread main() is starting in thread %s", id.str().c_str());
44 sg4::Engine e(&argc, argv);
45 auto* zone = sg4::create_full_zone("world");
46 lilibeth = zone->create_host("Lilibeth", 1e15);
49 /* Launch the user's main() on an actor */
51 sg4::ActorPtr main_actor = sg4::Actor::create("main thread", lilibeth, raw_main, argc, argv, envp);
53 XBT_INFO("Starting the simulation.");
54 sg4::Engine::get_instance()->run();
56 XBT_INFO("All threads exited. Terminating the simulation.");
61 struct sthread_mutex {
65 int sthread_create(unsigned long int* thread, const void* /*pthread_attr_t* attr*/, void* (*start_routine)(void*),
70 XBT_VERB("Create thread %d", TID);
74 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
76 std::string name = simgrid::xbt::string_printf("%d:%d", rank, TID);
77 sg4::ActorPtr actor = sg4::Actor::create(
79 [](auto* user_function, auto* param) {
90 intrusive_ptr_add_ref(actor.get());
91 *thread = reinterpret_cast<unsigned long>(actor.get());
94 int sthread_join(sthread_t thread, void** /*retval*/)
96 sg4::ActorPtr actor(reinterpret_cast<sg4::Actor*>(thread));
98 intrusive_ptr_release(actor.get());
103 int sthread_mutex_init(sthread_mutex_t* mutex, const void* /*pthread_mutexattr_t* attr*/)
105 auto m = sg4::Mutex::create();
106 intrusive_ptr_add_ref(m.get());
108 mutex->mutex = m.get();
112 int sthread_mutex_lock(sthread_mutex_t* mutex)
114 /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */
115 if (mutex->mutex == nullptr)
116 sthread_mutex_init(mutex, nullptr);
118 static_cast<sg4::Mutex*>(mutex->mutex)->lock();
122 int sthread_mutex_trylock(sthread_mutex_t* mutex)
124 /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */
125 if (mutex->mutex == nullptr)
126 sthread_mutex_init(mutex, nullptr);
128 return static_cast<sg4::Mutex*>(mutex->mutex)->try_lock();
131 int sthread_mutex_unlock(sthread_mutex_t* mutex)
133 /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */
134 if (mutex->mutex == nullptr)
135 sthread_mutex_init(mutex, nullptr);
137 static_cast<sg4::Mutex*>(mutex->mutex)->unlock();
140 int sthread_mutex_destroy(sthread_mutex_t* mutex)
142 /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */
143 if (mutex->mutex == nullptr)
144 sthread_mutex_init(mutex, nullptr);
146 intrusive_ptr_release(static_cast<sg4::Mutex*>(mutex->mutex));
150 int sthread_gettimeofday(struct timeval* tv)
153 double now = simgrid::s4u::Engine::get_clock();
154 double secs = trunc(now);
155 double usecs = (now - secs) * 1e6;
156 tv->tv_sec = static_cast<time_t>(secs);
157 tv->tv_usec = static_cast<decltype(tv->tv_usec)>(usecs); // suseconds_t (or useconds_t on WIN32)
162 void sthread_sleep(double seconds)
164 simgrid::s4u::this_actor::sleep_for(seconds);
168 int sem_init(sem_t *sem, int pshared, unsigned int value) {
171 res=raw_sem_init(sem,pshared,value);
175 int sem_wait(sem_t *sem) {
178 res = raw_sem_wait(sem);
182 int sem_post(sem_t *sem) {
183 return raw_sem_post(sem);
186 int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr) {
187 *cond = sg_cond_init();
191 int pthread_cond_signal(pthread_cond_t *cond) {
192 sg_cond_notify_one(*cond);
196 int pthread_cond_broadcast(pthread_cond_t *cond) {
197 sg_cond_notify_all(*cond);
201 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) {
202 sg_cond_wait(*cond, *mutex);
206 int pthread_cond_destroy(pthread_cond_t *cond) {
207 sg_cond_destroy(*cond);