Logo AND Algorithmique Numérique Distribuée

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