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 /* -------- Acquisition -------- */
27 bool MutexAcquisitionImpl::test(actor::ActorImpl*)
29 return mutex_->owner_ == issuer_;
31 void MutexAcquisitionImpl::wait_for(actor::ActorImpl* issuer, double timeout)
33 xbt_assert(mutex_->owner_ != nullptr); // it was locked either by someone else or by me during the lock_async
34 xbt_assert(issuer == issuer_, "Cannot wait on acquisitions created by another actor (id %ld)", issuer_->get_pid());
35 xbt_assert(timeout < 0, "Timeouts on mutex acquisitions are not implemented yet.");
37 this->register_simcall(&issuer_->simcall_); // Block on that acquisition
39 if (mutex_->get_owner() == issuer_) { // I'm the owner
42 // Already in the queue
45 void MutexAcquisitionImpl::finish()
47 xbt_assert(simcalls_.size() == 1, "Unexpected number of simcalls waiting: %zu", simcalls_.size());
48 smx_simcall_t simcall = simcalls_.front();
49 simcalls_.pop_front();
51 simcall->issuer_->waiting_synchro_ = nullptr;
52 simcall->issuer_->simcall_answer();
55 /* -------- Mutex -------- */
57 unsigned MutexImpl::next_id_ = 0;
59 MutexAcquisitionImplPtr MutexImpl::lock_async(actor::ActorImpl* issuer)
61 auto res = MutexAcquisitionImplPtr(new kernel::activity::MutexAcquisitionImpl(issuer, this), true);
63 if (owner_ != nullptr) {
64 /* Somebody is using the mutex; register the acquisition */
65 ongoing_acquisitions_.push_back(res);
72 /** Tries to lock the mutex for a actor
74 * @param issuer the actor that tries to acquire the mutex
75 * @return whether we managed to lock the mutex
77 bool MutexImpl::try_lock(actor::ActorImpl* issuer)
79 XBT_IN("(%p, %p)", this, issuer);
81 if (owner_ != nullptr) {
91 /** Unlock a mutex for a actor
93 * Unlocks the mutex and gives it to a actor waiting for it.
94 * If the unlocker is not the owner of the mutex nothing happens.
95 * If there are no actor waiting, it sets the mutex as free.
97 void MutexImpl::unlock(actor::ActorImpl* issuer)
99 XBT_IN("(%p, %p)", this, issuer);
100 xbt_assert(issuer == owner_, "Cannot release that mutex: you're not the owner. %s is (pid:%ld).",
101 owner_ != nullptr ? owner_->get_cname() : "(nobody)", owner_ != nullptr ? owner_->get_pid() : -1);
103 if (not ongoing_acquisitions_.empty()) {
104 /* Give the ownership to the first waiting actor */
105 auto acq = ongoing_acquisitions_.front();
106 ongoing_acquisitions_.pop_front();
108 owner_ = acq->get_issuer();
109 if (acq == owner_->waiting_synchro_)
111 // else, the issuer is not blocked on this acquisition so no need to release it
114 /* nobody to wake up */
120 } // namespace activity
121 } // namespace kernel
122 } // namespace simgrid