8000 add timed wait · flexibity-team/arduino-esp32@9bcfd53 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9bcfd53

Browse files
committed
add timed wait
1 parent 2ce133c commit 9bcfd53

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

libraries/BLE/src/FreeRTOS.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,38 @@ uint32_t FreeRTOS::Semaphore::wait(std::string owner) {
7878
return m_value;
7979
} // wait
8080

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+
81113

82114
FreeRTOS::Semaphore::Semaphore(std::string name) {
83115
m_usePthreads = false; // Are we using pThreads or FreeRTOS?

libraries/BLE/src/FreeRTOS.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ class FreeRTOS {
4040
bool take(uint32_t timeoutMs, std::string owner = "<Unknown>");
4141
std::string toString();
4242
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; };
4345

4446
private:
4547
SemaphoreHandle_t m_semaphore;

0 commit comments

Comments
 (0)
0