Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Rename sg::k::actor::simcall ::actor::simcall_answered
[simgrid.git] / src / s4u / s4u_Mutex.cpp
1 /* Copyright (c) 2006-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 <simgrid/modelchecker.h>
7 #include <simgrid/mutex.h>
8 #include <simgrid/s4u/Mutex.hpp>
9 #include <src/kernel/activity/MutexImpl.hpp>
10 #include <src/kernel/actor/SynchroObserver.hpp>
11 #include <src/mc/mc_replay.hpp>
12
13 namespace simgrid {
14 namespace s4u {
15
16 /** @brief Blocks the calling actor until the mutex can be obtained */
17 void Mutex::lock()
18 {
19   kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
20
21   if (MC_is_active() || MC_record_replay_is_active()) { // Split in 2 simcalls for transition persistency
22     kernel::actor::MutexObserver lock_observer{issuer, mc::Transition::Type::MUTEX_LOCK, pimpl_};
23     auto acquisition =
24         kernel::actor::simcall_answered([issuer, this] { return pimpl_->lock_async(issuer); }, &lock_observer);
25
26     kernel::actor::MutexObserver wait_observer{issuer, mc::Transition::Type::MUTEX_WAIT, pimpl_};
27     kernel::actor::simcall_blocking([issuer, acquisition] { return acquisition->wait_for(issuer, -1); },
28                                     &wait_observer);
29
30   } else { // Do it in one simcall only
31     kernel::actor::simcall_blocking([issuer, this] { pimpl_->lock_async(issuer)->wait_for(issuer, -1); });
32   }
33 }
34
35 /** @brief Release the ownership of the mutex, unleashing a blocked actor (if any)
36  *
37  * Will fail if the calling actor does not own the mutex.
38  */
39 void Mutex::unlock()
40 {
41   kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
42   kernel::actor::MutexObserver observer{issuer, mc::Transition::Type::MUTEX_UNLOCK, pimpl_};
43   kernel::actor::simcall_answered([this, issuer] { this->pimpl_->unlock(issuer); }, &observer);
44 }
45
46 /** @brief Acquire the mutex if it's free, and return false (without blocking) if not */
47 bool Mutex::try_lock()
48 {
49   kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
50   kernel::actor::MutexObserver observer{issuer, mc::Transition::Type::MUTEX_TRYLOCK, pimpl_};
51   return kernel::actor::simcall_answered([&observer] { return observer.get_mutex()->try_lock(observer.get_issuer()); },
52                                          &observer);
53 }
54
55 /** @brief Create a new mutex
56  *
57  * See @ref s4u_raii.
58  */
59 MutexPtr Mutex::create()
60 {
61   auto* mutex = new kernel::activity::MutexImpl();
62   return MutexPtr(&mutex->mutex(), false);
63 }
64
65 /* refcounting of the intrusive_ptr is delegated to the implementation object */
66 void intrusive_ptr_add_ref(const Mutex* mutex)
67 {
68   intrusive_ptr_add_ref(mutex->pimpl_);
69 }
70 void intrusive_ptr_release(const Mutex* mutex)
71 {
72   intrusive_ptr_release(mutex->pimpl_);
73 }
74
75 } // namespace s4u
76 } // namespace simgrid
77
78 /* **************************** Public C interface *************************** */
79 sg_mutex_t sg_mutex_init()
80 {
81   return simgrid::s4u::Mutex::create().detach();
82 }
83
84 void sg_mutex_lock(sg_mutex_t mutex)
85 {
86   mutex->lock();
87 }
88
89 void sg_mutex_unlock(sg_mutex_t mutex)
90 {
91   mutex->unlock();
92 }
93
94 int sg_mutex_try_lock(sg_mutex_t mutex)
95 {
96   return mutex->try_lock();
97 }
98
99 void sg_mutex_destroy(const_sg_mutex_t mutex)
100 {
101   intrusive_ptr_release(mutex);
102 }