Logo AND Algorithmique Numérique Distribuée

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