Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update copyright lines for 2022.
[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/mutex.h>
7 #include <simgrid/s4u/Mutex.hpp>
8 #include <src/kernel/activity/MutexImpl.hpp>
9 #include <src/kernel/actor/SimcallObserver.hpp>
10
11 namespace simgrid {
12 namespace s4u {
13
14 /** @brief Blocks the calling actor until the mutex can be obtained */
15 void Mutex::lock()
16 {
17   kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
18   kernel::actor::MutexLockSimcall observer{issuer, pimpl_};
19   kernel::actor::simcall_blocking([&observer] { observer.get_mutex()->lock(observer.get_issuer()); }, &observer);
20 }
21
22 /** @brief Release the ownership of the mutex, unleashing a blocked actor (if any)
23  *
24  * Will fail if the calling actor does not own the mutex.
25  */
26 void Mutex::unlock()
27 {
28   kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
29   kernel::actor::MutexUnlockSimcall observer{issuer, pimpl_};
30   kernel::actor::simcall([this, issuer] { this->pimpl_->unlock(issuer); }, &observer);
31 }
32
33 /** @brief Acquire the mutex if it's free, and return false (without blocking) if not */
34 bool Mutex::try_lock()
35 {
36   kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
37   kernel::actor::MutexLockSimcall observer{issuer, pimpl_, false};
38   return kernel::actor::simcall([&observer] { return observer.get_mutex()->try_lock(observer.get_issuer()); },
39                                 &observer);
40 }
41
42 /** @brief Create a new mutex
43  *
44  * See @ref s4u_raii.
45  */
46 MutexPtr Mutex::create()
47 {
48   auto* mutex = new kernel::activity::MutexImpl();
49   return MutexPtr(&mutex->mutex(), false);
50 }
51
52 /* refcounting of the intrusive_ptr is delegated to the implementation object */
53 void intrusive_ptr_add_ref(const Mutex* mutex)
54 {
55   xbt_assert(mutex);
56   mutex->pimpl_->ref();
57 }
58 void intrusive_ptr_release(const Mutex* mutex)
59 {
60   xbt_assert(mutex);
61   mutex->pimpl_->unref();
62 }
63
64 } // namespace s4u
65 } // namespace simgrid
66
67 /* **************************** Public C interface *************************** */
68 sg_mutex_t sg_mutex_init()
69 {
70   return simgrid::s4u::Mutex::create().detach();
71 }
72
73 void sg_mutex_lock(sg_mutex_t mutex)
74 {
75   mutex->lock();
76 }
77
78 void sg_mutex_unlock(sg_mutex_t mutex)
79 {
80   mutex->unlock();
81 }
82
83 int sg_mutex_try_lock(sg_mutex_t mutex)
84 {
85   return mutex->try_lock();
86 }
87
88 void sg_mutex_destroy(const_sg_mutex_t mutex)
89 {
90   intrusive_ptr_release(mutex);
91 }