Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
do not use xbt_mutex_t in SMPI
[simgrid.git] / src / s4u / s4u_Mutex.cpp
1 /* Copyright (c) 2006-2019. 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/s4u/Mutex.hpp"
7 #include "src/kernel/activity/MutexImpl.hpp"
8
9 namespace simgrid {
10 namespace s4u {
11
12 Mutex::~Mutex()
13 {
14   SIMIX_mutex_unref(pimpl_);
15 }
16 /** @brief Blocks the calling actor until the mutex can be obtained */
17 void Mutex::lock()
18 {
19   simcall_mutex_lock(pimpl_);
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   simcall_mutex_unlock(pimpl_);
29 }
30
31 /** @brief Acquire the mutex if it's free, and return false (without blocking) if not */
32 bool Mutex::try_lock()
33 {
34   return simcall_mutex_trylock(pimpl_);
35 }
36
37 /** @brief Create a new mutex
38  *
39  * See @ref s4u_raii.
40  */
41 MutexPtr Mutex::create()
42 {
43   smx_mutex_t mutex = simcall_mutex_init();
44   return MutexPtr(&mutex->mutex(), false);
45 }
46
47 /* refcounting of the intrusive_ptr is delegated to the implementation object */
48 void intrusive_ptr_add_ref(Mutex* mutex)
49 {
50   xbt_assert(mutex);
51   SIMIX_mutex_ref(mutex->pimpl_);
52 }
53 void intrusive_ptr_release(Mutex* mutex)
54 {
55   xbt_assert(mutex);
56   SIMIX_mutex_unref(mutex->pimpl_);
57 }
58 } // namespace s4u
59 } // namespace simgrid