1 /* Copyright (c) 2006-2015. The SimGrid Team. All rights reserved. */
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. */
6 #ifndef SIMGRID_S4U_MUTEX_HPP
7 #define SIMGRID_S4U_MUTEX_HPP
12 #include <boost/intrusive_ptr.hpp>
15 #include "simgrid/simix.h"
20 class ConditionVariable;
22 /** @brief A classical mutex, but blocking in the simulation world
24 * It is strictly impossible to use a real mutex (such as
25 * [std::mutex](http://en.cppreference.com/w/cpp/thread/mutex)
26 * or [pthread_mutex_t](http://pubs.opengroup.org/onlinepubs/007908775/xsh/pthread_mutex_lock.html)),
27 * because it would block the whole simulation.
28 * Instead, you should use the present class, that is a drop-in replacement of
29 * [std::mutex](http://en.cppreference.com/w/cpp/thread/mutex).
31 * As for any S4U object, Mutexes are using the @ref "RAII idiom" s4u_raii for memory management.
32 * Use createMutex() to get a ::MutexPtr to a newly created mutex and only manipulate ::MutexPtr.
35 XBT_PUBLIC_CLASS Mutex {
36 friend ConditionVariable;
38 friend simgrid::simix::Mutex;
39 simgrid::simix::Mutex* mutex_;
40 Mutex(simgrid::simix::Mutex* mutex) : mutex_(mutex) {}
42 /* refcounting of the intrusive_ptr is delegated to the implementation object */
43 friend void intrusive_ptr_add_ref(Mutex* mutex)
46 SIMIX_mutex_ref(mutex->mutex_);
48 friend void intrusive_ptr_release(Mutex* mutex)
51 SIMIX_mutex_unref(mutex->mutex_);
54 using Ptr = boost::intrusive_ptr<Mutex>;
57 /** You cannot create a new mutex by copying an existing one. Use MutexPtr instead */
58 Mutex(Mutex const&) = delete;
59 /** You cannot create a new mutex by value assignment either. Use MutexPtr instead */
60 Mutex& operator=(Mutex const&) = delete;
62 /** Constructs a new mutex */
63 static Ptr createMutex();
71 using MutexPtr = Mutex::Ptr;
73 }} // namespace simgrid::s4u
75 #endif /* SIMGRID_S4U_MUTEX_HPP */