1 /* Copyright (c) 2007-2023. 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"
9 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ker_mutex, ker_synchro, "Mutex kernel-space implementation");
11 namespace simgrid::kernel::activity {
13 /* -------- Acquisition -------- */
15 bool MutexAcquisitionImpl::test(actor::ActorImpl*)
17 return mutex_->owner_ == issuer_;
19 void MutexAcquisitionImpl::wait_for(actor::ActorImpl* issuer, double timeout)
21 xbt_assert(mutex_->owner_ != nullptr); // it was locked either by someone else or by me during the lock_async
22 xbt_assert(issuer == issuer_, "Cannot wait on acquisitions created by another actor (id %ld)", issuer_->get_pid());
23 xbt_assert(timeout < 0, "Timeouts on mutex acquisitions are not implemented yet.");
25 this->register_simcall(&issuer_->simcall_); // Block on that acquisition
27 if (mutex_->get_owner() == issuer_) { // I'm the owner
30 // Already in the queue
33 void MutexAcquisitionImpl::post()
38 void MutexAcquisitionImpl::finish()
40 xbt_assert(simcalls_.size() == 1, "Unexpected number of simcalls waiting: %zu", simcalls_.size());
41 actor::Simcall* simcall = simcalls_.front();
42 simcalls_.pop_front();
44 simcall->issuer_->waiting_synchro_ = nullptr;
45 simcall->issuer_->simcall_answer();
48 /* -------- Mutex -------- */
50 unsigned MutexImpl::next_id_ = 0;
52 MutexAcquisitionImplPtr MutexImpl::lock_async(actor::ActorImpl* issuer)
54 auto res = MutexAcquisitionImplPtr(new kernel::activity::MutexAcquisitionImpl(issuer, this), true);
56 if (owner_ != nullptr) {
57 /* Somebody is using the mutex; register the acquisition */
58 ongoing_acquisitions_.push_back(res);
65 /** Tries to lock the mutex for a actor
67 * @param issuer the actor that tries to acquire the mutex
68 * @return whether we managed to lock the mutex
70 bool MutexImpl::try_lock(actor::ActorImpl* issuer)
72 XBT_IN("(%p, %p)", this, issuer);
73 if (owner_ != nullptr) {
83 /** Unlock a mutex for a actor
85 * Unlocks the mutex and gives it to a actor waiting for it.
86 * If the unlocker is not the owner of the mutex nothing happens.
87 * If there are no actor waiting, it sets the mutex as free.
89 void MutexImpl::unlock(actor::ActorImpl* issuer)
91 XBT_IN("(%p, %p)", this, issuer);
92 xbt_assert(issuer == owner_, "Cannot release that mutex: you're not the owner. %s is (pid:%ld).",
93 owner_ != nullptr ? owner_->get_cname() : "(nobody)", owner_ != nullptr ? owner_->get_pid() : -1);
95 if (not ongoing_acquisitions_.empty()) {
96 /* Give the ownership to the first waiting actor */
97 auto acq = ongoing_acquisitions_.front();
98 ongoing_acquisitions_.pop_front();
100 owner_ = acq->get_issuer();
101 if (acq == owner_->waiting_synchro_)
103 // else, the issuer is not blocked on this acquisition so no need to release it
106 /* nobody to wake up */
112 } // namespace simgrid::kernel::activity