target=examples
ncores=$(grep -c processor /proc/cpuinfo)
+halfcores=$(expr $ncores / 2 + 1)
install_path=$(sed -n 's/^CMAKE_INSTALL_PREFIX:PATH=//p' CMakeCache.txt)
if [ -e ${install_path} ] && [ -d ${install_path} ] && [ -x ${install_path} ] && [ -w ${install_path} ] ; then
(
echo "install_path: ${install_path}"
echo "Target: ${target}"
- echo "Cores: ${ncores}"
- (nice ${builder} -j${ncores} ${target} tests || make ${target} tests) && nice ctest -j${ncores} --output-on-failure ; date
+ echo "Cores to build: ${ncores}"
+ echo "Cores to test: ${halfcores}"
+ (nice ${builder} -j${ncores} ${target} tests || ${builder} ${target} tests) && nice ctest -j${halfcores} --output-on-failure ; date
) 2>&1 | tee BuildSimGrid.sh.log
file(WRITE ${PROJECT_BINARY_DIR}/Testing/Notes/Build "GIT version : ${GIT_VERSION}\n")
file(APPEND ${PROJECT_BINARY_DIR}/Testing/Notes/Build "Release : simgrid-${release_version}\n")
-INCLUDE(Dart)
#########################################################################
foreach(x
- mutex-simple
+ mutex-simple mutex-recursive
producer-consumer)
if("${CMAKE_SYSTEM}" MATCHES "Linux")
--- /dev/null
+# We ignore the LD_PRELOAD lines from the expected output because they contain the build path
+! ignore .*LD_PRELOAD.*
+
+$ $VALGRIND_NO_TRACE_CHILDREN ${bindir:=.}/../../bin/simgrid-mc --cfg=model-check/setenv:LD_PRELOAD=${libdir:=.}/libsgmalloc.so:${libdir:=.}/libsthread.so ${bindir:=.}/pthread-mutex-recursive
+> sthread is intercepting the execution of ./pthread-mutex-recursive
+> [0.000000] [mc_dfs/INFO] Start a DFS exploration. Reduction is: dpor.
+> Got the lock on the default mutex.
+> Failed to relock the default mutex.
+> Got the lock on the recursive mutex.
+> Got the lock again on the recursive mutex.
+> Got the lock on the default mutex.
+> Failed to relock the default mutex.
+> Got the lock on the recursive mutex.
+> Got the lock again on the recursive mutex.
+> [0.000000] [mc_dfs/INFO] DFS exploration ended. 17 unique states visited; 1 backtracks (3 transition replays, 21 states visited overall)
! ignore .*LD_PRELOAD.*
$ $VALGRIND_NO_TRACE_CHILDREN ${bindir:=.}/../../bin/simgrid-mc --cfg=model-check/setenv:LD_PRELOAD=${libdir:=.}/libsgmalloc.so:${libdir:=.}/libsthread.so ${bindir:=.}/pthread-mutex-simple
-> [0.000000] [sthread/INFO] Starting the simulation.
+> sthread is intercepting the execution of ./pthread-mutex-simple
> All threads are started.
> [0.000000] [mc_dfs/INFO] Start a DFS exploration. Reduction is: dpor.
> The thread 0 is terminating.
! ignore .*LD_PRELOAD.*
$ $VALGRIND_NO_TRACE_CHILDREN ${bindir:=.}/../../bin/simgrid-mc --cfg=model-check/setenv:LD_PRELOAD=${libdir:=.}/libsgmalloc.so:${libdir:=.}/libsthread.so ${bindir:=.}/pthread-mutex-simpledeadlock
-> [0.000000] [sthread/INFO] Starting the simulation.
+> sthread is intercepting the execution of ./pthread-mutex-simpledeadlock
> All threads are started.
> [0.000000] [mc_dfs/INFO] Start a DFS exploration. Reduction is: dpor.
> The thread 0 is terminating.
! ignore .*LD_PRELOAD.*
$ $VALGRIND_NO_TRACE_CHILDREN ${bindir:=.}/../../bin/simgrid-mc --cfg=model-check/setenv:LD_PRELOAD=${libdir:=.}/libsgmalloc.so:${libdir:=.}/libsthread.so ${bindir:=.}/pthread-producer-consumer -q -C 1 -P 1
-> [0.000000] [sthread/INFO] Starting the simulation.
+> sthread is intercepting the execution of ./pthread-producer-consumer
> [0.000000] [mc_dfs/INFO] Start a DFS exploration. Reduction is: dpor.
> [0.000000] [mc_dfs/INFO] DFS exploration ended. 786 unique states visited; 97 backtracks (2049 transition replays, 2932 states visited overall)
$ $VALGRIND_NO_TRACE_CHILDREN ${bindir:=.}/../../bin/simgrid-mc --cfg=model-check/reduction:sdpor --cfg=model-check/setenv:LD_PRELOAD=${libdir:=.}/libsgmalloc.so:${libdir:=.}/libsthread.so ${bindir:=.}/pthread-producer-consumer -q -C 1 -P 1
> [0.000000] [xbt_cfg/INFO] Configuration change: Set 'model-check/reduction' to 'sdpor'
-> [0.000000] [sthread/INFO] Starting the simulation.
+> sthread is intercepting the execution of ./pthread-producer-consumer
> [0.000000] [mc_dfs/INFO] Start a DFS exploration. Reduction is: sdpor.
> [0.000000] [mc_dfs/INFO] DFS exploration ended. 1186 unique states visited; 157 backtracks (3403 transition replays, 4746 states visited overall)
$ $VALGRIND_NO_TRACE_CHILDREN ${bindir:=.}/../../bin/simgrid-mc --cfg=model-check/reduction:odpor --cfg=model-check/setenv:LD_PRELOAD=${libdir:=.}/libsgmalloc.so:${libdir:=.}/libsthread.so ${bindir:=.}/pthread-producer-consumer -q -C 1 -P 1
> [0.000000] [xbt_cfg/INFO] Configuration change: Set 'model-check/reduction' to 'odpor'
-> [0.000000] [sthread/INFO] Starting the simulation.
+> sthread is intercepting the execution of ./pthread-producer-consumer
> [0.000000] [mc_dfs/INFO] Start a DFS exploration. Reduction is: odpor.
> [0.000000] [mc_dfs/INFO] DFS exploration ended. 39 unique states visited; 0 backtracks (0 transition replays, 39 states visited overall)
--- /dev/null
+$ env ASAN_OPTIONS=verify_asan_link_order=0:$ASAN_OPTIONS LD_PRELOAD=${libdir:=.}/libsthread.so ./pthread-mutex-recursive
+> sthread is intercepting the execution of ./pthread-mutex-recursive
+> Got the lock on the default mutex.
+> Failed to relock the default mutex.
+> Got the lock on the recursive mutex.
+> Got the lock again on the recursive mutex.
+> [0.000000] [sthread/INFO] All threads exited. Terminating the simulation.
+/* Copyright (c) 2002-2023. The SimGrid Team. All rights reserved. */
+
+/* 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. */
+
/* Simple test code with no bug */
#include <pthread.h>
$ env ASAN_OPTIONS=verify_asan_link_order=0:$ASAN_OPTIONS LD_PRELOAD=${libdir:=.}/libsthread.so ./pthread-mutex-simple
-> [0.000000] [sthread/INFO] Starting the simulation.
+> sthread is intercepting the execution of ./pthread-mutex-simple
> All threads are started.
> The thread 0 is terminating.
> The thread 1 is terminating.
+/* Copyright (c) 2002-2023. The SimGrid Team. All rights reserved. */
+
+/* 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. */
+
/* Simple test code that may deadlock:
Thread 1 locks mutex1 then mutex2 while thread 2 locks in reverse order.
$ env ASAN_OPTIONS=verify_asan_link_order=0:$ASAN_OPTIONS LD_PRELOAD=${libdir:=.}/libsthread.so ./pthread-producer-consumer
-> [0.000000] [sthread/INFO] Starting the simulation.
+> sthread is intercepting the execution of ./pthread-producer-consumer
> Producer 1: Insert Item 0 at 0
> Producer 2: Insert Item 0 at 1
> Consumer 1: Remove Item 0 from 0
> [0.000000] [sthread/INFO] All threads exited. Terminating the simulation.
$ env ASAN_OPTIONS=verify_asan_link_order=0:$ASAN_OPTIONS LD_PRELOAD=${libdir:=.}/libsthread.so ./pthread-producer-consumer -c 2 -C 1 -p 2 -P 1
-> [0.000000] [sthread/INFO] Starting the simulation.
+> sthread is intercepting the execution of ./pthread-producer-consumer
> Producer 1: Insert Item 0 at 0
> Consumer 1: Remove Item 0 from 0
> Producer 1: Insert Item 1 at 1
! ignore .*LD_PRELOAD.*
$ $VALGRIND_NO_TRACE_CHILDREN ${bindir:=.}/../../../bin/simgrid-mc --cfg=model-check/setenv:LD_PRELOAD=${libdir:=.}/libsthread.so ${bindir:=.}/stdobject "--log=root.fmt:[%11.6r]%e(%a@%h)%e%m%n" --log=no_loc
-> [ 0.000000] (maestro@) Starting the simulation.
+> sthread is intercepting the execution of ./stdobject
> starting two helpers...
> waiting for helpers to finish...
> [ 0.000000] (maestro@) Start a DFS exploration. Reduction is: dpor.
> v = { 1, 2, 3, 5, 8, 13, 21, 21, };
> [ 0.000000] (maestro@) thread 1 takes &v
> [ 0.000000] (maestro@) thread 2 takes &v
-> [ 0.000000] (maestro@) Unprotected concurent access to &v: thread 1 vs thread 2 (locations hidden because of --log=no_loc).
+> [ 0.000000] (maestro@) Unprotected concurent access to &v: thread 1 from 1 location vs thread 2 (locations hidden because of --log=no_loc).
> [ 0.000000] (maestro@) **************************
> [ 0.000000] (maestro@) *** PROPERTY NOT VALID ***
> [ 0.000000] (maestro@) **************************
$ ./sthread-mutex-simple
-> [0.000000] [sthread/INFO] Starting the simulation.
+> sthread is intercepting the execution of ./sthread-mutex-simple
> All threads are started.
> The thread 0 is terminating.
> The thread 1 is terminating.
class Simcall;
class SimcallObserver;
+class MutexObserver;
class ObjectAccessSimcallObserver;
class ObjectAccessSimcallItem;
} // namespace actor
namespace simgrid::s4u {
/** @brief A classical mutex, but blocking in the simulation world.
+ *
+ * S4U mutexes are not recursive. If an actor tries to lock the same object twice, it deadlocks with itself.
*
* @beginrst
* It is strictly impossible to use a real mutex, such as
public:
/** \static Constructs a new mutex */
- static MutexPtr create();
+ static MutexPtr create(bool recursive = false);
+
void lock();
void unlock();
bool try_lock();
py::class_<Mutex, MutexPtr>(m, "Mutex",
"A classical mutex, but blocking in the simulation world."
"See the C++ documentation for details.")
- .def(py::init<>(&Mutex::create), py::call_guard<py::gil_scoped_release>(), "Mutex constructor.")
+ .def(py::init<>(&Mutex::create), py::call_guard<py::gil_scoped_release>(),
+ "Mutex constructor (pass True as a parameter to get a recursive Mutex).", py::arg("recursive") = false)
.def("lock", &Mutex::lock, py::call_guard<py::gil_scoped_release>(), "Block until the mutex is acquired.")
.def("try_lock", &Mutex::try_lock, py::call_guard<py::gil_scoped_release>(),
"Try to acquire the mutex. Return true if the mutex was acquired, false otherwise.")
MutexAcquisitionImplPtr MutexImpl::lock_async(actor::ActorImpl* issuer)
{
- auto res = MutexAcquisitionImplPtr(new kernel::activity::MutexAcquisitionImpl(issuer, this), true);
-
- if (owner_ != nullptr) {
- /* Somebody is using the mutex; register the acquisition */
+ /* If the mutex is recursive */
+ if (is_recursive_) {
+ if (owner_ == issuer) {
+ recursive_depth++;
+ auto res = MutexAcquisitionImplPtr(new kernel::activity::MutexAcquisitionImpl(issuer, this), true);
+ res->grant();
+ return res;
+ } else if (owner_ == nullptr) { // Free
+ owner_ = issuer;
+ recursive_depth = 1;
+ auto res = MutexAcquisitionImplPtr(new kernel::activity::MutexAcquisitionImpl(issuer, this), true);
+ res->grant();
+ return res;
+ }
+
+ for (auto acq : ongoing_acquisitions_)
+ if (acq->get_issuer() == issuer) {
+ acq->recursive_depth_++;
+ return acq;
+ }
+
+ // Not yet in the ongoing acquisition list. Get in there
+ auto res = MutexAcquisitionImplPtr(new kernel::activity::MutexAcquisitionImpl(issuer, this), true);
ongoing_acquisitions_.push_back(res);
- } else {
+ return res;
+ }
+
+ // None-recursive mutex
+ auto res = MutexAcquisitionImplPtr(new kernel::activity::MutexAcquisitionImpl(issuer, this), true);
+ if (owner_ == nullptr) { // Lock is free, take it
owner_ = issuer;
+ recursive_depth = 1;
+ res->grant();
+ } else { // Somebody is using the mutex; register the acquisition
+ ongoing_acquisitions_.push_back(res);
}
return res;
}
*/
bool MutexImpl::try_lock(actor::ActorImpl* issuer)
{
- XBT_IN("(%p, %p)", this, issuer);
- if (owner_ != nullptr) {
- XBT_OUT();
- return false;
+ if (owner_ == issuer && is_recursive_) {
+ recursive_depth++;
+ return true;
}
+ if (owner_ != nullptr)
+ return false;
- owner_ = issuer;
- XBT_OUT();
+ owner_ = issuer;
return true;
}
xbt_assert(issuer == owner_, "Cannot release that mutex: you're not the owner. %s is (pid:%ld).",
owner_ != nullptr ? owner_->get_cname() : "(nobody)", owner_ != nullptr ? owner_->get_pid() : -1);
+ if (is_recursive_) {
+ recursive_depth--;
+ if (recursive_depth > 0) // Still owning the lock
+ return;
+ }
+
if (not ongoing_acquisitions_.empty()) {
/* Give the ownership to the first waiting actor */
auto acq = ongoing_acquisitions_.front();
ongoing_acquisitions_.pop_front();
owner_ = acq->get_issuer();
+ acq->grant();
+ recursive_depth = acq->recursive_depth_;
if (acq == owner_->waiting_synchro_)
acq->finish();
// else, the issuer is not blocked on this acquisition so no need to release it
class XBT_PUBLIC MutexAcquisitionImpl : public ActivityImpl_T<MutexAcquisitionImpl> {
actor::ActorImpl* issuer_ = nullptr;
MutexImpl* mutex_ = nullptr;
+ int recursive_depth_ = 1;
+ // TODO: use granted_ this instead of owner_ == self to test().
+ // This is mandatory to get double-lock on non-recursive locks to properly deadlock
+ bool granted_ = false;
+
+ friend MutexImpl;
public:
MutexAcquisitionImpl(actor::ActorImpl* issuer, MutexImpl* mutex) : issuer_(issuer), mutex_(mutex) {}
MutexImplPtr get_mutex() { return mutex_; }
actor::ActorImpl* get_issuer() { return issuer_; }
+ void grant() { granted_ = true; }
bool test(actor::ActorImpl* issuer = nullptr) override;
void wait_for(actor::ActorImpl* issuer, double timeout) override;
std::deque<MutexAcquisitionImplPtr> ongoing_acquisitions_;
static unsigned next_id_;
unsigned id_ = next_id_++;
+ bool is_recursive_ = false;
+ int recursive_depth = 0;
friend MutexAcquisitionImpl;
public:
- MutexImpl() : piface_(this) {}
+ MutexImpl(bool recursive = false) : piface_(this), is_recursive_(recursive) {}
MutexImpl(MutexImpl const&) = delete;
MutexImpl& operator=(MutexImpl const&) = delete;
*
* See @ref s4u_raii.
*/
-MutexPtr Mutex::create()
+MutexPtr Mutex::create(bool recursive)
{
- auto* mutex = new kernel::activity::MutexImpl();
+ auto* mutex = new kernel::activity::MutexImpl(recursive);
return MutexPtr(&mutex->mutex(), false);
}
/** Config Globals */
-XBT_PUBLIC_DATA int _sg_cfg_init_status;
+XBT_PUBLIC_DATA int _sg_cfg_init_status; /* 0: not inited; 1: config module inited; 2: root zone of platform created */
XBT_PUBLIC void sg_config_init(int* argc, char** argv);
XBT_PUBLIC void sg_config_finalize();
simgrid::kernel::actor::ActorImpl* owner = nullptr;
const char* file = nullptr;
int line = -1;
+ int recursive_depth = 0;
explicit ObjectOwner(simgrid::kernel::actor::ActorImpl* o) : owner(o) {}
};
[self, objaddr, objname, file, line]() -> bool {
XBT_INFO("%s takes %s", self->get_cname(), objname);
auto* ownership = get_owner(objaddr);
- if (ownership->owner != nullptr) {
+ if (ownership->owner == self) {
+ ownership->recursive_depth++;
+ return true;
+ } else if (ownership->owner != nullptr) {
auto msg = std::string("Unprotected concurent access to ") + objname + ": " + ownership->owner->get_name();
- if (not xbt_log_no_loc)
+ if (not xbt_log_no_loc) {
msg += simgrid::xbt::string_printf(" at %s:%d", ownership->file, ownership->line);
+ if (ownership->recursive_depth > 1) {
+ msg += simgrid::xbt::string_printf(" (and %d other locations)", ownership->recursive_depth - 1);
+ if (ownership->recursive_depth != 2)
+ msg += "s";
+ }
+ } else {
+ msg += simgrid::xbt::string_printf(" from %d location", ownership->recursive_depth);
+ if (ownership->recursive_depth != 1)
+ msg += "s";
+ }
msg += " vs " + self->get_name();
if (xbt_log_no_loc)
msg += std::string(" (locations hidden because of --log=no_loc).");
ownership->owner = self;
ownership->file = file;
ownership->line = line;
+ ownership->recursive_depth = 1;
return true;
},
&observer);
[self, objaddr, objname]() -> void {
XBT_INFO("%s releases %s", self->get_cname(), objname);
auto* ownership = get_owner(objaddr);
- xbt_assert(ownership->owner == self, "safety check failed: %s is not owner of the object it's releasing.",
- self->get_cname());
- ownership->owner = nullptr;
+ xbt_assert(ownership->owner == self,
+ "safety check failed: %s is not owner of the object it's releasing. That object owned by %s.",
+ self->get_cname(), (ownership->owner == nullptr ? "nobody" : ownership->owner->get_cname()));
+ ownership->recursive_depth--;
+ if (ownership->recursive_depth == 0)
+ ownership->owner = nullptr;
},
&observer);
sthread_enable();
static int (*raw_mutex_unlock)(pthread_mutex_t*);
static int (*raw_mutex_destroy)(pthread_mutex_t*);
+static int (*raw_pthread_mutexattr_init)(pthread_mutexattr_t*);
+static int (*raw_pthread_mutexattr_settype)(pthread_mutexattr_t*, int);
+static int (*raw_pthread_mutexattr_gettype)(const pthread_mutexattr_t* restrict, int* restrict);
+static int (*raw_pthread_mutexattr_getrobust)(const pthread_mutexattr_t*, int*);
+static int (*raw_pthread_mutexattr_setrobust)(pthread_mutexattr_t*, int);
+
static unsigned int (*raw_sleep)(unsigned int);
static int (*raw_usleep)(useconds_t);
static int (*raw_gettimeofday)(struct timeval*, void*);
raw_mutex_unlock = dlsym(RTLD_NEXT, "pthread_mutex_unlock");
raw_mutex_destroy = dlsym(RTLD_NEXT, "pthread_mutex_destroy");
+ raw_pthread_mutexattr_init = dlsym(RTLD_NEXT, "pthread_mutexattr_init");
+ raw_pthread_mutexattr_settype = dlsym(RTLD_NEXT, "pthread_mutexattr_settype");
+ raw_pthread_mutexattr_gettype = dlsym(RTLD_NEXT, "pthread_mutexattr_gettype");
+ raw_pthread_mutexattr_getrobust = dlsym(RTLD_NEXT, "pthread_mutexattr_getrobust");
+ raw_pthread_mutexattr_setrobust = dlsym(RTLD_NEXT, "pthread_mutexattr_setrobust");
+
raw_sleep = dlsym(RTLD_NEXT, "sleep");
raw_usleep = dlsym(RTLD_NEXT, "usleep");
raw_gettimeofday = dlsym(RTLD_NEXT, "gettimeofday");
sthread_enable();
return res;
}
+
+#define _STHREAD_CONCAT(a, b) a##b
+#define intercepted_call(name, raw_params, call_params, sim_params) \
+ int _STHREAD_CONCAT(pthread_, name) raw_params \
+ { \
+ if (_STHREAD_CONCAT(raw_pthread_, name) == NULL) \
+ intercepter_init(); \
+ if (sthread_inside_simgrid) \
+ return _STHREAD_CONCAT(raw_pthread_, name) call_params; \
+ \
+ sthread_disable(); \
+ int res = _STHREAD_CONCAT(sthread_, name) sim_params; \
+ sthread_enable(); \
+ return res; \
+ }
+
+intercepted_call(mutexattr_init, (pthread_mutexattr_t * attr), (attr), ((sthread_mutexattr_t*)attr));
+intercepted_call(mutexattr_settype, (pthread_mutexattr_t * attr, int type), (attr, type),
+ ((sthread_mutexattr_t*)attr, type));
+intercepted_call(mutexattr_gettype, (const pthread_mutexattr_t* restrict attr, int* type), (attr, type),
+ ((sthread_mutexattr_t*)attr, type));
+intercepted_call(mutexattr_setrobust, (pthread_mutexattr_t* restrict attr, int robustness), (attr, robustness),
+ ((sthread_mutexattr_t*)attr, robustness));
+intercepted_call(mutexattr_getrobust, (const pthread_mutexattr_t* restrict attr, int* restrict robustness),
+ (attr, robustness), ((sthread_mutexattr_t*)attr, robustness));
+
int pthread_join(pthread_t thread, void** retval)
{
if (raw_pthread_join == NULL)
return raw_mutex_init(mutex, attr);
sthread_disable();
- int res = sthread_mutex_init((sthread_mutex_t*)mutex, attr);
+ int res = sthread_mutex_init((sthread_mutex_t*)mutex, (sthread_mutexattr_t*)attr);
sthread_enable();
return res;
}
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 {
+ unsigned recursive : 1;
+ unsigned errorcheck : 1;
+ unsigned robust : 1;
+} sthread_mutexattr_t;
+
+int sthread_mutexattr_init(sthread_mutexattr_t* attr);
+int sthread_mutexattr_settype(sthread_mutexattr_t* attr, int type);
+int sthread_mutexattr_gettype(const sthread_mutexattr_t* attr, int* type);
+int sthread_mutexattr_getrobust(const sthread_mutexattr_t* attr, int* robustness);
+int sthread_mutexattr_setrobust(sthread_mutexattr_t* attr, int robustness);
+
typedef struct {
void* mutex;
} sthread_mutex_t;
-int sthread_mutex_init(sthread_mutex_t* mutex, const /*pthread_mutexattr_t*/ void* attr);
+int sthread_mutex_init(sthread_mutex_t* mutex, const sthread_mutexattr_t* attr);
int sthread_mutex_lock(sthread_mutex_t* mutex);
int sthread_mutex_trylock(sthread_mutex_t* mutex);
int sthread_mutex_unlock(sthread_mutex_t* mutex);
/* SimGrid's pthread interposer. Actual implementation of the symbols (see the comment in sthread.h) */
#include "smpi/smpi.h"
+#include "xbt/asserts.h"
+#include "xbt/ex.h"
+#include "xbt/log.h"
#include "xbt/string.hpp"
#include <simgrid/actor.h>
#include <simgrid/s4u/Actor.hpp>
{
/* Do not intercept the main when run from SMPI: it will initialize the simulation properly */
for (int i = 0; envp[i] != nullptr; i++)
- if (std::string_view(envp[i]).rfind("SMPI_GLOBAL_SIZE", 0) == 0)
+ if (std::string_view(envp[i]).rfind("SMPI_GLOBAL_SIZE", 0) == 0) {
+ printf("sthread refuses to intercept the SMPI application %s directly, as its interception is done otherwise.\n",
+ argv[0]);
return raw_main(argc, argv, envp);
+ }
- /* If not in SMPI, the old main becomes an actor in a newly created simulation */
- std::ostringstream id;
- id << std::this_thread::get_id();
+ /* Do not intercept valgrind step 1 */
+ if (not strcmp(argv[0], "/usr/bin/valgrind.bin") || not strcmp(argv[0], "/bin/sh")) {
+ printf("sthread refuses to intercept the execution of %s. Running the application unmodified.\n", argv[0]);
+ fflush(stdout);
+ return raw_main(argc, argv, envp);
+ }
- XBT_DEBUG("sthread main() is starting in thread %s", id.str().c_str());
+ /* If not in SMPI, the old main becomes an actor in a newly created simulation */
+ printf("sthread is intercepting the execution of %s\n", argv[0]);
+ fflush(stdout);
sg4::Engine e(&argc, argv);
auto* zone = sg4::create_full_zone("world");
sthread_enable();
sg4::ActorPtr main_actor = sg4::Actor::create("main thread", lilibeth, raw_main, argc, argv, envp);
- XBT_INFO("Starting the simulation.");
sg4::Engine::get_instance()->run();
sthread_disable();
XBT_INFO("All threads exited. Terminating the simulation.");
return 0;
}
-int sthread_mutex_init(sthread_mutex_t* mutex, const void* /*pthread_mutexattr_t* attr*/)
+int sthread_mutexattr_init(sthread_mutexattr_t* attr)
+{
+ memset(attr, 0, sizeof(*attr));
+ return 0;
+}
+int sthread_mutexattr_settype(sthread_mutexattr_t* attr, int type)
{
- auto m = sg4::Mutex::create();
+ switch (type) {
+ case PTHREAD_MUTEX_NORMAL:
+ xbt_assert(not attr->recursive, "S4U does not allow to remove the recursivness of a mutex.");
+ attr->recursive = 0;
+ break;
+ case PTHREAD_MUTEX_RECURSIVE:
+ attr->recursive = 1;
+ attr->errorcheck = 0; // reset
+ break;
+ case PTHREAD_MUTEX_ERRORCHECK:
+ attr->errorcheck = 1;
+ THROW_UNIMPLEMENTED;
+ break;
+ default:
+ THROW_IMPOSSIBLE;
+ }
+ return 0;
+}
+int sthread_mutexattr_gettype(const sthread_mutexattr_t* attr, int* type)
+{
+ if (attr->recursive)
+ *type = PTHREAD_MUTEX_RECURSIVE;
+ else if (attr->errorcheck)
+ *type = PTHREAD_MUTEX_ERRORCHECK;
+ else
+ *type = PTHREAD_MUTEX_NORMAL;
+ return 0;
+}
+int sthread_mutexattr_getrobust(const sthread_mutexattr_t* attr, int* robustness)
+{
+ *robustness = attr->robust;
+ return 0;
+}
+int sthread_mutexattr_setrobust(sthread_mutexattr_t* attr, int robustness)
+{
+ attr->robust = robustness;
+ if (robustness)
+ THROW_UNIMPLEMENTED;
+ return 0;
+}
+
+int sthread_mutex_init(sthread_mutex_t* mutex, const sthread_mutexattr_t* attr)
+{
+ auto m = sg4::Mutex::create(attr != nullptr && attr->recursive);
intrusive_ptr_add_ref(m.get());
mutex->mutex = m.get();
if (mutex->mutex == nullptr)
sthread_mutex_init(mutex, nullptr);
+ XBT_DEBUG("%s(%p)", __FUNCTION__, mutex);
static_cast<sg4::Mutex*>(mutex->mutex)->lock();
return 0;
}
if (mutex->mutex == nullptr)
sthread_mutex_init(mutex, nullptr);
- return static_cast<sg4::Mutex*>(mutex->mutex)->try_lock();
+ XBT_DEBUG("%s(%p)", __FUNCTION__, mutex);
+ if (static_cast<sg4::Mutex*>(mutex->mutex)->try_lock())
+ return 0;
+ return EBUSY;
}
int sthread_mutex_unlock(sthread_mutex_t* mutex)
if (mutex->mutex == nullptr)
sthread_mutex_init(mutex, nullptr);
+ XBT_DEBUG("%s(%p)", __FUNCTION__, mutex);
static_cast<sg4::Mutex*>(mutex->mutex)->unlock();
return 0;
}
if (mutex->mutex == nullptr)
sthread_mutex_init(mutex, nullptr);
+ XBT_DEBUG("%s(%p)", __FUNCTION__, mutex);
intrusive_ptr_release(static_cast<sg4::Mutex*>(mutex->mutex));
return 0;
}