Logo AND Algorithmique Numérique Distribuée

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