Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b2482eda54dd9050b957a9b4c7fad1d90777b074
[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
13 namespace simgrid {
14 namespace 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   simcall_sem_acquire(pimpl_);
25 }
26
27 bool Semaphore::acquire_timeout(double timeout)
28 {
29   return simcall_sem_acquire_timeout(pimpl_, timeout);
30 }
31
32 void Semaphore::release()
33 {
34   kernel::actor::simcall([this] { pimpl_->release(); });
35 }
36
37 int Semaphore::get_capacity() const
38 {
39   return kernel::actor::simcall([this] { return pimpl_->get_capacity(); });
40 }
41
42 bool Semaphore::would_block() const
43 {
44   return kernel::actor::simcall([this] { return pimpl_->would_block(); });
45 }
46
47 /* refcounting of the intrusive_ptr is delegated to the implementation object */
48 void intrusive_ptr_add_ref(const Semaphore* sem)
49 {
50   xbt_assert(sem);
51   sem->pimpl_->ref();
52 }
53
54 void intrusive_ptr_release(const Semaphore* sem)
55 {
56   xbt_assert(sem);
57   sem->pimpl_->unref();
58 }
59
60 } // namespace s4u
61 } // namespace simgrid
62
63 /* **************************** Public C interface *************************** */
64 /** @brief creates a semaphore object of the given initial capacity */
65 sg_sem_t sg_sem_init(int initial_value)
66 {
67   return simgrid::s4u::Semaphore::create(initial_value).detach();
68 }
69
70 /** @brief locks on a semaphore object */
71 void sg_sem_acquire(sg_sem_t sem)
72 {
73   sem->acquire();
74 }
75
76 /** @brief locks on a semaphore object up until the provided timeout expires */
77 int sg_sem_acquire_timeout(sg_sem_t sem, double timeout)
78 {
79   return sem->acquire_timeout(timeout);
80 }
81
82 /** @brief releases the semaphore object */
83 void sg_sem_release(sg_sem_t sem)
84 {
85   sem->release();
86 }
87
88 int sg_sem_get_capacity(const_sg_sem_t sem)
89 {
90   return sem->get_capacity();
91 }
92
93 void sg_sem_destroy(const_sg_sem_t sem)
94 {
95   intrusive_ptr_release(sem);
96 }
97
98 /** @brief returns a boolean indicating if this semaphore would block at this very specific time
99  *
100  * Note that the returned value may be wrong right after the function call, when you try to use it...
101  * But that's a classical semaphore issue, and SimGrid's semaphore are not different to usual ones here.
102  */
103 int sg_sem_would_block(const_sg_sem_t sem)
104 {
105   return sem->would_block();
106 }