Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 's4u/model_list' into 'master'
[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() || simgrid::mc::reduction_mode != simgrid::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->simcalls_.push_back(&issuer->simcall_);
38     issuer->waiting_synchro_ = synchro;
39     sleeping_.push_back(*issuer);
40   } else {
41     /* mutex free */
42     locked_ = true;
43     owner_  = issuer;
44     issuer->simcall_answer();
45   }
46   XBT_OUT();
47 }
48
49 /** Tries to lock the mutex for a process
50  *
51  * @param  issuer  the process that tries to acquire the mutex
52  * @return whether we managed to lock the mutex
53  */
54 bool MutexImpl::try_lock(actor::ActorImpl* issuer)
55 {
56   XBT_IN("(%p, %p)", this, issuer);
57   MC_CHECK_NO_DPOR();
58   if (locked_) {
59     XBT_OUT();
60     return false;
61   }
62
63   locked_ = true;
64   owner_  = issuer;
65   XBT_OUT();
66   return true;
67 }
68
69 /** Unlock a mutex for a process
70  *
71  * Unlocks the mutex and gives it to a process waiting for it.
72  * If the unlocker is not the owner of the mutex nothing happens.
73  * If there are no process waiting, it sets the mutex as free.
74  */
75 void MutexImpl::unlock(actor::ActorImpl* issuer)
76 {
77   XBT_IN("(%p, %p)", this, issuer);
78   xbt_assert(locked_, "Cannot release that mutex: it was not locked.");
79   xbt_assert(issuer == owner_, "Cannot release that mutex: it was locked by %s (pid:%ld), not by you.",
80              owner_->get_cname(), owner_->get_pid());
81
82   if (not sleeping_.empty()) {
83     /* Give the ownership to the first waiting actor */
84     owner_ = &sleeping_.front();
85     sleeping_.pop_front();
86     owner_->waiting_synchro_ = nullptr;
87     owner_->simcall_answer();
88   } else {
89     /* nobody to wake up */
90     locked_ = false;
91     owner_  = nullptr;
92   }
93   XBT_OUT();
94 }
95 /** Increase the refcount for this mutex */
96 MutexImpl* MutexImpl::ref()
97 {
98   intrusive_ptr_add_ref(this);
99   return this;
100 }
101
102 /** Decrease the refcount for this mutex */
103 void MutexImpl::unref()
104 {
105   intrusive_ptr_release(this);
106 }
107
108 } // namespace activity
109 } // namespace kernel
110 } // namespace simgrid