1 /* Copyright (c) 2002-2023. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 /* SimGrid's pthread interposer. Actual implementation of the symbols (see the comment in sthread.h) */
9 #include "xbt/asserts.h"
12 #include "xbt/string.hpp"
13 #include <simgrid/actor.h>
14 #include <simgrid/s4u/Actor.hpp>
15 #include <simgrid/s4u/Engine.hpp>
16 #include <simgrid/s4u/Mutex.hpp>
17 #include <simgrid/s4u/NetZone.hpp>
18 #include <simgrid/s4u/Semaphore.hpp>
20 #include <xbt/sysdep.h>
22 #include "src/internal_config.h"
23 #include "src/sthread/sthread.h"
28 #include <semaphore.h>
32 #include <string_view>
35 XBT_LOG_NEW_DEFAULT_CATEGORY(sthread, "pthread intercepter");
36 namespace sg4 = simgrid::s4u;
38 static sg4::Host* lilibeth = nullptr;
40 int sthread_main(int argc, char** argv, char** envp, int (*raw_main)(int, char**, char**))
42 /* Do not intercept the main when run from SMPI: it will initialize the simulation properly */
43 for (int i = 0; envp[i] != nullptr; i++)
44 if (std::string_view(envp[i]).rfind("SMPI_GLOBAL_SIZE", 0) == 0) {
45 printf("sthread refuses to intercept the SMPI application %s directly, as its interception is done otherwise.\n",
47 return raw_main(argc, argv, envp);
50 /* Do not intercept valgrind step 1 */
51 if (not strcmp(argv[0], "/usr/bin/valgrind.bin") || not strcmp(argv[0], "/bin/sh")) {
52 printf("sthread refuses to intercept the execution of %s. Running the application unmodified.\n", argv[0]);
54 return raw_main(argc, argv, envp);
57 /* If not in SMPI, the old main becomes an actor in a newly created simulation */
58 printf("sthread is intercepting the execution of %s\n", argv[0]);
61 sg4::Engine e(&argc, argv);
62 auto* zone = sg4::create_full_zone("world");
63 lilibeth = zone->create_host("Lilibeth", 1e15);
66 /* Launch the user's main() on an actor */
68 sg4::ActorPtr main_actor = sg4::Actor::create("main thread", lilibeth, raw_main, argc, argv, envp);
70 sg4::Engine::get_instance()->run();
72 XBT_INFO("All threads exited. Terminating the simulation.");
77 struct sthread_mutex {
81 int sthread_create(unsigned long int* thread, const void* /*pthread_attr_t* attr*/, void* (*start_routine)(void*),
86 XBT_VERB("Create thread %d", TID);
87 std::string name = std::string("thread ") + std::to_string(TID);
89 if (SMPI_is_inited()) {
91 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
92 name = simgrid::xbt::string_printf("%d:%d", rank, TID);
95 sg4::ActorPtr actor = sg4::Actor::create(
97 [](auto* user_function, auto* param) {
100 SMPI_thread_create();
103 user_function(param);
108 intrusive_ptr_add_ref(actor.get());
109 *thread = reinterpret_cast<unsigned long>(actor.get());
112 int sthread_join(sthread_t thread, void** /*retval*/)
114 sg4::ActorPtr actor(reinterpret_cast<sg4::Actor*>(thread));
116 intrusive_ptr_release(actor.get());
121 int sthread_mutexattr_init(sthread_mutexattr_t* attr)
123 memset(attr, 0, sizeof(*attr));
126 int sthread_mutexattr_settype(sthread_mutexattr_t* attr, int type)
129 case PTHREAD_MUTEX_NORMAL:
130 xbt_assert(not attr->recursive, "S4U does not allow to remove the recursivness of a mutex.");
133 case PTHREAD_MUTEX_RECURSIVE:
135 attr->errorcheck = 0; // reset
137 case PTHREAD_MUTEX_ERRORCHECK:
138 attr->errorcheck = 1;
146 int sthread_mutexattr_gettype(const sthread_mutexattr_t* attr, int* type)
149 *type = PTHREAD_MUTEX_RECURSIVE;
150 else if (attr->errorcheck)
151 *type = PTHREAD_MUTEX_ERRORCHECK;
153 *type = PTHREAD_MUTEX_NORMAL;
156 int sthread_mutexattr_getrobust(const sthread_mutexattr_t* attr, int* robustness)
158 *robustness = attr->robust;
161 int sthread_mutexattr_setrobust(sthread_mutexattr_t* attr, int robustness)
163 attr->robust = robustness;
169 int sthread_mutex_init(sthread_mutex_t* mutex, const sthread_mutexattr_t* attr)
171 auto m = sg4::Mutex::create(attr != nullptr && attr->recursive);
172 intrusive_ptr_add_ref(m.get());
174 mutex->mutex = m.get();
178 int sthread_mutex_lock(sthread_mutex_t* mutex)
180 /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */
181 if (mutex->mutex == nullptr)
182 sthread_mutex_init(mutex, nullptr);
184 XBT_DEBUG("%s(%p)", __func__, mutex);
185 static_cast<sg4::Mutex*>(mutex->mutex)->lock();
189 int sthread_mutex_trylock(sthread_mutex_t* mutex)
191 /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */
192 if (mutex->mutex == nullptr)
193 sthread_mutex_init(mutex, nullptr);
195 XBT_DEBUG("%s(%p)", __func__, mutex);
196 if (static_cast<sg4::Mutex*>(mutex->mutex)->try_lock())
201 int sthread_mutex_unlock(sthread_mutex_t* mutex)
203 /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */
204 if (mutex->mutex == nullptr)
205 sthread_mutex_init(mutex, nullptr);
207 XBT_DEBUG("%s(%p)", __func__, mutex);
208 static_cast<sg4::Mutex*>(mutex->mutex)->unlock();
211 int sthread_mutex_destroy(sthread_mutex_t* mutex)
213 /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */
214 if (mutex->mutex == nullptr)
215 sthread_mutex_init(mutex, nullptr);
217 XBT_DEBUG("%s(%p)", __func__, mutex);
218 intrusive_ptr_release(static_cast<sg4::Mutex*>(mutex->mutex));
221 int sthread_sem_init(sthread_sem_t* sem, int /*pshared*/, unsigned int value)
223 auto s = sg4::Semaphore::create(value);
224 intrusive_ptr_add_ref(s.get());
229 int sthread_sem_destroy(sthread_sem_t* sem)
231 intrusive_ptr_release(static_cast<sg4::Semaphore*>(sem->sem));
234 int sthread_sem_post(sthread_sem_t* sem)
236 static_cast<sg4::Semaphore*>(sem->sem)->release();
239 int sthread_sem_wait(sthread_sem_t* sem)
241 static_cast<sg4::Semaphore*>(sem->sem)->acquire();
244 int sthread_sem_trywait(sthread_sem_t* sem)
246 auto* s = static_cast<sg4::Semaphore*>(sem->sem);
247 if (s->would_block()) {
254 int sthread_sem_timedwait(sthread_sem_t* sem, const struct timespec* abs_timeout)
256 if (static_cast<sg4::Semaphore*>(sem->sem)->acquire_timeout(static_cast<double>(abs_timeout->tv_sec) +
257 static_cast<double>(abs_timeout->tv_nsec) / 1E9)) {
264 int sthread_gettimeofday(struct timeval* tv)
267 double now = simgrid::s4u::Engine::get_clock();
268 double secs = trunc(now);
269 double usecs = (now - secs) * 1e6;
270 tv->tv_sec = static_cast<time_t>(secs);
271 tv->tv_usec = static_cast<decltype(tv->tv_usec)>(usecs); // suseconds_t
276 void sthread_sleep(double seconds)
278 XBT_DEBUG("sleep(%lf)", seconds);
279 simgrid::s4u::this_actor::sleep_for(seconds);
283 int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr) {
284 *cond = sg_cond_init();
288 int pthread_cond_signal(pthread_cond_t *cond) {
289 sg_cond_notify_one(*cond);
293 int pthread_cond_broadcast(pthread_cond_t *cond) {
294 sg_cond_notify_all(*cond);
298 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) {
299 sg_cond_wait(*cond, *mutex);
303 int pthread_cond_destroy(pthread_cond_t *cond) {
304 sg_cond_destroy(*cond);