Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add test() for asynchronous executions
[simgrid.git] / src / simix / smx_synchro_private.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_SYNCHRO_PRIVATE_H
7 #define SIMIX_SYNCHRO_PRIVATE_H
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 typedef boost::intrusive::list<ActorImpl, boost::intrusive::member_hook<ActorImpl, boost::intrusive::list_member_hook<>,
17                                                                         &ActorImpl::smx_synchro_hook>>
18     SynchroList;
19
20 class XBT_PUBLIC() MutexImpl {
21 public:
22   MutexImpl();
23   ~MutexImpl();
24   MutexImpl(MutexImpl const&) = delete;
25   MutexImpl& operator=(MutexImpl const&) = delete;
26
27   void lock(smx_actor_t issuer);
28   bool try_lock(smx_actor_t issuer);
29   void unlock(smx_actor_t issuer);
30
31   bool locked       = false;
32   smx_actor_t owner = nullptr;
33   // List of sleeping processes:
34   simgrid::simix::SynchroList sleeping;
35
36   // boost::intrusive_ptr<Mutex> support:
37   friend void intrusive_ptr_add_ref(MutexImpl* mutex)
38   {
39     XBT_ATTRIB_UNUSED auto previous = mutex->refcount_.fetch_add(1);
40     xbt_assert(previous != 0);
41   }
42   friend void intrusive_ptr_release(MutexImpl* mutex)
43   {
44     if (mutex->refcount_.fetch_sub(1) == 1)
45       delete mutex;
46   }
47
48   simgrid::s4u::Mutex& mutex() { return mutex_; }
49
50 private:
51   std::atomic_int_fast32_t refcount_{1};
52   simgrid::s4u::Mutex mutex_;
53 };
54 }
55 }
56
57 struct s_smx_cond_t {
58   s_smx_cond_t() : cond_(this) {}
59
60   std::atomic_int_fast32_t refcount_{1};
61   smx_mutex_t mutex   = nullptr;
62   simgrid::simix::SynchroList sleeping; /* list of sleeping processes */
63   simgrid::s4u::ConditionVariable cond_;
64 };
65
66 struct s_smx_sem_t {
67   unsigned int value;
68   simgrid::simix::SynchroList sleeping; /* list of sleeping processes */
69 };
70
71 XBT_PRIVATE void SIMIX_post_synchro(smx_activity_t synchro);
72 XBT_PRIVATE void SIMIX_synchro_stop_waiting(smx_actor_t process, smx_simcall_t simcall);
73 XBT_PRIVATE void SIMIX_synchro_destroy(smx_activity_t synchro);
74 XBT_PRIVATE void SIMIX_synchro_finish(smx_activity_t synchro);
75
76 XBT_PRIVATE smx_cond_t SIMIX_cond_init();
77 XBT_PRIVATE void SIMIX_cond_broadcast(smx_cond_t cond);
78 XBT_PRIVATE void SIMIX_cond_signal(smx_cond_t cond);
79 XBT_PRIVATE void intrusive_ptr_add_ref(s_smx_cond_t* cond);
80 XBT_PRIVATE void intrusive_ptr_release(s_smx_cond_t* cond);
81
82 XBT_PRIVATE XBT_PRIVATE smx_sem_t SIMIX_sem_init(unsigned int value);
83 XBT_PRIVATE void SIMIX_sem_release(smx_sem_t sem);
84 XBT_PRIVATE int SIMIX_sem_would_block(smx_sem_t sem);
85 XBT_PRIVATE int SIMIX_sem_get_capacity(smx_sem_t sem);
86
87 #endif