Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[s4u] add condition variables
[simgrid.git] / src / simix / smx_synchro_private.h
1 /* Copyright (c) 2012, 2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #ifndef _SIMIX_SYNCHRO_PRIVATE_H
8 #define _SIMIX_SYNCHRO_PRIVATE_H
9
10 #include <atomic>
11
12 #include "xbt/base.h"
13 #include "xbt/swag.h"
14 #include "xbt/xbt_os_thread.h"
15 #include "src/simix/popping_private.h"
16
17 namespace simgrid {
18 namespace simix {
19
20 class XBT_PUBLIC() Mutex {
21 public:
22   Mutex();
23   ~Mutex();
24   Mutex(Mutex const&) = delete;
25   Mutex& operator=(Mutex const&) = delete;
26
27   void lock(smx_process_t issuer);
28   bool try_lock(smx_process_t issuer);
29   void unlock(smx_process_t issuer);
30
31   bool locked = false;
32   smx_process_t owner = nullptr;
33   // List of sleeping processes:
34   xbt_swag_t sleeping = nullptr;
35
36   // boost::intrusive_ptr<Mutex> support:
37   friend void intrusive_ptr_add_ref(Mutex* mutex)
38   {
39     // Atomic operation! Do not split in two instructions!
40     auto previous = (mutex->refcount_)++;
41     xbt_assert(previous != 0);
42     (void) previous;
43   }
44   friend void intrusive_ptr_release(Mutex* mutex)
45   {
46     // Atomic operation! Do not split in two instructions!
47     auto count = --(mutex->refcount_);
48     if (count == 0)
49       delete mutex;
50   }
51 private:
52   std::atomic_int_fast32_t refcount_ { 1 };
53 };
54
55 }
56 }
57
58 typedef struct s_smx_cond {
59   smx_mutex_t mutex;
60   xbt_swag_t sleeping;          /* list of sleeping process */
61   std::atomic_int_fast32_t refcount_;
62 } s_smx_cond_t;
63
64 typedef struct s_smx_sem {
65   unsigned int value;
66   xbt_swag_t sleeping;          /* list of sleeping process */
67 } s_smx_sem_t;
68
69
70
71 XBT_PRIVATE void SIMIX_post_synchro(smx_synchro_t synchro);
72 XBT_PRIVATE void SIMIX_synchro_stop_waiting(smx_process_t process, smx_simcall_t simcall);
73 XBT_PRIVATE void SIMIX_synchro_destroy(smx_synchro_t synchro);
74 XBT_PRIVATE void SIMIX_synchro_finish(smx_synchro_t synchro);
75
76 XBT_PRIVATE smx_cond_t SIMIX_cond_init(void);
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 #endif