Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
051f11c153d61aea22dc00f291a0afcdbd345c4d
[simgrid.git] / src / s4u / s4u_Mutex.cpp
1 /* Copyright (c) 2006-2023. 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::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_ASYNC_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] { acquisition->wait_for(issuer, -1); }, &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_answered([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_answered([&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   intrusive_ptr_add_ref(mutex->pimpl_);
68 }
69 void intrusive_ptr_release(const Mutex* mutex)
70 {
71   intrusive_ptr_release(mutex->pimpl_);
72 }
73
74 } // namespace simgrid::s4u
75
76 /* **************************** Public C interface *************************** */
77 sg_mutex_t sg_mutex_init()
78 {
79   return simgrid::s4u::Mutex::create().detach();
80 }
81
82 void sg_mutex_lock(sg_mutex_t mutex)
83 {
84   mutex->lock();
85 }
86
87 void sg_mutex_unlock(sg_mutex_t mutex)
88 {
89   mutex->unlock();
90 }
91
92 int sg_mutex_try_lock(sg_mutex_t mutex)
93 {
94   return mutex->try_lock();
95 }
96
97 void sg_mutex_destroy(const_sg_mutex_t mutex)
98 {
99   intrusive_ptr_release(mutex);
100 }