1 /* Copyright (c) 2012-2016. The SimGrid Team. All rights reserved. */
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. */
6 #ifndef _SIMIX_SYNCHRO_PRIVATE_H
7 #define _SIMIX_SYNCHRO_PRIVATE_H
11 #include <simgrid/s4u/Mutex.hpp>
12 #include <simgrid/s4u/conditionVariable.hpp>
16 #include "xbt/xbt_os_thread.h"
17 #include "src/simix/popping_private.h"
22 class XBT_PUBLIC() Mutex {
26 Mutex(Mutex const&) = delete;
27 Mutex& operator=(Mutex const&) = delete;
29 void lock(smx_actor_t issuer);
30 bool try_lock(smx_actor_t issuer);
31 void unlock(smx_actor_t issuer);
34 smx_actor_t owner = nullptr;
35 // List of sleeping processes:
36 xbt_swag_t sleeping = nullptr;
38 // boost::intrusive_ptr<Mutex> support:
39 friend void intrusive_ptr_add_ref(Mutex* mutex)
41 // Atomic operation! Do not split in two instructions!
42 auto previous = (mutex->refcount_)++;
43 xbt_assert(previous != 0);
46 friend void intrusive_ptr_release(Mutex* mutex)
48 // Atomic operation! Do not split in two instructions!
49 auto count = --(mutex->refcount_);
54 simgrid::s4u::Mutex& mutex() { return mutex_; }
57 std::atomic_int_fast32_t refcount_ { 1 };
58 simgrid::s4u::Mutex mutex_;
64 typedef struct s_smx_cond {
65 s_smx_cond() : cond_(this) {}
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_;
73 typedef struct s_smx_sem {
75 xbt_swag_t sleeping; /* list of sleeping process */
78 XBT_PRIVATE void SIMIX_post_synchro(smx_activity_t synchro);
79 XBT_PRIVATE void SIMIX_synchro_stop_waiting(smx_actor_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);
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);
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);