Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
908378c497532e57abb1ddc538d77e06bef2d383
[simgrid.git] / src / s4u / s4u_Semaphore.cpp
1 /* Copyright (c) 2018-2021. 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 "src/msg/msg_private.hpp"
7 #include "xbt/log.h"
8
9 #include "simgrid/forward.h"
10 #include "simgrid/s4u/Semaphore.hpp"
11 #include "src/kernel/activity/SemaphoreImpl.hpp"
12 #include "src/mc/checker/SimcallObserver.hpp"
13
14 namespace simgrid {
15 namespace s4u {
16
17 SemaphorePtr Semaphore::create(unsigned int initial_capacity)
18 {
19   auto* sem = new kernel::activity::SemaphoreImpl(initial_capacity);
20   return SemaphorePtr(&sem->sem(), false);
21 }
22
23 void Semaphore::acquire()
24 {
25   kernel::actor::ActorImpl* issuer = kernel::actor::ActorImpl::self();
26   mc::SemAcquireSimcall observer{issuer, pimpl_};
27   kernel::actor::simcall_blocking<void>([&observer] { observer.get_sem()->acquire(observer.get_issuer(), -1.0); },
28                                         &observer);
29 }
30
31 bool Semaphore::acquire_timeout(double timeout)
32 {
33   return simcall_sem_acquire_timeout(pimpl_, timeout);
34 }
35
36 void Semaphore::release()
37 {
38   kernel::actor::simcall([this] { pimpl_->release(); });
39 }
40
41 int Semaphore::get_capacity() const
42 {
43   return kernel::actor::simcall([this] { return pimpl_->get_capacity(); });
44 }
45
46 bool Semaphore::would_block() const
47 {
48   return kernel::actor::simcall([this] { return pimpl_->would_block(); });
49 }
50
51 /* refcounting of the intrusive_ptr is delegated to the implementation object */
52 void intrusive_ptr_add_ref(const Semaphore* sem)
53 {
54   xbt_assert(sem);
55   sem->pimpl_->ref();
56 }
57
58 void intrusive_ptr_release(const Semaphore* sem)
59 {
60   xbt_assert(sem);
61   sem->pimpl_->unref();
62 }
63
64 } // namespace s4u
65 } // namespace simgrid
66
67 /* **************************** Public C interface *************************** */
68 /** @brief creates a semaphore object of the given initial capacity */
69 sg_sem_t sg_sem_init(int initial_value)
70 {
71   return simgrid::s4u::Semaphore::create(initial_value).detach();
72 }
73
74 /** @brief locks on a semaphore object */
75 void sg_sem_acquire(sg_sem_t sem)
76 {
77   sem->acquire();
78 }
79
80 /** @brief locks on a semaphore object up until the provided timeout expires */
81 int sg_sem_acquire_timeout(sg_sem_t sem, double timeout)
82 {
83   return sem->acquire_timeout(timeout);
84 }
85
86 /** @brief releases the semaphore object */
87 void sg_sem_release(sg_sem_t sem)
88 {
89   sem->release();
90 }
91
92 int sg_sem_get_capacity(const_sg_sem_t sem)
93 {
94   return sem->get_capacity();
95 }
96
97 void sg_sem_destroy(const_sg_sem_t sem)
98 {
99   intrusive_ptr_release(sem);
100 }
101
102 /** @brief returns a boolean indicating if this semaphore would block at this very specific time
103  *
104  * Note that the returned value may be wrong right after the function call, when you try to use it...
105  * But that's a classical semaphore issue, and SimGrid's semaphore are not different to usual ones here.
106  */
107 int sg_sem_would_block(const_sg_sem_t sem)
108 {
109   return sem->would_block();
110 }