Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Test the dependencies of Mutex transitions
[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 #if SIMGRID_HAVE_MC
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")
15 #else
16 #define MC_CHECK_NO_DPOR() (void)0
17 #endif
18
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(ker_mutex, ker_synchro, "Mutex kernel-space implementation");
20
21 namespace simgrid {
22 namespace kernel {
23 namespace activity {
24
25 bool MutexAcquisitionImpl::test(actor::ActorImpl*)
26 {
27   return mutex_->owner_ == issuer_;
28 }
29 void MutexAcquisitionImpl::wait_for(actor::ActorImpl* issuer, double timeout)
30 {
31   xbt_assert(mutex_->owner_ != nullptr); // it was locked either by someone else or by me during the lock_async
32   xbt_assert(
33       issuer == issuer_,
34       "Actors can only wait acquisitions that they created themselves while this one was created by actor id %ld.",
35       issuer_->get_pid());
36   xbt_assert(timeout < 0, "Timeouts on mutex acquisitions are not implemented yet.");
37
38   this->register_simcall(&issuer_->simcall_); // Block on that acquisition
39
40   if (mutex_->get_owner() == issuer_) { // I'm the owner
41     finish();
42   } else {
43     // Already in the queue
44   }
45 }
46 void MutexAcquisitionImpl::finish()
47 {
48   xbt_assert(simcalls_.size() == 1, "Unexpected number of simcalls waiting: %zu", simcalls_.size());
49   smx_simcall_t simcall = simcalls_.front();
50   simcalls_.pop_front();
51
52   simcall->issuer_->waiting_synchro_ = nullptr;
53   simcall->issuer_->simcall_answer();
54 }
55
56 unsigned MutexImpl::next_id_ = 0;
57
58 MutexAcquisitionImplPtr MutexImpl::lock_async(actor::ActorImpl* issuer)
59 {
60   auto res = MutexAcquisitionImplPtr(new kernel::activity::MutexAcquisitionImpl(issuer, this), true);
61
62   if (owner_ != nullptr) {
63     /* FIXME: check if the host is active ? */
64     /* Somebody using the mutex, use a synchronization to get host failures */
65     sleeping_.push_back(res);
66   } else {
67     owner_  = issuer;
68   }
69   return res;
70 }
71
72 /** Tries to lock the mutex for a actor
73  *
74  * @param  issuer  the actor that tries to acquire the mutex
75  * @return whether we managed to lock the mutex
76  */
77 bool MutexImpl::try_lock(actor::ActorImpl* issuer)
78 {
79   XBT_IN("(%p, %p)", this, issuer);
80   MC_CHECK_NO_DPOR();
81   if (owner_ != nullptr) {
82     XBT_OUT();
83     return false;
84   }
85
86   owner_  = issuer;
87   XBT_OUT();
88   return true;
89 }
90
91 /** Unlock a mutex for a actor
92  *
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.
96  */
97 void MutexImpl::unlock(actor::ActorImpl* issuer)
98 {
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);
102
103   if (not sleeping_.empty()) {
104     /* Give the ownership to the first waiting actor */
105     auto acq = sleeping_.front();
106     owner_   = acq->get_issuer();
107
108     if (acq == owner_->waiting_synchro_)
109       acq->finish();
110
111     sleeping_.pop_front();
112   } else {
113     /* nobody to wake up */
114     owner_  = nullptr;
115   }
116   XBT_OUT();
117 }
118 /** Increase the refcount for this mutex */
119 MutexImpl* MutexImpl::ref()
120 {
121   intrusive_ptr_add_ref(this);
122   return this;
123 }
124
125 /** Decrease the refcount for this mutex */
126 void MutexImpl::unref()
127 {
128   intrusive_ptr_release(this);
129 }
130
131 } // namespace activity
132 } // namespace kernel
133 } // namespace simgrid