Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
this header is C++ only
[simgrid.git] / src / s4u / s4u_mutex.cpp
1 /* Copyright (c) 2006-2017. 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 "src/msg/msg_private.h"
7 #include "src/simix/smx_synchro_private.hpp"
8 #include "xbt/log.h"
9
10 #include "simgrid/s4u/Mutex.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   simcall_mutex_lock(mutex_);
19 }
20
21 /** @brief Release the ownership of the mutex, unleashing a blocked actor (if any)
22  *
23  * Will fail if the calling actor does not own the mutex.
24  */
25 void Mutex::unlock()
26 {
27   simcall_mutex_unlock(mutex_);
28 }
29
30 /** @brief Acquire the mutex if it's free, and return false (without blocking) if not */
31 bool Mutex::try_lock()
32 {
33   return simcall_mutex_trylock(mutex_);
34 }
35
36 /** @brief Create a new mutex
37  *
38  * See @ref s4u_raii.
39  */
40 MutexPtr Mutex::createMutex()
41 {
42   smx_mutex_t mutex = simcall_mutex_init();
43   return MutexPtr(&mutex->mutex(), false);
44 }
45
46 }
47 }