Xyris  0.5
Semaphore.hpp
Go to the documentation of this file.
1 /**
2  * @file Semaphore.hpp
3  * @author Keeton Feavel ([email protected])
4  * @brief
5  * @version 0.3
6  * @date 2020-08-28
7  *
8  * @copyright Copyright the Xyris Contributors (c) 2020
9  *
10  */
11 #pragma once
12 
13 #include <stdint.h>
14 #include <Scheduler/tasks.hpp>
15 
16 class Semaphore {
17 public:
18  /**
19  * @brief Initializes a semaphore struct using the values
20  * defined. Semaphores are used in order to maintain mutual
21  * exclusion when multiple threads need to access a particular
22  * variable.
23  *
24  * @param val Initial value
25  * @param share Allow semaphore to be shared across threads
26  * @param name Semaphore name (for debugging / printing)
27  */
28  Semaphore(uint32_t val, bool share, const char* name = nullptr);
29 
30  /**
31  * @brief Wait for the semaphore to become available.
32  * @return int Returns true on success.
33  */
34  bool wait();
35 
36  /**
37  * @brief Check if the semaphore is available. If unavailable, return immediately,
38  * otherwise wait on the semaphore.
39  *
40  * @return int Returns 0 on success.
41  */
42  bool tryWait();
43 
44  /**
45  * @brief Wait on the semaphore for a set duration of time.
46  *
47  * @param usec Microseconds to wait until returning if unsuccessful.
48  * @return int Returns true on success.
49  */
50  bool timeWait(const uint32_t* usec);
51 
52  /**
53  * @brief Post to the semaphore.
54  *
55  * @return int Returns true on success.
56  */
57  bool post();
58 
59  /**
60  * @brief Atomically get the semaphore's current counter value.
61  *
62  * @return uint32_t Returns the semaphore count.
63  */
64  uint32_t count();
65 
66 private:
67  bool m_isShared;
68  uint32_t m_count;
70 };
Semaphore::timeWait
bool timeWait(const uint32_t *usec)
Wait on the semaphore for a set duration of time.
Definition: Semaphore.cpp:58
Semaphore::Semaphore
Semaphore(uint32_t val, bool share, const char *name=nullptr)
Initializes a semaphore struct using the values defined. Semaphores are used in order to maintain mut...
Definition: Semaphore.cpp:23
Semaphore::post
bool post()
Post to the semaphore.
Definition: Semaphore.cpp:73
Semaphore::count
uint32_t count()
Atomically get the semaphore's current counter value.
Definition: Semaphore.cpp:81
Semaphore::m_count
uint32_t m_count
Definition: Semaphore.hpp:68
tasks.hpp
Semaphore::m_taskSync
struct task_sync m_taskSync
Definition: Semaphore.hpp:69
task_sync
Definition: tasks.hpp:55
Semaphore::m_isShared
bool m_isShared
Definition: Semaphore.hpp:67
Semaphore::tryWait
bool tryWait()
Check if the semaphore is available. If unavailable, return immediately, otherwise wait on the semaph...
Definition: Semaphore.cpp:45
Semaphore::wait
bool wait()
Wait for the semaphore to become available.
Definition: Semaphore.cpp:31
Semaphore
Definition: Semaphore.hpp:16