Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2023.
[simgrid.git] / src / kernel / activity / MutexImpl.cpp
1 /* Copyright (c) 2007-2023. The SimGrid Team. All rights reserved.          */
2
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. */
5
6 #include "src/kernel/activity/MutexImpl.hpp"
7 #include "src/kernel/activity/Synchro.hpp"
8
9 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ker_mutex, ker_synchro, "Mutex kernel-space implementation");
10
11 namespace simgrid::kernel::activity {
12
13 /* -------- Acquisition -------- */
14
15 bool MutexAcquisitionImpl::test(actor::ActorImpl*)
16 {
17   return mutex_->owner_ == issuer_;
18 }
19 void MutexAcquisitionImpl::wait_for(actor::ActorImpl* issuer, double timeout)
20 {
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.");
24
25   this->register_simcall(&issuer_->simcall_); // Block on that acquisition
26
27   if (mutex_->get_owner() == issuer_) { // I'm the owner
28     finish();
29   } else {
30     // Already in the queue
31   }
32 }
33 void MutexAcquisitionImpl::finish()
34 {
35   xbt_assert(simcalls_.size() == 1, "Unexpected number of simcalls waiting: %zu", simcalls_.size());
36   actor::Simcall* simcall = simcalls_.front();
37   simcalls_.pop_front();
38
39   simcall->issuer_->waiting_synchro_ = nullptr;
40   simcall->issuer_->simcall_answer();
41 }
42
43 /* -------- Mutex -------- */
44
45 unsigned MutexImpl::next_id_ = 0;
46
47 MutexAcquisitionImplPtr MutexImpl::lock_async(actor::ActorImpl* issuer)
48 {
49   auto res = MutexAcquisitionImplPtr(new kernel::activity::MutexAcquisitionImpl(issuer, this), true);
50
51   if (owner_ != nullptr) {
52     /* Somebody is using the mutex; register the acquisition */
53     ongoing_acquisitions_.push_back(res);
54   } else {
55     owner_  = issuer;
56   }
57   return res;
58 }
59
60 /** Tries to lock the mutex for a actor
61  *
62  * @param  issuer  the actor that tries to acquire the mutex
63  * @return whether we managed to lock the mutex
64  */
65 bool MutexImpl::try_lock(actor::ActorImpl* issuer)
66 {
67   XBT_IN("(%p, %p)", this, issuer);
68   if (owner_ != nullptr) {
69     XBT_OUT();
70     return false;
71   }
72
73   owner_  = issuer;
74   XBT_OUT();
75   return true;
76 }
77
78 /** Unlock a mutex for a actor
79  *
80  * Unlocks the mutex and gives it to a actor waiting for it.
81  * If the unlocker is not the owner of the mutex nothing happens.
82  * If there are no actor waiting, it sets the mutex as free.
83  */
84 void MutexImpl::unlock(actor::ActorImpl* issuer)
85 {
86   XBT_IN("(%p, %p)", this, issuer);
87   xbt_assert(issuer == owner_, "Cannot release that mutex: you're not the owner. %s is (pid:%ld).",
88              owner_ != nullptr ? owner_->get_cname() : "(nobody)", owner_ != nullptr ? owner_->get_pid() : -1);
89
90   if (not ongoing_acquisitions_.empty()) {
91     /* Give the ownership to the first waiting actor */
92     auto acq = ongoing_acquisitions_.front();
93     ongoing_acquisitions_.pop_front();
94
95     owner_ = acq->get_issuer();
96     if (acq == owner_->waiting_synchro_)
97       acq->finish();
98     // else, the issuer is not blocked on this acquisition so no need to release it
99
100   } else {
101     /* nobody to wake up */
102     owner_  = nullptr;
103   }
104   XBT_OUT();
105 }
106
107 } // namespace simgrid::kernel::activity