Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4c8b9207fae94316078de54276932a02675394ba
[simgrid.git] / include / simgrid / s4u / Mutex.hpp
1 /* Copyright (c) 2006-2018. 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 #ifndef SIMGRID_S4U_MUTEX_HPP
7 #define SIMGRID_S4U_MUTEX_HPP
8
9 #include <simgrid/forward.h>
10 #include <xbt/asserts.h>
11
12 namespace simgrid {
13 namespace s4u {
14
15 /** @brief A classical mutex, but blocking in the simulation world
16  *  @ingroup s4u_api
17  *
18  * It is strictly impossible to use a real mutex, such as
19  * <a href="http://en.cppreference.com/w/cpp/thread/mutex">std::mutex</a>
20  * or <a href="http://pubs.opengroup.org/onlinepubs/007908775/xsh/pthread_mutex_lock.html">pthread_mutex_t</a>,
21  * because it would block the whole simulation.
22  * Instead, you should use the present class, that is a drop-in replacement of
23  * <a href="http://en.cppreference.com/w/cpp/thread/mutex>std::mutex</a>.
24  *
25  * As for any S4U object, Mutexes are using the @ref s4u_raii "RAII idiom" for memory management.
26  * Use create() to get a simgrid::s4u::MutexPtr to a newly created mutex and only manipulate simgrid::s4u::MutexPtr.
27  *
28  */
29 class XBT_PUBLIC Mutex {
30   friend simgrid::s4u::ConditionVariable;
31   friend simgrid::kernel::activity::MutexImpl;
32
33   simgrid::kernel::activity::MutexImpl* pimpl_;
34   explicit Mutex(simgrid::kernel::activity::MutexImpl* mutex) : pimpl_(mutex) {}
35
36   /* refcounting */
37   friend XBT_PUBLIC void intrusive_ptr_add_ref(Mutex* mutex);
38   friend XBT_PUBLIC void intrusive_ptr_release(Mutex* mutex);
39
40 public:
41   // No copy:
42   /** You cannot create a new mutex by copying an existing one. Use MutexPtr instead */
43   Mutex(Mutex const&) = delete;
44   /** You cannot create a new mutex by value assignment either. Use MutexPtr instead */
45   Mutex& operator=(Mutex const&) = delete;
46
47   /** Constructs a new mutex */
48   static MutexPtr create();
49
50   void lock();
51   void unlock();
52   bool try_lock();
53
54   // deprecated
55   /** @deprecated Mutex::create() */
56   XBT_ATTRIB_DEPRECATED_v323("Please use Mutex::create()") static MutexPtr createMutex() { return create(); }
57 };
58
59 }} // namespace simgrid::s4u
60
61 #endif /* SIMGRID_S4U_MUTEX_HPP */