Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Plug a memleak in barriers
[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
10 #include "src/kernel/activity/MutexImpl.hpp"
11 #include "src/kernel/actor/SynchroObserver.hpp"
12 #include "src/mc/mc_replay.hpp"
13
14 namespace simgrid {
15 namespace s4u {
16
17 /** @brief Blocks the calling actor until the mutex can be obtained */
18 void Mutex::lock()
19 {
20   kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
21
22   if (MC_is_active() || MC_record_replay_is_active()) { // Split in 2 simcalls for transition persistency
23     kernel::actor::MutexObserver lock_observer{issuer, mc::Transition::Type::MUTEX_LOCK, pimpl_};
24     auto acquisition =
25         kernel::actor::simcall_answered([issuer, this] { return pimpl_->lock_async(issuer); }, &lock_observer);
26
27     kernel::actor::MutexObserver wait_observer{issuer, mc::Transition::Type::MUTEX_WAIT, pimpl_};
28     kernel::actor::simcall_blocking([issuer, acquisition] { return acquisition->wait_for(issuer, -1); },
29                                     &wait_observer);
30
31   } else { // Do it in one simcall only
32     kernel::actor::simcall_blocking([issuer, this] { pimpl_->lock_async(issuer)->wait_for(issuer, -1); });
33   }
34 }
35
36 /** @brief Release the ownership of the mutex, unleashing a blocked actor (if any)
37  *
38  * Will fail if the calling actor does not own the mutex.
39  */
40 void Mutex::unlock()
41 {
42   kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
43   kernel::actor::MutexObserver observer{issuer, mc::Transition::Type::MUTEX_UNLOCK, pimpl_};
44   kernel::actor::simcall_answered([this, issuer] { this->pimpl_->unlock(issuer); }, &observer);
45 }
46
47 /** @brief Acquire the mutex if it's free, and return false (without blocking) if not */
48 bool Mutex::try_lock()
49 {
50   kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
51   kernel::actor::MutexObserver observer{issuer, mc::Transition::Type::MUTEX_TRYLOCK, pimpl_};
52   return kernel::actor::simcall_answered([&observer] { return observer.get_mutex()->try_lock(observer.get_issuer()); },
53                                          &observer);
54 }
55
56 /** @brief Create a new mutex
57  *
58  * See @ref s4u_raii.
59  */
60 MutexPtr Mutex::create()
61 {
62   auto* mutex = new kernel::activity::MutexImpl();
63   return MutexPtr(&mutex->mutex(), false);
64 }
65
66 /* refcounting of the intrusive_ptr is delegated to the implementation object */
67 void intrusive_ptr_add_ref(const Mutex* mutex)
68 {
69   intrusive_ptr_add_ref(mutex->pimpl_);
70 }
71 void intrusive_ptr_release(const Mutex* mutex)
72 {
73   intrusive_ptr_release(mutex->pimpl_);
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 }