1 /* Copyright (c) 2007-2022. 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 #include "src/kernel/activity/MutexImpl.hpp"
7 #include "src/kernel/activity/Synchro.hpp"
10 #include "simgrid/modelchecker.h"
11 #include "src/mc/mc_safety.hpp"
12 #define MC_CHECK_NO_DPOR() \
13 xbt_assert(not MC_is_active() || mc::reduction_mode != mc::ReductionMode::dpor, \
14 "Mutex is currently not supported with DPOR, use --cfg=model-check/reduction:none")
16 #define MC_CHECK_NO_DPOR() (void)0
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ker_mutex, ker_synchro, "Mutex kernel-space implementation");
25 bool MutexAcquisitionImpl::test(actor::ActorImpl*)
27 return mutex_->owner_ == issuer_;
29 void MutexAcquisitionImpl::wait_for(actor::ActorImpl* issuer, double timeout)
31 xbt_assert(mutex_->owner_ != nullptr); // it was locked either by someone else or by me during the lock_async
34 "Actors can only wait acquisitions that they created themselves while this one was created by actor id %ld.",
36 xbt_assert(timeout < 0, "Timeouts on mutex acquisitions are not implemented yet.");
38 this->register_simcall(&issuer_->simcall_); // Block on that acquisition
40 if (mutex_->get_owner() == issuer_) { // I'm the owner
43 // Already in the queue
46 void MutexAcquisitionImpl::finish()
48 xbt_assert(simcalls_.size() == 1, "Unexpected number of simcalls waiting: %zu", simcalls_.size());
49 smx_simcall_t simcall = simcalls_.front();
50 simcalls_.pop_front();
52 simcall->issuer_->waiting_synchro_ = nullptr;
53 simcall->issuer_->simcall_answer();
56 MutexAcquisitionImplPtr MutexImpl::lock_async(actor::ActorImpl* issuer)
58 auto res = MutexAcquisitionImplPtr(new kernel::activity::MutexAcquisitionImpl(issuer, this), true);
60 if (owner_ != nullptr) {
61 /* FIXME: check if the host is active ? */
62 /* Somebody using the mutex, use a synchronization to get host failures */
63 sleeping_.push_back(res);
70 /** Tries to lock the mutex for a actor
72 * @param issuer the actor that tries to acquire the mutex
73 * @return whether we managed to lock the mutex
75 bool MutexImpl::try_lock(actor::ActorImpl* issuer)
77 XBT_IN("(%p, %p)", this, issuer);
79 if (owner_ != nullptr) {
89 /** Unlock a mutex for a actor
91 * Unlocks the mutex and gives it to a actor waiting for it.
92 * If the unlocker is not the owner of the mutex nothing happens.
93 * If there are no actor waiting, it sets the mutex as free.
95 void MutexImpl::unlock(actor::ActorImpl* issuer)
97 XBT_IN("(%p, %p)", this, issuer);
98 xbt_assert(issuer == owner_, "Cannot release that mutex: you're not the owner. %s is (pid:%ld).",
99 owner_ != nullptr ? owner_->get_cname() : "(nobody)", owner_ != nullptr ? owner_->get_pid() : -1);
101 if (not sleeping_.empty()) {
102 /* Give the ownership to the first waiting actor */
103 auto acq = sleeping_.front();
104 owner_ = acq->get_issuer();
106 if (acq == owner_->waiting_synchro_)
109 sleeping_.pop_front();
111 /* nobody to wake up */
116 /** Increase the refcount for this mutex */
117 MutexImpl* MutexImpl::ref()
119 intrusive_ptr_add_ref(this);
123 /** Decrease the refcount for this mutex */
124 void MutexImpl::unref()
126 intrusive_ptr_release(this);
129 } // namespace activity
130 } // namespace kernel
131 } // namespace simgrid