Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
check that disk has been given as argument before accessing its pimpl
[simgrid.git] / src / s4u / s4u_Semaphore.cpp
1 /* Copyright (c) 2018-2022. 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 #include <simgrid/modelchecker.h>
7 #include <simgrid/s4u/Semaphore.hpp>
8 #include <simgrid/semaphore.h>
9
10 #include "src/kernel/activity/SemaphoreImpl.hpp"
11 #include "src/kernel/actor/SynchroObserver.hpp"
12 #include "src/mc/mc_replay.hpp"
13
14 namespace simgrid::s4u {
15
16 SemaphorePtr Semaphore::create(unsigned int initial_capacity)
17 {
18   auto* sem = new kernel::activity::SemaphoreImpl(initial_capacity);
19   return SemaphorePtr(&sem->sem(), false);
20 }
21
22 void Semaphore::acquire()
23 {
24   acquire_timeout(-1);
25 }
26
27 bool Semaphore::acquire_timeout(double timeout)
28 {
29   kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
30
31   if (MC_is_active() || MC_record_replay_is_active()) { // Split in 2 simcalls for transition persistency
32     kernel::actor::SemaphoreObserver lock_observer{issuer, mc::Transition::Type::SEM_ASYNC_LOCK, pimpl_};
33     auto acquisition =
34         kernel::actor::simcall_answered([issuer, this] { return pimpl_->acquire_async(issuer); }, &lock_observer);
35
36     kernel::actor::SemaphoreAcquisitionObserver wait_observer{issuer, mc::Transition::Type::SEM_WAIT, acquisition.get(),
37                                                               timeout};
38     return kernel::actor::simcall_blocking([issuer, acquisition, timeout] { acquisition->wait_for(issuer, timeout); },
39                                            &wait_observer);
40
41   } else { // Do it in one simcall only and without observer
42     kernel::actor::SemaphoreAcquisitionObserver observer{issuer, mc::Transition::Type::SEM_WAIT, nullptr, timeout};
43     return kernel::actor::simcall_blocking(
44         [this, issuer, timeout] { pimpl_->acquire_async(issuer)->wait_for(issuer, timeout); }, &observer);
45   }
46 }
47
48 void Semaphore::release()
49 {
50   kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
51   kernel::actor::SemaphoreObserver observer{issuer, mc::Transition::Type::SEM_UNLOCK, pimpl_};
52
53   kernel::actor::simcall_answered([this] { pimpl_->release(); }, &observer);
54 }
55
56 int Semaphore::get_capacity() const
57 {
58   return pimpl_->get_capacity();
59 }
60
61 bool Semaphore::would_block() const
62 {
63   return pimpl_->would_block();
64 }
65
66 /* refcounting of the intrusive_ptr is delegated to the implementation object */
67 void intrusive_ptr_add_ref(const Semaphore* sem)
68 {
69   intrusive_ptr_add_ref(sem->pimpl_);
70 }
71
72 void intrusive_ptr_release(const Semaphore* sem)
73 {
74   intrusive_ptr_release(sem->pimpl_);
75 }
76
77 } // namespace simgrid::s4u
78
79 /* **************************** Public C interface *************************** */
80 /** @brief creates a semaphore object of the given initial capacity */
81 sg_sem_t sg_sem_init(int initial_value)
82 {
83   return simgrid::s4u::Semaphore::create(initial_value).detach();
84 }
85
86 /** @brief locks on a semaphore object */
87 void sg_sem_acquire(sg_sem_t sem)
88 {
89   sem->acquire();
90 }
91
92 /** @brief locks on a semaphore object up until the provided timeout expires */
93 int sg_sem_acquire_timeout(sg_sem_t sem, double timeout)
94 {
95   return sem->acquire_timeout(timeout);
96 }
97
98 /** @brief releases the semaphore object */
99 void sg_sem_release(sg_sem_t sem)
100 {
101   sem->release();
102 }
103
104 int sg_sem_get_capacity(const_sg_sem_t sem)
105 {
106   return sem->get_capacity();
107 }
108
109 void sg_sem_destroy(const_sg_sem_t sem)
110 {
111   intrusive_ptr_release(sem);
112 }
113
114 /** @brief returns a boolean indicating if this semaphore would block at this very specific time
115  *
116  * Note that the returned value may be wrong right after the function call, when you try to use it...
117  * But that's a classical semaphore issue, and SimGrid's semaphore are not different to usual ones here.
118  */
119 int sg_sem_would_block(const_sg_sem_t sem)
120 {
121   return sem->would_block();
122 }