Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Avoid unsafe things
[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 "simgrid/s4u/conditionVariable.hpp"
10 #include "xbt/swag.h"
11
12 namespace simgrid {
13 namespace simix {
14
15 class XBT_PUBLIC() Mutex {
16 public:
17   Mutex();
18   ~Mutex();
19   Mutex(Mutex const&) = delete;
20   Mutex& operator=(Mutex 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(Mutex* mutex)
33   {
34     // Atomic operation! Do not split in two instructions!
35     auto previous = (mutex->refcount_)++;
36     xbt_assert(previous != 0);
37     (void) previous;
38   }
39   friend void intrusive_ptr_release(Mutex* mutex)
40   {
41     // Atomic operation! Do not split in two instructions!
42     auto count = --(mutex->refcount_);
43     if (count == 0)
44       delete mutex;
45   }
46
47   simgrid::s4u::Mutex& mutex() { return mutex_; }
48
49 private:
50   std::atomic_int_fast32_t refcount_ { 1 };
51   simgrid::s4u::Mutex mutex_;
52 };
53
54 }
55 }
56
57 typedef struct s_smx_cond {
58   s_smx_cond() : cond_(this) {}
59
60   std::atomic_int_fast32_t refcount_ { 1 };
61   smx_mutex_t mutex = nullptr;
62   xbt_swag_t sleeping = nullptr;  /* list of sleeping process */
63   simgrid::s4u::ConditionVariable cond_;
64 } s_smx_cond_t;
65
66 typedef struct s_smx_sem {
67   unsigned int value;
68   xbt_swag_t sleeping;          /* list of sleeping process */
69 } s_smx_sem_t;
70
71 XBT_PRIVATE void SIMIX_post_synchro(smx_activity_t synchro);
72 XBT_PRIVATE void SIMIX_synchro_stop_waiting(smx_actor_t process, smx_simcall_t simcall);
73 XBT_PRIVATE void SIMIX_synchro_destroy(smx_activity_t synchro);
74 XBT_PRIVATE void SIMIX_synchro_finish(smx_activity_t synchro);
75
76 XBT_PRIVATE smx_cond_t SIMIX_cond_init();
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