File tree Expand file tree Collapse file tree 2 files changed +34
-0
lines changed Expand file tree Collapse file tree 2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change @@ -78,6 +78,38 @@ uint32_t FreeRTOS::Semaphore::wait(std::string owner) {
78
78
return m_value;
79
79
} // wait
80
80
81
+ /* *
82
+ * @brief Wait for a semaphore to be released in a given period of time by trying to take it and
83
+ * then releasing it again. The value associated with the semaphore can be taken by value() call after return
84
+ * @param [in] owner A debug tag.
85
+ * @param [in] timeoutMs timeout to wait in ms.
86
+ * @return True if we took the semaphore within timeframe.
87
+ */
88
+ bool FreeRTOS::Semaphore::timedWait (std::string owner, uint32_t timeoutMs) {
89
+ log_v (" >> wait: Semaphore waiting: %s for %s" , toString ().c_str (), owner.c_str ());
90
+
91
+ if (m_usePthreads && timeoutMs != portMAX_DELAY) {
92
+ assert (false ); // We apparently don't have a timed wait for pthreads.
93
+ }
94
+
95
+ auto ret = pdTRUE;
96
+
97
+ if (m_usePthreads) {
98
+ pthread_mutex_lock (&m_pthread_mutex);
99
+ } else {
100
+ ret = xSemaphoreTake (m_semaphore, timeoutMs);
101
+ }
102
+
103
+ if (m_usePthreads) {
104
+ pthread_mutex_unlock (&m_pthread_mutex);
105
+ } else {
106
+ xSemaphoreGive (m_semaphore);
107
+ }
108
+
109
+ log_v (" << wait: Semaphore %s released: %d" , toString ().c_str (), ret);
110
+ return ret;
111
+ } // wait
112
+
81
113
82
114
FreeRTOS::Semaphore::Semaphore (std::string name) {
83
115
m_usePthreads = false ; // Are we using pThreads or FreeRTOS?
Original file line number Diff line number Diff line change @@ -40,6 +40,8 @@ class FreeRTOS {
40
40
bool take (uint32_t timeoutMs, std::string owner = " <Unknown>" );
41
41
std::string toString ();
42
42
uint32_t wait (std::string owner = " <Unknown>" );
43
+ bool timedWait (std::string owner = " <Unknown>" , uint32_t timeoutMs = portMAX_DELAY);
44
+ uint32_t value (){ return m_value; };
43
45
44
46
private:
45
47
SemaphoreHandle_t m_semaphore;
You can’t perform that action at this time.
0 commit comments