1 /* Copyright (c) 2012, 2014. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #ifndef _SIMIX_SYNCHRO_PRIVATE_H
8 #define _SIMIX_SYNCHRO_PRIVATE_H
12 #include <simgrid/s4u/mutex.hpp>
13 #include <simgrid/s4u/conditionVariable.hpp>
17 #include "xbt/xbt_os_thread.h"
18 #include "src/simix/popping_private.h"
23 class XBT_PUBLIC() Mutex {
27 Mutex(Mutex const&) = delete;
28 Mutex& operator=(Mutex const&) = delete;
30 void lock(smx_process_t issuer);
31 bool try_lock(smx_process_t issuer);
32 void unlock(smx_process_t issuer);
35 smx_process_t owner = nullptr;
36 // List of sleeping processes:
37 xbt_swag_t sleeping = nullptr;
39 // boost::intrusive_ptr<Mutex> support:
40 friend void intrusive_ptr_add_ref(Mutex* mutex)
42 // Atomic operation! Do not split in two instructions!
43 auto previous = (mutex->refcount_)++;
44 xbt_assert(previous != 0);
47 friend void intrusive_ptr_release(Mutex* mutex)
49 // Atomic operation! Do not split in two instructions!
50 auto count = --(mutex->refcount_);
55 simgrid::s4u::Mutex& mutex() { return mutex_; }
58 std::atomic_int_fast32_t refcount_ { 1 };
59 simgrid::s4u::Mutex mutex_;
65 typedef struct s_smx_cond {
66 s_smx_cond() : cond_(this) {}
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_;
74 typedef struct s_smx_sem {
76 xbt_swag_t sleeping; /* list of sleeping process */
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);
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);
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);