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) */
8 #include "simgrid/s4u/Barrier.hpp"
10 #include "xbt/asserts.h"
13 #include "xbt/string.hpp"
14 #include <simgrid/actor.h>
15 #include <simgrid/s4u/Actor.hpp>
16 #include <simgrid/s4u/Engine.hpp>
17 #include <simgrid/s4u/Mutex.hpp>
18 #include <simgrid/s4u/NetZone.hpp>
19 #include <simgrid/s4u/Semaphore.hpp>
21 #include <xbt/sysdep.h>
23 #include "src/internal_config.h"
24 #include "src/sthread/sthread.h"
29 #include <semaphore.h>
33 #include <string_view>
36 XBT_LOG_NEW_DEFAULT_CATEGORY(sthread, "pthread intercepter");
37 namespace sg4 = simgrid::s4u;
39 static sg4::Host* lilibeth = nullptr;
41 int sthread_main(int argc, char** argv, char** envp, int (*raw_main)(int, char**, char**))
43 /* Do not intercept the main when run from SMPI: it will initialize the simulation properly */
44 for (int i = 0; envp[i] != nullptr; i++)
45 if (std::string_view(envp[i]).rfind("SMPI_GLOBAL_SIZE", 0) == 0) {
46 printf("sthread refuses to intercept the SMPI application %s directly, as its interception is done otherwise.\n",
48 return raw_main(argc, argv, envp);
51 /* Do not intercept valgrind step 1 */
52 if (not strcmp(argv[0], "/usr/bin/valgrind.bin") || not strcmp(argv[0], "/bin/sh")|| not strcmp(argv[0], "/bin/bash")|| not strcmp(argv[0], "gdb")) {
53 printf("sthread refuses to intercept the execution of %s. Running the application unmodified.\n", argv[0]);
55 return raw_main(argc, argv, envp);
58 /* If not in SMPI, the old main becomes an actor in a newly created simulation */
59 printf("sthread is intercepting the execution of %s\n", argv[0]);
62 sg4::Engine e(&argc, argv);
63 auto* zone = sg4::create_full_zone("world");
64 lilibeth = zone->create_host("Lilibeth", 1e15);
67 /* Launch the user's main() on an actor */
69 sg4::ActorPtr main_actor = sg4::Actor::create("main thread", lilibeth, raw_main, argc, argv, envp);
71 sg4::Engine::get_instance()->run();
73 XBT_INFO("All threads exited. Terminating the simulation.");
78 struct sthread_mutex {
82 int sthread_create(unsigned long int* thread, const void* /*pthread_attr_t* attr*/, void* (*start_routine)(void*),
87 XBT_VERB("Create thread %d", TID);
88 std::string name = std::string("thread ") + std::to_string(TID);
90 if (SMPI_is_inited()) {
92 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
93 name = simgrid::xbt::string_printf("%d:%d", rank, TID);
96 sg4::ActorPtr actor = sg4::Actor::create(
98 [](auto* user_function, auto* param) {
100 if (SMPI_is_inited())
101 SMPI_thread_create();
104 user_function(param);
109 intrusive_ptr_add_ref(actor.get());
110 *thread = reinterpret_cast<unsigned long>(actor.get());
113 int sthread_join(sthread_t thread, void** /*retval*/)
115 sg4::ActorPtr actor(reinterpret_cast<sg4::Actor*>(thread));
117 intrusive_ptr_release(actor.get());
122 int sthread_mutexattr_init(sthread_mutexattr_t* attr)
124 memset(attr, 0, sizeof(*attr));
127 int sthread_mutexattr_settype(sthread_mutexattr_t* attr, int type)
130 case PTHREAD_MUTEX_NORMAL:
131 xbt_assert(not attr->recursive, "S4U does not allow to remove the recursivness of a mutex.");
134 case PTHREAD_MUTEX_RECURSIVE:
136 attr->errorcheck = 0; // reset
138 case PTHREAD_MUTEX_ERRORCHECK:
139 attr->errorcheck = 1;
147 int sthread_mutexattr_gettype(const sthread_mutexattr_t* attr, int* type)
150 *type = PTHREAD_MUTEX_RECURSIVE;
151 else if (attr->errorcheck)
152 *type = PTHREAD_MUTEX_ERRORCHECK;
154 *type = PTHREAD_MUTEX_NORMAL;
157 int sthread_mutexattr_getrobust(const sthread_mutexattr_t* attr, int* robustness)
159 *robustness = attr->robust;
162 int sthread_mutexattr_setrobust(sthread_mutexattr_t* attr, int robustness)
164 attr->robust = robustness;
170 int sthread_mutex_init(sthread_mutex_t* mutex, const sthread_mutexattr_t* attr)
172 auto m = sg4::Mutex::create(attr != nullptr && attr->recursive);
173 intrusive_ptr_add_ref(m.get());
175 mutex->mutex = m.get();
179 int sthread_mutex_lock(sthread_mutex_t* mutex)
181 /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */
182 if (mutex->mutex == nullptr)
183 sthread_mutex_init(mutex, nullptr);
185 XBT_DEBUG("%s(%p)", __func__, mutex);
186 static_cast<sg4::Mutex*>(mutex->mutex)->lock();
190 int sthread_mutex_trylock(sthread_mutex_t* mutex)
192 /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */
193 if (mutex->mutex == nullptr)
194 sthread_mutex_init(mutex, nullptr);
196 XBT_DEBUG("%s(%p)", __func__, mutex);
197 if (static_cast<sg4::Mutex*>(mutex->mutex)->try_lock())
202 int sthread_mutex_unlock(sthread_mutex_t* mutex)
204 /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */
205 if (mutex->mutex == nullptr)
206 sthread_mutex_init(mutex, nullptr);
208 XBT_DEBUG("%s(%p)", __func__, mutex);
209 static_cast<sg4::Mutex*>(mutex->mutex)->unlock();
212 int sthread_mutex_destroy(sthread_mutex_t* mutex)
214 /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */
215 if (mutex->mutex == nullptr)
216 sthread_mutex_init(mutex, nullptr);
218 XBT_DEBUG("%s(%p)", __func__, mutex);
219 intrusive_ptr_release(static_cast<sg4::Mutex*>(mutex->mutex));
223 int sthread_barrier_init(sthread_barrier_t* barrier, const sthread_barrierattr_t* attr, unsigned count){
224 auto b = sg4::Barrier::create(count);
225 intrusive_ptr_add_ref(b.get());
227 barrier->barrier = b.get();
230 int sthread_barrier_wait(sthread_barrier_t* barrier){
231 XBT_DEBUG("%s(%p)", __func__, barrier);
232 static_cast<sg4::Barrier*>(barrier->barrier)->wait();
235 int sthread_barrier_destroy(sthread_barrier_t* barrier){
236 XBT_DEBUG("%s(%p)", __func__, barrier);
237 intrusive_ptr_release(static_cast<sg4::Barrier*>(barrier->barrier));
241 int sthread_sem_init(sthread_sem_t* sem, int /*pshared*/, unsigned int value)
243 auto s = sg4::Semaphore::create(value);
244 intrusive_ptr_add_ref(s.get());
249 int sthread_sem_destroy(sthread_sem_t* sem)
251 intrusive_ptr_release(static_cast<sg4::Semaphore*>(sem->sem));
254 int sthread_sem_post(sthread_sem_t* sem)
256 static_cast<sg4::Semaphore*>(sem->sem)->release();
259 int sthread_sem_wait(sthread_sem_t* sem)
261 static_cast<sg4::Semaphore*>(sem->sem)->acquire();
264 int sthread_sem_trywait(sthread_sem_t* sem)
266 auto* s = static_cast<sg4::Semaphore*>(sem->sem);
267 if (s->would_block()) {
274 int sthread_sem_timedwait(sthread_sem_t* sem, const struct timespec* abs_timeout)
276 if (static_cast<sg4::Semaphore*>(sem->sem)->acquire_timeout(static_cast<double>(abs_timeout->tv_sec) +
277 static_cast<double>(abs_timeout->tv_nsec) / 1E9)) {
284 int sthread_gettimeofday(struct timeval* tv)
287 double now = simgrid::s4u::Engine::get_clock();
288 double secs = trunc(now);
289 double usecs = (now - secs) * 1e6;
290 tv->tv_sec = static_cast<time_t>(secs);
291 tv->tv_usec = static_cast<decltype(tv->tv_usec)>(usecs); // suseconds_t
296 unsigned int sthread_sleep(double seconds)
298 XBT_DEBUG("sleep(%lf)", seconds);
299 simgrid::s4u::this_actor::sleep_for(seconds);
302 int sthread_usleep(double seconds)
304 XBT_DEBUG("sleep(%lf)", seconds);
305 simgrid::s4u::this_actor::sleep_for(seconds);
310 int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr) {
311 *cond = sg_cond_init();
315 int pthread_cond_signal(pthread_cond_t *cond) {
316 sg_cond_notify_one(*cond);
320 int pthread_cond_broadcast(pthread_cond_t *cond) {
321 sg_cond_notify_all(*cond);
325 int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) {
326 sg_cond_wait(*cond, *mutex);
330 int pthread_cond_destroy(pthread_cond_t *cond) {
331 sg_cond_destroy(*cond);