EtcPal  0.4.1
ETC Platform Abstraction Layer (EtcPal)
View other versions:
sem.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2022 ETC Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *******************************************************************************
16  * This file is a part of EtcPal. For more information, go to:
17  * https://github.com/ETCLabs/EtcPal
18  ******************************************************************************/
19 
22 
23 #ifndef ETCPAL_CPP_SEM_H_
24 #define ETCPAL_CPP_SEM_H_
25 
26 #include "etcpal/cpp/common.h"
27 #include "etcpal/sem.h"
28 
29 namespace etcpal
30 {
36 
68 class Semaphore
69 {
70 public:
73  static constexpr unsigned int kDefaultMaxCount = 50;
74 
75  Semaphore(unsigned int initial_count = 0, unsigned int max_count = kDefaultMaxCount);
76  ~Semaphore();
77 
78  Semaphore(const Semaphore& other) = delete;
79  Semaphore& operator=(const Semaphore& other) = delete;
80  Semaphore(Semaphore&& other) = delete;
81  Semaphore& operator=(Semaphore&& other) = delete;
82 
83  bool Wait();
84  bool TryWait(int timeout_ms = 0);
85  bool Post();
86  bool PostFromIsr();
87 
88  etcpal_sem_t& get();
89 
90 private:
91  etcpal_sem_t sem_{};
92 };
93 
95 inline Semaphore::Semaphore(unsigned int initial_count, unsigned int max_count)
96 {
97  (void)etcpal_sem_create(&sem_, initial_count, max_count);
98 }
99 
102 {
103  etcpal_sem_destroy(&sem_);
104 }
105 
108 inline bool Semaphore::Wait()
109 {
110  return etcpal_sem_wait(&sem_);
111 }
112 
122 inline bool Semaphore::TryWait(int timeout_ms)
123 {
124  return etcpal_sem_timed_wait(&sem_, timeout_ms);
125 }
126 
130 inline bool Semaphore::Post()
131 {
132  return etcpal_sem_post(&sem_);
133 }
134 
141 {
142  return etcpal_sem_post_from_isr(&sem_);
143 }
144 
147 {
148  return sem_;
149 }
150 
151 }; // namespace etcpal
152 
153 #endif // ETCPAL_CPP_SEM_H_
A wrapper class for the EtcPal counting semaphore type.
Definition: sem.h:69
static constexpr unsigned int kDefaultMaxCount
The default value used for the semaphore's max_count.
Definition: sem.h:73
etcpal_sem_t & get()
Get a reference to the underlying etcpal_sem_t type.
Definition: sem.h:146
bool PostFromIsr()
Post the semaphore from an interrupt context.
Definition: sem.h:140
~Semaphore()
Destroy the semaphore.
Definition: sem.h:101
bool Post()
Post the semaphore.
Definition: sem.h:130
Semaphore(unsigned int initial_count=0, unsigned int max_count=kDefaultMaxCount)
Create a new semaphore.
Definition: sem.h:95
bool TryWait(int timeout_ms=0)
Wait for the semaphore until either it is received or a timeout expires.
Definition: sem.h:122
bool Wait()
Wait for the semaphore.
Definition: sem.h:108
Common definitions used by EtcPal C++ wrappers.
bool etcpal_sem_create(etcpal_sem_t *id, unsigned int initial_count, unsigned int max_count)
Create a new counting semaphore.
PLATFORM_DEFINED etcpal_sem_t
The semaphore identifier.
Definition: sem.dox:72
bool etcpal_sem_post(etcpal_sem_t *id)
Post a semaphore.
void etcpal_sem_destroy(etcpal_sem_t *id)
Destroy a semaphore object.
bool etcpal_sem_post_from_isr(etcpal_sem_t *id)
Post a semaphore from an interrupt context.
bool etcpal_sem_timed_wait(etcpal_sem_t *id, int timeout_ms)
Wait for a semaphore, giving up after a timeout.
bool etcpal_sem_wait(etcpal_sem_t *id)
Wait for a semaphore.