Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
rename smx_synchro_t to smx_activity_t
[simgrid.git] / src / simix / smx_synchro_private.h
1 /* Copyright (c) 2012-2016. 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 <atomic>
10
11 #include <simgrid/s4u/Mutex.hpp>
12 #include <simgrid/s4u/conditionVariable.hpp>
13
14 #include "xbt/base.h"
15 #include "xbt/swag.h"
16 #include "xbt/xbt_os_thread.h"
17 #include "src/simix/popping_private.h"
18
19 namespace simgrid {
20 namespace simix {
21
22 class XBT_PUBLIC() Mutex {
23 public:
24   Mutex();
25   ~Mutex();
26   Mutex(Mutex const&) = delete;
27   Mutex& operator=(Mutex const&) = delete;
28
29   void lock(smx_process_t issuer);
30   bool try_lock(smx_process_t issuer);
31   void unlock(smx_process_t issuer);
32
33   bool locked = false;
34   smx_process_t owner = nullptr;
35   // List of sleeping processes:
36   xbt_swag_t sleeping = nullptr;
37
38   // boost::intrusive_ptr<Mutex> support:
39   friend void intrusive_ptr_add_ref(Mutex* mutex)
40   {
41     // Atomic operation! Do not split in two instructions!
42     auto previous = (mutex->refcount_)++;
43     xbt_assert(previous != 0);
44     (void) previous;
45   }
46   friend void intrusive_ptr_release(Mutex* mutex)
47   {
48     // Atomic operation! Do not split in two instructions!
49     auto count = --(mutex->refcount_);
50     if (count == 0)
51       delete mutex;
52   }
53
54   simgrid::s4u::Mutex& mutex() { return mutex_; }
55
56 private:
57   std::atomic_int_fast32_t refcount_ { 1 };
58   simgrid::s4u::Mutex mutex_;
59 };
60
61 }
62 }
63
64 typedef struct s_smx_cond {
65   s_smx_cond() : cond_(this) {}
66
67   std::atomic_int_fast32_t refcount_ { 1 };
68   smx_mutex_t mutex = nullptr;
69   xbt_swag_t sleeping = nullptr;  /* list of sleeping process */
70   simgrid::s4u::ConditionVariable cond_;
71 } s_smx_cond_t;
72
73 typedef struct s_smx_sem {
74   unsigned int value;
75   xbt_swag_t sleeping;          /* list of sleeping process */
76 } s_smx_sem_t;
77
78 XBT_PRIVATE void SIMIX_post_synchro(smx_activity_t synchro);
79 XBT_PRIVATE void SIMIX_synchro_stop_waiting(smx_process_t process, smx_simcall_t simcall);
80 XBT_PRIVATE void SIMIX_synchro_destroy(smx_activity_t synchro);
81 XBT_PRIVATE void SIMIX_synchro_finish(smx_activity_t synchro);
82
83 XBT_PRIVATE smx_cond_t SIMIX_cond_init();
84 XBT_PRIVATE void SIMIX_cond_broadcast(smx_cond_t cond);
85 XBT_PRIVATE void SIMIX_cond_signal(smx_cond_t cond);
86 XBT_PRIVATE void intrusive_ptr_add_ref(s_smx_cond_t *cond);
87 XBT_PRIVATE void intrusive_ptr_release(s_smx_cond_t *cond);
88
89 XBT_PRIVATE XBT_PRIVATE smx_sem_t SIMIX_sem_init(unsigned int value);
90 XBT_PRIVATE void SIMIX_sem_release(smx_sem_t sem);
91 XBT_PRIVATE int SIMIX_sem_would_block(smx_sem_t sem);
92 XBT_PRIVATE int SIMIX_sem_get_capacity(smx_sem_t sem);
93 #endif