Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Implement SemaphoreImpl::acquire_async (timeouts are allowed)
[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(issuer == issuer_, "Cannot wait on acquisitions created by another actor (id %ld)", issuer_->get_pid());
33   xbt_assert(timeout < 0, "Timeouts on mutex acquisitions are not implemented yet.");
34
35   this->register_simcall(&issuer_->simcall_); // Block on that acquisition
36
37   if (mutex_->get_owner() == issuer_) { // I'm the owner
38     finish();
39   } else {
40     // Already in the queue
41   }
42 }
43 void MutexAcquisitionImpl::finish()
44 {
45   xbt_assert(simcalls_.size() == 1, "Unexpected number of simcalls waiting: %zu", simcalls_.size());
46   smx_simcall_t simcall = simcalls_.front();
47   simcalls_.pop_front();
48
49   simcall->issuer_->waiting_synchro_ = nullptr;
50   simcall->issuer_->simcall_answer();
51 }
52
53 unsigned MutexImpl::next_id_ = 0;
54
55 MutexAcquisitionImplPtr MutexImpl::lock_async(actor::ActorImpl* issuer)
56 {
57   auto res = MutexAcquisitionImplPtr(new kernel::activity::MutexAcquisitionImpl(issuer, this), true);
58
59   if (owner_ != nullptr) {
60     /* Somebody is using the mutex; register the acquisition */
61     sleeping_.push_back(res);
62   } else {
63     owner_  = issuer;
64   }
65   return res;
66 }
67
68 /** Tries to lock the mutex for a actor
69  *
70  * @param  issuer  the actor that tries to acquire the mutex
71  * @return whether we managed to lock the mutex
72  */
73 bool MutexImpl::try_lock(actor::ActorImpl* issuer)
74 {
75   XBT_IN("(%p, %p)", this, issuer);
76   MC_CHECK_NO_DPOR();
77   if (owner_ != nullptr) {
78     XBT_OUT();
79     return false;
80   }
81
82   owner_  = issuer;
83   XBT_OUT();
84   return true;
85 }
86
87 /** Unlock a mutex for a actor
88  *
89  * Unlocks the mutex and gives it to a actor waiting for it.
90  * If the unlocker is not the owner of the mutex nothing happens.
91  * If there are no actor waiting, it sets the mutex as free.
92  */
93 void MutexImpl::unlock(actor::ActorImpl* issuer)
94 {
95   XBT_IN("(%p, %p)", this, issuer);
96   xbt_assert(issuer == owner_, "Cannot release that mutex: you're not the owner. %s is (pid:%ld).",
97              owner_ != nullptr ? owner_->get_cname() : "(nobody)", owner_ != nullptr ? owner_->get_pid() : -1);
98
99   if (not sleeping_.empty()) {
100     /* Give the ownership to the first waiting actor */
101     auto acq = sleeping_.front();
102     sleeping_.pop_front();
103
104     owner_ = acq->get_issuer();
105     if (acq == owner_->waiting_synchro_)
106       acq->finish();
107     // else, the issuer is not blocked on this acquisition so no need to release it
108
109   } else {
110     /* nobody to wake up */
111     owner_  = nullptr;
112   }
113   XBT_OUT();
114 }
115
116 } // namespace activity
117 } // namespace kernel
118 } // namespace simgrid