Logo AND Algorithmique Numérique Distribuée

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