Logo AND Algorithmique Numérique Distribuée

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