Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use s4u API in example.
[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     // Atomic operation! Do not split in two instructions!
35     XBT_ATTRIB_UNUSED auto previous = (mutex->refcount_)++;
36     xbt_assert(previous != 0);
37   }
38   friend void intrusive_ptr_release(MutexImpl* mutex)
39   {
40     // Atomic operation! Do not split in two instructions!
41     auto count = --(mutex->refcount_);
42     if (count == 0)
43       delete mutex;
44   }
45
46   simgrid::s4u::Mutex& mutex() { return mutex_; }
47
48 private:
49   std::atomic_int_fast32_t refcount_{1};
50   simgrid::s4u::Mutex mutex_;
51 };
52 }
53 }
54
55 struct s_smx_cond_t {
56   s_smx_cond_t() : cond_(this) {}
57
58   std::atomic_int_fast32_t refcount_{1};
59   smx_mutex_t mutex   = nullptr;
60   xbt_swag_t sleeping = nullptr; /* list of sleeping process */
61   simgrid::s4u::ConditionVariable cond_;
62 };
63
64 struct s_smx_sem_t {
65   unsigned int value;
66   xbt_swag_t sleeping; /* list of sleeping process */
67 };
68
69 XBT_PRIVATE void SIMIX_post_synchro(smx_activity_t synchro);
70 XBT_PRIVATE void SIMIX_synchro_stop_waiting(smx_actor_t process, smx_simcall_t simcall);
71 XBT_PRIVATE void SIMIX_synchro_destroy(smx_activity_t synchro);
72 XBT_PRIVATE void SIMIX_synchro_finish(smx_activity_t synchro);
73
74 XBT_PRIVATE smx_cond_t SIMIX_cond_init();
75 XBT_PRIVATE void SIMIX_cond_broadcast(smx_cond_t cond);
76 XBT_PRIVATE void SIMIX_cond_signal(smx_cond_t cond);
77 XBT_PRIVATE void intrusive_ptr_add_ref(s_smx_cond_t* cond);
78 XBT_PRIVATE void intrusive_ptr_release(s_smx_cond_t* cond);
79
80 XBT_PRIVATE XBT_PRIVATE smx_sem_t SIMIX_sem_init(unsigned int value);
81 XBT_PRIVATE void SIMIX_sem_release(smx_sem_t sem);
82 XBT_PRIVATE int SIMIX_sem_would_block(smx_sem_t sem);
83 XBT_PRIVATE int SIMIX_sem_get_capacity(smx_sem_t sem);
84
85 #endif