Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of framagit.org:simgrid/simgrid
[simgrid.git] / src / kernel / activity / MutexImpl.cpp
1 /* Copyright (c) 2007-2021. 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/SynchroRaw.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(simix_mutex, simix_synchro, "Mutex kernel-space implementation");
20
21 namespace simgrid {
22 namespace kernel {
23 namespace activity {
24
25 void MutexImpl::lock(actor::ActorImpl* issuer)
26 {
27   XBT_IN("(%p; %p)", this, issuer);
28   MC_CHECK_NO_DPOR();
29   /* FIXME: check where to validate the arguments */
30   RawImplPtr synchro = nullptr;
31
32   if (locked_) {
33     /* FIXME: check if the host is active ? */
34     /* Somebody using the mutex, use a synchronization to get host failures */
35     synchro = RawImplPtr(new RawImpl([this, issuer]() { this->remove_sleeping_actor(*issuer); }));
36     (*synchro).set_host(issuer->get_host()).start();
37     synchro->register_simcall(&issuer->simcall_);
38     sleeping_.push_back(*issuer);
39   } else {
40     /* mutex free */
41     locked_ = true;
42     owner_  = issuer;
43     issuer->simcall_answer();
44   }
45   XBT_OUT();
46 }
47
48 /** Tries to lock the mutex for a process
49  *
50  * @param  issuer  the process that tries to acquire the mutex
51  * @return whether we managed to lock the mutex
52  */
53 bool MutexImpl::try_lock(actor::ActorImpl* issuer)
54 {
55   XBT_IN("(%p, %p)", this, issuer);
56   MC_CHECK_NO_DPOR();
57   if (locked_) {
58     XBT_OUT();
59     return false;
60   }
61
62   locked_ = true;
63   owner_  = issuer;
64   XBT_OUT();
65   return true;
66 }
67
68 /** Unlock a mutex for a process
69  *
70  * Unlocks the mutex and gives it to a process waiting for it.
71  * If the unlocker is not the owner of the mutex nothing happens.
72  * If there are no process waiting, it sets the mutex as free.
73  */
74 void MutexImpl::unlock(actor::ActorImpl* issuer)
75 {
76   XBT_IN("(%p, %p)", this, issuer);
77   xbt_assert(locked_, "Cannot release that mutex: it was not locked.");
78   xbt_assert(issuer == owner_, "Cannot release that mutex: it was locked by %s (pid:%ld), not by you.",
79              owner_->get_cname(), owner_->get_pid());
80
81   if (not sleeping_.empty()) {
82     /* Give the ownership to the first waiting actor */
83     owner_ = &sleeping_.front();
84     sleeping_.pop_front();
85     owner_->waiting_synchro_ = nullptr;
86     owner_->simcall_answer();
87   } else {
88     /* nobody to wake up */
89     locked_ = false;
90     owner_  = nullptr;
91   }
92   XBT_OUT();
93 }
94 /** Increase the refcount for this mutex */
95 MutexImpl* MutexImpl::ref()
96 {
97   intrusive_ptr_add_ref(this);
98   return this;
99 }
100
101 /** Decrease the refcount for this mutex */
102 void MutexImpl::unref()
103 {
104   intrusive_ptr_release(this);
105 }
106
107 } // namespace activity
108 } // namespace kernel
109 } // namespace simgrid