Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
split MutexImpl into their own files
[simgrid.git] / src / simix / MutexImpl.hpp
1 /* Copyright (c) 2012-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 #ifndef SIMIX_MUTEXIMPL_HPP
7 #define SIMIX_MUTEXIMPL_HPP
8
9 #include "simgrid/s4u/ConditionVariable.hpp"
10 #include "src/simix/ActorImpl.hpp"
11 #include <boost/intrusive/list.hpp>
12
13 namespace simgrid {
14 namespace simix {
15
16 class XBT_PUBLIC() MutexImpl {
17 public:
18   MutexImpl();
19   ~MutexImpl();
20   MutexImpl(MutexImpl const&) = delete;
21   MutexImpl& operator=(MutexImpl const&) = delete;
22
23   void lock(smx_actor_t issuer);
24   bool try_lock(smx_actor_t issuer);
25   void unlock(smx_actor_t issuer);
26
27   bool locked       = false;
28   smx_actor_t owner = nullptr;
29   // List of sleeping processes:
30   simgrid::simix::SynchroList sleeping;
31
32   // boost::intrusive_ptr<Mutex> support:
33   friend void intrusive_ptr_add_ref(MutexImpl* mutex)
34   {
35     XBT_ATTRIB_UNUSED auto previous = mutex->refcount_.fetch_add(1);
36     xbt_assert(previous != 0);
37   }
38   friend void intrusive_ptr_release(MutexImpl* mutex)
39   {
40     if (mutex->refcount_.fetch_sub(1) == 1)
41       delete mutex;
42   }
43
44   simgrid::s4u::Mutex& mutex() { return mutex_; }
45
46 private:
47   std::atomic_int_fast32_t refcount_{1};
48   simgrid::s4u::Mutex mutex_;
49 };
50 }
51 }
52 #endif /* SIMIX_MUTEXIMPL_HPP */