Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Finish up the implementation of recursive mutexes
[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
34 void MutexAcquisitionImpl::finish()
35 {
36   xbt_assert(simcalls_.size() == 1, "Unexpected number of simcalls waiting: %zu", simcalls_.size());
37   actor::Simcall* simcall = simcalls_.front();
38   simcalls_.pop_front();
39
40   simcall->issuer_->waiting_synchro_ = nullptr;
41   simcall->issuer_->simcall_answer();
42 }
43
44 /* -------- Mutex -------- */
45
46 unsigned MutexImpl::next_id_ = 0;
47
48 MutexAcquisitionImplPtr MutexImpl::lock_async(actor::ActorImpl* issuer)
49 {
50   /* If the mutex is recursive */
51   if (is_recursive_) {
52     if (owner_ == issuer) {
53       recursive_depth++;
54       auto res = MutexAcquisitionImplPtr(new kernel::activity::MutexAcquisitionImpl(issuer, this), true);
55       res->grant();
56       return res;
57     } else if (owner_ == nullptr) { // Free
58       owner_          = issuer;
59       recursive_depth = 1;
60       auto res        = MutexAcquisitionImplPtr(new kernel::activity::MutexAcquisitionImpl(issuer, this), true);
61       res->grant();
62       return res;
63     }
64
65     for (auto acq : ongoing_acquisitions_)
66       if (acq->get_issuer() == issuer) {
67         acq->recursive_depth_++;
68         return acq;
69       }
70
71     // Not yet in the ongoing acquisition list. Get in there
72     auto res = MutexAcquisitionImplPtr(new kernel::activity::MutexAcquisitionImpl(issuer, this), true);
73     ongoing_acquisitions_.push_back(res);
74     return res;
75   }
76
77   // None-recursive mutex
78   auto res = MutexAcquisitionImplPtr(new kernel::activity::MutexAcquisitionImpl(issuer, this), true);
79   if (owner_ == nullptr) { // Lock is free, take it
80     owner_  = issuer;
81     recursive_depth = 1;
82     res->grant();
83   } else { // Somebody is using the mutex; register the acquisition
84     ongoing_acquisitions_.push_back(res);
85   }
86   return res;
87 }
88
89 /** Tries to lock the mutex for a actor
90  *
91  * @param  issuer  the actor that tries to acquire the mutex
92  * @return whether we managed to lock the mutex
93  */
94 bool MutexImpl::try_lock(actor::ActorImpl* issuer)
95 {
96   if (owner_ == issuer && is_recursive_) {
97     recursive_depth++;
98     return true;
99   }
100   if (owner_ != nullptr)
101     return false;
102
103   owner_ = issuer;
104   return true;
105 }
106
107 /** Unlock a mutex for a actor
108  *
109  * Unlocks the mutex and gives it to a actor waiting for it.
110  * If the unlocker is not the owner of the mutex nothing happens.
111  * If there are no actor waiting, it sets the mutex as free.
112  */
113 void MutexImpl::unlock(actor::ActorImpl* issuer)
114 {
115   XBT_IN("(%p, %p)", this, issuer);
116   xbt_assert(issuer == owner_, "Cannot release that mutex: you're not the owner. %s is (pid:%ld).",
117              owner_ != nullptr ? owner_->get_cname() : "(nobody)", owner_ != nullptr ? owner_->get_pid() : -1);
118
119   if (is_recursive_) {
120     recursive_depth--;
121     if (recursive_depth > 0) // Still owning the lock
122       return;
123   }
124
125   if (not ongoing_acquisitions_.empty()) {
126     /* Give the ownership to the first waiting actor */
127     auto acq = ongoing_acquisitions_.front();
128     ongoing_acquisitions_.pop_front();
129
130     owner_ = acq->get_issuer();
131     acq->grant();
132     recursive_depth = acq->recursive_depth_;
133     if (acq == owner_->waiting_synchro_)
134       acq->finish();
135     // else, the issuer is not blocked on this acquisition so no need to release it
136
137   } else {
138     /* nobody to wake up */
139     owner_  = nullptr;
140   }
141   XBT_OUT();
142 }
143
144 } // namespace simgrid::kernel::activity