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"
9 #include "simgrid/s4u/ConditionVariable.hpp"
10 #include "smpi/smpi.h"
11 #include "xbt/asserts.h"
14 #include "xbt/string.hpp"
15 #include <simgrid/actor.h>
16 #include <simgrid/s4u/Actor.hpp>
17 #include <simgrid/s4u/Engine.hpp>
18 #include <simgrid/s4u/Mutex.hpp>
19 #include <simgrid/s4u/NetZone.hpp>
20 #include <simgrid/s4u/Semaphore.hpp>
22 #include <xbt/sysdep.h>
24 #include "src/internal_config.h"
25 #include "src/sthread/sthread.h"
30 #include <semaphore.h>
34 #include <string_view>
37 XBT_LOG_NEW_DEFAULT_CATEGORY(sthread, "pthread intercepter");
38 namespace sg4 = simgrid::s4u;
40 static sg4::Host* lilibeth = nullptr;
42 int sthread_main(int argc, char** argv, char** envp, int (*raw_main)(int, char**, char**))
44 /* Do not intercept the main when run from SMPI: it will initialize the simulation properly */
45 for (int i = 0; envp[i] != nullptr; i++)
46 if (std::string_view(envp[i]).rfind("SMPI_GLOBAL_SIZE", 0) == 0) {
47 printf("sthread refuses to intercept the SMPI application %s directly, as its interception is done otherwise.\n",
49 return raw_main(argc, argv, envp);
52 /* Do not intercept system binaries such as valgrind step 1 */
53 std::vector<std::string> binaries = {"/usr/bin/valgrind.bin", "/bin/sh", "/bin/bash", "gdb", "addr2line"};
54 for (int i = 0; envp[i] != nullptr; i++) {
55 auto view = std::string_view(envp[i]);
56 /* If you want to ignore more than one binary, export STHREAD_IGNORE_BINARY1=toto STHREAD_IGNORE_BINARY2=tutu */
57 /* Note that this cannot be configured with --cfg because we are before the main() */
58 if (view.rfind("STHREAD_IGNORE_BINARY", 0) == 0) {
59 view.remove_prefix(std::min(view.rfind("=") + 1, view.size()));
60 binaries.push_back(std::string(view));
63 auto binary_view = std::string_view(argv[0]);
64 for (auto binary : binaries) {
65 if (binary_view.rfind(binary) != std::string_view::npos) {
66 printf("sthread refuses to intercept the execution of %s. Running the application unmodified.\n", argv[0]);
68 return raw_main(argc, argv, envp);
72 /* If not in SMPI, the old main becomes an actor in a newly created simulation */
73 printf("sthread is intercepting the execution of %s. If it's not what you want, export STHREAD_IGNORE_BINARY=%s\n",
77 sg4::Engine e(&argc, argv);
78 auto* zone = sg4::create_full_zone("world");
79 lilibeth = zone->create_host("Lilibeth", 1e15);
82 /* Launch the user's main() on an actor */
83 const SThreadGuard sthread_guard;
84 sg4::ActorPtr main_actor = sg4::Actor::create("main thread", lilibeth, raw_main, argc, argv, envp);
86 sg4::Engine::get_instance()->run();
87 XBT_INFO("All threads exited. Terminating the simulation.");
92 struct sthread_mutex {
96 int sthread_create(unsigned long int* thread, const void* /*pthread_attr_t* attr*/, void* (*start_routine)(void*),
101 XBT_VERB("Create thread %d", TID);
102 std::string name = std::string("thread ") + std::to_string(TID);
104 if (SMPI_is_inited()) {
106 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
107 name = simgrid::xbt::string_printf("%d:%d", rank, TID);
110 sg4::ActorPtr actor = sg4::Actor::create(
112 [](auto* user_function, auto* param) {
114 if (SMPI_is_inited())
115 SMPI_thread_create();
117 const SThreadGuard sthread_guard;
118 user_function(param);
122 intrusive_ptr_add_ref(actor.get());
123 *thread = reinterpret_cast<unsigned long>(actor.get());
126 int sthread_join(sthread_t thread, void** /*retval*/)
128 sg4::ActorPtr actor(reinterpret_cast<sg4::Actor*>(thread));
130 intrusive_ptr_release(actor.get());
135 int sthread_mutexattr_init(sthread_mutexattr_t* attr)
137 memset(attr, 0, sizeof(*attr));
140 int sthread_mutexattr_settype(sthread_mutexattr_t* attr, int type)
143 case PTHREAD_MUTEX_NORMAL:
144 xbt_assert(not attr->recursive, "S4U does not allow to remove the recursivness of a mutex.");
147 case PTHREAD_MUTEX_RECURSIVE:
149 attr->errorcheck = 0; // reset
151 case PTHREAD_MUTEX_ERRORCHECK:
152 attr->errorcheck = 1;
160 int sthread_mutexattr_gettype(const sthread_mutexattr_t* attr, int* type)
163 *type = PTHREAD_MUTEX_RECURSIVE;
164 else if (attr->errorcheck)
165 *type = PTHREAD_MUTEX_ERRORCHECK;
167 *type = PTHREAD_MUTEX_NORMAL;
170 int sthread_mutexattr_getrobust(const sthread_mutexattr_t* attr, int* robustness)
172 *robustness = attr->robust;
175 int sthread_mutexattr_setrobust(sthread_mutexattr_t* attr, int robustness)
177 attr->robust = robustness;
183 int sthread_mutex_init(sthread_mutex_t* mutex, const sthread_mutexattr_t* attr)
185 auto m = sg4::Mutex::create(attr != nullptr && attr->recursive);
186 intrusive_ptr_add_ref(m.get());
188 mutex->mutex = m.get();
192 int sthread_mutex_lock(sthread_mutex_t* mutex)
194 /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */
195 if (mutex->mutex == nullptr)
196 sthread_mutex_init(mutex, nullptr);
198 XBT_DEBUG("%s(%p)", __func__, mutex);
199 static_cast<sg4::Mutex*>(mutex->mutex)->lock();
203 int sthread_mutex_trylock(sthread_mutex_t* mutex)
205 /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */
206 if (mutex->mutex == nullptr)
207 sthread_mutex_init(mutex, nullptr);
209 XBT_DEBUG("%s(%p)", __func__, mutex);
210 if (static_cast<sg4::Mutex*>(mutex->mutex)->try_lock())
215 int sthread_mutex_unlock(sthread_mutex_t* mutex)
217 /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */
218 if (mutex->mutex == nullptr)
219 sthread_mutex_init(mutex, nullptr);
221 XBT_DEBUG("%s(%p)", __func__, mutex);
222 static_cast<sg4::Mutex*>(mutex->mutex)->unlock();
225 int sthread_mutex_destroy(sthread_mutex_t* mutex)
227 /* At least in glibc, PTHREAD_STATIC_INITIALIZER sets every fields to 0 */
228 if (mutex->mutex == nullptr)
229 sthread_mutex_init(mutex, nullptr);
231 XBT_DEBUG("%s(%p)", __func__, mutex);
232 intrusive_ptr_release(static_cast<sg4::Mutex*>(mutex->mutex));
236 int sthread_barrier_init(sthread_barrier_t* barrier, const sthread_barrierattr_t* attr, unsigned count){
237 auto b = sg4::Barrier::create(count);
238 intrusive_ptr_add_ref(b.get());
240 barrier->barrier = b.get();
243 int sthread_barrier_wait(sthread_barrier_t* barrier){
244 XBT_DEBUG("%s(%p)", __func__, barrier);
245 static_cast<sg4::Barrier*>(barrier->barrier)->wait();
248 int sthread_barrier_destroy(sthread_barrier_t* barrier){
249 XBT_DEBUG("%s(%p)", __func__, barrier);
250 intrusive_ptr_release(static_cast<sg4::Barrier*>(barrier->barrier));
254 int sthread_cond_init(sthread_cond_t* cond, sthread_condattr_t* attr)
256 auto cv = sg4::ConditionVariable::create();
257 intrusive_ptr_add_ref(cv.get());
259 cond->cond = cv.get();
260 cond->mutex = nullptr;
263 int sthread_cond_signal(sthread_cond_t* cond)
265 XBT_DEBUG("%s(%p)", __func__, cond);
267 if (cond->mutex == nullptr)
268 XBT_WARN("No mutex was associated so far with condition variable %p. Safety checks skipped.", cond);
270 auto* owner = static_cast<sg4::Mutex*>(cond->mutex)->get_owner();
271 if (owner == nullptr)
272 XBT_WARN("The mutex associated to condition %p is not currently owned by anyone when calling "
273 "pthread_cond_signal(). The signal could get lost.",
275 else if (owner != simgrid::s4u::Actor::self())
276 XBT_WARN("The mutex associated to condition %p is currently owned by %s, not by the thread currently calling "
277 "calling pthread_cond_signal(). The signal could get lost.",
278 cond, owner->get_cname());
281 static_cast<sg4::ConditionVariable*>(cond->cond)->notify_one();
284 int sthread_cond_broadcast(sthread_cond_t* cond)
286 XBT_DEBUG("%s(%p)", __func__, cond);
288 if (cond->mutex == nullptr)
289 XBT_WARN("No mutex was associated so far with condition variable %p. Safety checks skipped.", cond);
291 auto* owner = static_cast<sg4::Mutex*>(cond->mutex)->get_owner();
292 if (owner == nullptr)
293 XBT_WARN("The mutex associated to condition %p is not currently owned by anyone when calling "
294 "pthread_cond_broadcast(). The signal could get lost.",
296 else if (owner != simgrid::s4u::Actor::self())
297 XBT_WARN("The mutex associated to condition %p is currently owned by %s, not by the thread currently calling "
298 "calling pthread_cond_broadcast(). The signal could get lost.",
299 cond, owner->get_cname());
302 static_cast<sg4::ConditionVariable*>(cond->cond)->notify_all();
305 int sthread_cond_wait(sthread_cond_t* cond, sthread_mutex_t* mutex)
307 XBT_DEBUG("%s(%p)", __func__, cond);
309 if (cond->mutex == nullptr)
310 cond->mutex = mutex->mutex;
311 else if (cond->mutex != mutex->mutex)
312 XBT_WARN("The condition %p is now waited with mutex %p while it was previoulsy waited with mutex %p. sthread may "
313 "not work with such a dangerous code.",
314 cond, cond->mutex, mutex->mutex);
316 static_cast<sg4::ConditionVariable*>(cond->cond)->wait(static_cast<sg4::Mutex*>(mutex->mutex));
319 int sthread_cond_timedwait(sthread_cond_t* cond, sthread_mutex_t* mutex, const struct timespec* abs_timeout)
321 XBT_DEBUG("%s(%p)", __func__, cond);
323 if (cond->mutex == nullptr)
324 cond->mutex = mutex->mutex;
325 else if (cond->mutex != mutex->mutex)
326 XBT_WARN("The condition %p is now waited with mutex %p while it was previoulsy waited with mutex %p. sthread may "
327 "not work with such a dangerous code.",
328 cond, cond->mutex, mutex->mutex);
332 int sthread_cond_destroy(sthread_cond_t* cond)
334 XBT_DEBUG("%s(%p)", __func__, cond);
335 intrusive_ptr_release(static_cast<sg4::ConditionVariable*>(cond->cond));
339 int sthread_sem_init(sthread_sem_t* sem, int /*pshared*/, unsigned int value)
341 auto s = sg4::Semaphore::create(value);
342 intrusive_ptr_add_ref(s.get());
347 int sthread_sem_destroy(sthread_sem_t* sem)
349 intrusive_ptr_release(static_cast<sg4::Semaphore*>(sem->sem));
352 int sthread_sem_post(sthread_sem_t* sem)
354 static_cast<sg4::Semaphore*>(sem->sem)->release();
357 int sthread_sem_wait(sthread_sem_t* sem)
359 static_cast<sg4::Semaphore*>(sem->sem)->acquire();
362 int sthread_sem_trywait(sthread_sem_t* sem)
364 auto* s = static_cast<sg4::Semaphore*>(sem->sem);
365 if (s->would_block()) {
372 int sthread_sem_timedwait(sthread_sem_t* sem, const struct timespec* abs_timeout)
374 if (static_cast<sg4::Semaphore*>(sem->sem)->acquire_timeout(static_cast<double>(abs_timeout->tv_sec) +
375 static_cast<double>(abs_timeout->tv_nsec) / 1E9)) {
382 int sthread_gettimeofday(struct timeval* tv)
385 double now = simgrid::s4u::Engine::get_clock();
386 double secs = trunc(now);
387 double usecs = (now - secs) * 1e6;
388 tv->tv_sec = static_cast<time_t>(secs);
389 tv->tv_usec = static_cast<decltype(tv->tv_usec)>(usecs); // suseconds_t
394 unsigned int sthread_sleep(double seconds)
396 XBT_DEBUG("sleep(%lf)", seconds);
397 simgrid::s4u::this_actor::sleep_for(seconds);
400 int sthread_usleep(double seconds)
402 XBT_DEBUG("sleep(%lf)", seconds);
403 simgrid::s4u::this_actor::sleep_for(seconds);