From 358e5659801b6e0128467478bb138d2c7dd54c25 Mon Sep 17 00:00:00 2001 From: Martin Quinson Date: Sat, 25 Jun 2022 12:02:58 +0200 Subject: [PATCH] tentative of implementing pthread_join, and to ensure that we are inside simgrid once the context stops --- examples/sthread/pthread-mutex-simple.c | 5 +++-- src/kernel/context/ContextSwapped.cpp | 5 +++++ src/sthread/sthread.c | 20 ++++++++++++++++---- src/sthread/sthread.h | 1 + src/sthread/sthread_impl.cpp | 8 ++++++++ src/xbt/log.cpp | 1 - src/xbt/xbt_main.cpp | 2 ++ 7 files changed, 35 insertions(+), 7 deletions(-) diff --git a/examples/sthread/pthread-mutex-simple.c b/examples/sthread/pthread-mutex-simple.c index f39de639fd..abe582d5f0 100644 --- a/examples/sthread/pthread-mutex-simple.c +++ b/examples/sthread/pthread-mutex-simple.c @@ -30,8 +30,9 @@ int main(int argc, char* argv[]) pthread_create(&thread1, NULL, thread1_fun, NULL); fprintf(stderr, "here\n"); pthread_create(&thread2, NULL, thread2_fun, NULL); - // pthread_join(thread1, NULL); - // pthread_join(thread2, NULL); + fprintf(stderr, "there\n"); + pthread_join(thread1, NULL); + pthread_join(thread2, NULL); fprintf(stderr, "User main is done\n"); return 0; diff --git a/src/kernel/context/ContextSwapped.cpp b/src/kernel/context/ContextSwapped.cpp index c6812f4bce..92e9c7f715 100644 --- a/src/kernel/context/ContextSwapped.cpp +++ b/src/kernel/context/ContextSwapped.cpp @@ -8,6 +8,7 @@ #include "src/internal_config.h" #include "src/kernel/EngineImpl.hpp" #include "src/kernel/actor/ActorImpl.hpp" +#include "src/sthread/sthread.h" // sthread_inside_simgrid #include "xbt/parmap.hpp" #include "src/kernel/context/ContextSwapped.hpp" @@ -48,11 +49,15 @@ void smx_ctx_wrapper(simgrid::kernel::context::SwappedContext* context) __sanitizer_finish_switch_fiber(nullptr, &context->asan_ctx_->asan_stack_, &context->asan_ctx_->asan_stack_size_); #endif try { + sthread_inside_simgrid = 0; (*context)(); + sthread_inside_simgrid = 1; context->stop(); } catch (simgrid::ForcefulKillException const&) { + sthread_inside_simgrid = 1; XBT_DEBUG("Caught a ForcefulKillException"); } catch (simgrid::Exception const& e) { + sthread_inside_simgrid = 1; XBT_INFO("Actor killed by an uncaught exception %s", boost::core::demangle(typeid(e).name()).c_str()); throw; } diff --git a/src/sthread/sthread.c b/src/sthread/sthread.c index b9934341b2..7724bcf8bf 100644 --- a/src/sthread/sthread.c +++ b/src/sthread/sthread.c @@ -7,10 +7,9 @@ #include #include -int sthread_inside_simgrid = 1; // whether sthread should leave pthread operations, or intercept them. - /* We don't want to intercept pthread within simgrid. Instead we should provide the real implem to simgrid */ static int (*raw_pthread_create)(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*); +static int (*raw_pthread_join)(pthread_t, void**); static int (*raw_mutex_init)(pthread_mutex_t*, const pthread_mutexattr_t*) = NULL; static int (*raw_mutex_lock)(pthread_mutex_t*) = NULL; static int (*raw_mutex_trylock)(pthread_mutex_t*) = NULL; @@ -22,8 +21,8 @@ static int (*raw_sem_wait)(sem_t*) = NUL static int (*raw_sem_post)(sem_t*) = NULL; static void intercepter_init() { - raw_pthread_create = - (int (*)(pthread_t*, const pthread_attr_t*, void* (*)(void*), void*))dlsym(RTLD_NEXT, "pthread_create"); + raw_pthread_create = (typeof(raw_pthread_create))dlsym(RTLD_NEXT, "pthread_create"); + raw_pthread_join = (typeof(raw_pthread_join))dlsym(RTLD_NEXT, "pthread_join"); raw_mutex_init = (int (*)(pthread_mutex_t*, const pthread_mutexattr_t*))dlsym(RTLD_NEXT, "pthread_mutex_init"); raw_mutex_lock = (int (*)(pthread_mutex_t*))dlsym(RTLD_NEXT, "pthread_mutex_lock"); raw_mutex_trylock = (int (*)(pthread_mutex_t*))dlsym(RTLD_NEXT, "pthread_mutex_trylock"); @@ -49,6 +48,19 @@ int pthread_create(pthread_t* thread, const pthread_attr_t* attr, void* (*start_ sthread_inside_simgrid = 0; return res; } +int pthread_join(pthread_t thread, void** retval) +{ + if (raw_pthread_join == NULL) + intercepter_init(); + + if (sthread_inside_simgrid) + return raw_pthread_join(thread, retval); + + sthread_inside_simgrid = 1; + int res = sthread_join(thread, retval); + sthread_inside_simgrid = 0; + return res; +} int pthread_mutex_init(pthread_mutex_t* mutex, const pthread_mutexattr_t* attr) { diff --git a/src/sthread/sthread.h b/src/sthread/sthread.h index 2099432293..6e7d4f8962 100644 --- a/src/sthread/sthread.h +++ b/src/sthread/sthread.h @@ -17,6 +17,7 @@ int sthread_main(int argc, char** argv, char** envp, int (*raw_main)(int, char** typedef unsigned long int sthread_t; int sthread_create(sthread_t* thread, const /*pthread_attr_t*/ void* attr, void* (*start_routine)(void*), void* arg); +int sthread_join(sthread_t thread, void** retval); typedef struct { void* mutex; diff --git a/src/sthread/sthread_impl.cpp b/src/sthread/sthread_impl.cpp index 8dce02fb65..f2afe1eb32 100644 --- a/src/sthread/sthread_impl.cpp +++ b/src/sthread/sthread_impl.cpp @@ -81,6 +81,14 @@ int sthread_create(unsigned long int* thread, const /*pthread_attr_t*/ void* att *thread = reinterpret_cast(actor.get()); return 0; } +int sthread_join(sthread_t thread, void** retval) +{ + sg4::ActorPtr actor(reinterpret_cast(thread)); + actor->join(); + intrusive_ptr_release(actor.get()); + + return 0; +} int sthread_mutex_init(sthread_mutex_t* mutex, const /*pthread_mutexattr_t*/ void* attr) { diff --git a/src/xbt/log.cpp b/src/xbt/log.cpp index 11681b205a..916c77baf1 100644 --- a/src/xbt/log.cpp +++ b/src/xbt/log.cpp @@ -5,7 +5,6 @@ /* This program is free software; you can redistribute it and/or modify it * under the terms of the license (GNU LGPL) which comes with this package. */ -#include "src/sthread/sthread.h" // sthread_inside_simgrid #include "src/xbt/log_private.hpp" #include "xbt/string.hpp" #include "xbt/sysdep.h" diff --git a/src/xbt/xbt_main.cpp b/src/xbt/xbt_main.cpp index d9e473e749..194479b179 100644 --- a/src/xbt/xbt_main.cpp +++ b/src/xbt/xbt_main.cpp @@ -41,6 +41,8 @@ std::string binary_name; /* Name of the system process containing us (m std::vector cmdline; /* all we got in argv */ } // namespace simgrid::xbt +int sthread_inside_simgrid = 1; // whether sthread should leave pthread operations or intercept them. + int xbt_initialized = 0; simgrid::config::Flag cfg_dbg_clean_atexit{ "debug/clean-atexit", -- 2.20.1