Line | Branch | Exec | Source |
---|---|---|---|
1 | /** | ||
2 | * @file lib.Semaphore.hpp | ||
3 | * @author Sergey Baigudin, sergey@baigudin.software | ||
4 | * @copyright 2014-2022, Sergey Baigudin, Baigudin Software | ||
5 | */ | ||
6 | #ifndef LIB_SEMAPHORE_HPP_ | ||
7 | #define LIB_SEMAPHORE_HPP_ | ||
8 | |||
9 | #include "lib.NonCopyable.hpp" | ||
10 | #include "api.Semaphore.hpp" | ||
11 | #include "sys.Call.hpp" | ||
12 | |||
13 | namespace eoos | ||
14 | { | ||
15 | namespace lib | ||
16 | { | ||
17 | |||
18 | /** | ||
19 | * @class Semaphore<A> | ||
20 | * @brief Semaphore class. | ||
21 | * | ||
22 | * @tparam A Heap memory allocator class. | ||
23 | */ | ||
24 | template <class A = Allocator> | ||
25 | class Semaphore : public NonCopyable<A>, public api::Semaphore | ||
26 | { | ||
27 | typedef lib::NonCopyable<A> Parent; | ||
28 | |||
29 | public: | ||
30 | |||
31 | /** | ||
32 | * @brief Constructor. | ||
33 | * | ||
34 | * @param permits The initial number of permits available. | ||
35 | */ | ||
36 | 8 | explicit Semaphore(int32_t const permits) | |
37 | : NonCopyable<A>() | ||
38 | , api::Semaphore() | ||
39 | 8 | , semaphore_(NULLPTR) { | |
40 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
8 | bool_t const isConstructed( construct(permits) ); |
41 | 8 | setConstructed( isConstructed ); | |
42 | 8 | } | |
43 | |||
44 | /** | ||
45 | * @brief Destructor. | ||
46 | */ | ||
47 | 16 | virtual ~Semaphore() | |
48 | { | ||
49 | 12 | delete semaphore_; | |
50 | } | ||
51 | |||
52 | /** | ||
53 | * @copydoc eoos::api::Object::isConstructed() | ||
54 | */ | ||
55 | 18 | virtual bool_t isConstructed() const | |
56 | { | ||
57 | 18 | return Parent::isConstructed(); | |
58 | } | ||
59 | |||
60 | /** | ||
61 | * @copydoc eoos::api::Semaphore::acquire() | ||
62 | */ | ||
63 | 2 | virtual bool_t acquire() | |
64 | { | ||
65 | 2 | bool_t res( false ); | |
66 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | if( isConstructed() ) |
67 | { | ||
68 | 2 | res = semaphore_->acquire(); | |
69 | } | ||
70 | 2 | return res; | |
71 | } | ||
72 | |||
73 | /** | ||
74 | * @copydoc eoos::api::Semaphore::release() | ||
75 | */ | ||
76 | 2 | virtual void release() | |
77 | { | ||
78 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | if( isConstructed() ) |
79 | { | ||
80 | 2 | semaphore_->release(); | |
81 | } | ||
82 | 2 | } | |
83 | |||
84 | protected: | ||
85 | |||
86 | using Parent::setConstructed; | ||
87 | |||
88 | private: | ||
89 | |||
90 | /** | ||
91 | * @brief Constructor. | ||
92 | * | ||
93 | * @param permits The initial number of permits available. | ||
94 | * @return True if object has been constructed successfully. | ||
95 | */ | ||
96 | 8 | bool_t construct(int32_t const permits) | |
97 | { | ||
98 | 8 | bool_t res( isConstructed() ); | |
99 |
1/2✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
|
8 | if( res == true ) |
100 | { | ||
101 | 8 | semaphore_ = sys::Call::get().createSemaphore(permits); | |
102 |
3/4✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
✓ Branch 3 taken 6 times.
✗ Branch 4 not taken.
|
8 | res = (semaphore_ != NULLPTR) ? semaphore_->isConstructed() : false; |
103 | } | ||
104 | 8 | return res; | |
105 | } | ||
106 | |||
107 | /** | ||
108 | * @brief System semaphore interface. | ||
109 | */ | ||
110 | api::Semaphore* semaphore_; | ||
111 | |||
112 | }; | ||
113 | |||
114 | } // namespace lib | ||
115 | } // namespace eoos | ||
116 | #endif // SEMAPHORE_HPP_ | ||
117 |