X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/96cedde3cdbc0b8ffc3f096a1b65d021b0226f99..35bf365de6be434aeeafa8b3d5f5c9a1e3082ce3:/src/kernel/activity/MutexImpl.hpp diff --git a/src/kernel/activity/MutexImpl.hpp b/src/kernel/activity/MutexImpl.hpp index 613d799477..cad25c34c9 100644 --- a/src/kernel/activity/MutexImpl.hpp +++ b/src/kernel/activity/MutexImpl.hpp @@ -1,34 +1,92 @@ -/* Copyright (c) 2012-2019. The SimGrid Team. All rights reserved. */ +/* Copyright (c) 2012-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. */ -#ifndef SIMIX_MUTEXIMPL_HPP -#define SIMIX_MUTEXIMPL_HPP +#ifndef SIMGRID_KERNEL_ACTIVITY_MUTEX_HPP +#define SIMGRID_KERNEL_ACTIVITY_MUTEX_HPP -#include "simgrid/s4u/ConditionVariable.hpp" -#include "src/simix/ActorImpl.hpp" +#include "simgrid/s4u/Mutex.hpp" +#include "src/kernel/activity/ActivityImpl.hpp" +#include "src/kernel/actor/ActorImpl.hpp" +#include "xbt/asserts.h" #include -namespace simgrid { -namespace kernel { -namespace activity { +namespace simgrid::kernel::activity { + +/** Mutex Acquisition: the act / process of acquiring the mutex. + * + * You can declare some interest on a mutex without being blocked waiting if it's already occupied. + * If it gets freed by its current owned, you become the new owner, even if you're still not blocked on it. + * Nobody can lock it behind your back or overpass you in the queue in any way, even if you're still not blocked on it. + * + * Afterward, when you do consider the lock, the test() or wait() operations are both non-blocking since you're the + * owner. People who declared interest in the mutex after you get stuck in the queue behind you. + * + * + * Splitting the locking process this way is interesting for symmetry with the other activities such as exec or + * communication that do have an async variant and could be mildly interesting to the users once exposed in S4U, but + * that's not the only reason. It's also very important to the MC world: the Mutex::lock_async() is always enabled + * (nothing can prevent you from adding yourself to the queue of potential owners) while Acquisition::wait() is + * persistent: it's not always enabled but once it gets enabled (because you're the owner), it remains enabled for ever. + * + * Mutex::lock() is not persistent: sometimes it's enabled if the mutex is free, and then it gets disabled if + * someone else locks the mutex, and then it becomes enabled again once the mutex is freed. This is why Mutex::lock() + * is not used in our MC computational model: we ban non-persistent transitions because they would make some + * computations much more complex. + * + * In particular, computing the extension of an unfolding's configuration is polynomial when you only have persistent + * transitions while it's O(2^n) when some of the transitions are non-persistent (you have to consider again all subsets + * of a set if some transitions may become disabled in between, while you don't have to reconsider them if you can reuse + * your previous computations). + */ +class XBT_PUBLIC MutexAcquisitionImpl : public ActivityImpl_T { + 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; + void finish() override; + void set_exception(actor::ActorImpl* issuer) override + { /* nothing to do */ + } +}; class XBT_PUBLIC MutexImpl { + std::atomic_int_fast32_t refcount_{1}; + s4u::Mutex piface_; + actor::ActorImpl* owner_ = nullptr; + std::deque ongoing_acquisitions_; + static unsigned next_id_; + unsigned id_ = next_id_++; + bool is_recursive_ = false; + int recursive_depth = 0; + + friend MutexAcquisitionImpl; + public: - MutexImpl(); - ~MutexImpl(); + MutexImpl(bool recursive = false) : piface_(this), is_recursive_(recursive) {} MutexImpl(MutexImpl const&) = delete; MutexImpl& operator=(MutexImpl const&) = delete; - void lock(smx_actor_t issuer); - bool try_lock(smx_actor_t issuer); - void unlock(smx_actor_t issuer); + MutexAcquisitionImplPtr lock_async(actor::ActorImpl* issuer); + bool try_lock(actor::ActorImpl* issuer); + void unlock(actor::ActorImpl* issuer); + unsigned get_id() const { return id_; } - bool locked = false; - smx_actor_t owner = nullptr; - // List of sleeping processes: - simgrid::kernel::actor::SynchroList sleeping; + actor::ActorImpl* get_owner() const { return owner_; } // boost::intrusive_ptr support: friend void intrusive_ptr_add_ref(MutexImpl* mutex) @@ -36,19 +94,17 @@ public: XBT_ATTRIB_UNUSED auto previous = mutex->refcount_.fetch_add(1); xbt_assert(previous != 0); } + friend void intrusive_ptr_release(MutexImpl* mutex) { - if (mutex->refcount_.fetch_sub(1) == 1) + if (mutex->refcount_.fetch_sub(1) == 1) { + xbt_assert(mutex->ongoing_acquisitions_.empty(), "The destroyed mutex still had ongoing acquisitions"); + xbt_assert(mutex->owner_ == nullptr, "The destroyed mutex is still owned by %s", mutex->owner_->get_cname()); delete mutex; + } } - simgrid::s4u::Mutex& mutex() { return piface_; } - -private: - std::atomic_int_fast32_t refcount_{1}; - simgrid::s4u::Mutex piface_; + s4u::Mutex& get_iface() { return piface_; } }; -} -} -} -#endif /* SIMIX_MUTEXIMPL_HPP */ +} // namespace simgrid::kernel::activity +#endif