Logo AND Algorithmique Numérique Distribuée

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