-
-
Notifications
You must be signed in to change notification settings - Fork 247
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a46a7f8
commit 65b5b20
Showing
6 changed files
with
135 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#pragma once | ||
#include "AudioConfig.h" | ||
#include <atomic> | ||
|
||
#ifdef USE_STD_CONCURRENCY | ||
# include <mutex> | ||
#endif | ||
|
||
namespace audio_tools { | ||
|
||
/** | ||
* @brief Empty Mutex implementation which does nothing | ||
* @ingroup concurrency | ||
* @author Phil Schatzmann | ||
* @copyright GPLv3 | ||
*/ | ||
class MutexBase { | ||
public: | ||
virtual void lock() {} | ||
virtual void unlock() {} | ||
}; | ||
|
||
|
||
class SpinLock : public MutexBase { | ||
std::atomic<bool> lock_ = {0}; | ||
|
||
void lock() { | ||
for (;;) { | ||
// Optimistically assume the lock is free on the first try | ||
if (!lock_.exchange(true, std::memory_order_acquire)) { | ||
return; | ||
} | ||
// Wait for lock to be released without generating cache misses | ||
while (lock_.load(std::memory_order_relaxed)) { | ||
// Issue X86 PAUSE or ARM YIELD instruction to reduce contention between | ||
// hyper-threads | ||
//__builtin_ia32_pause(); | ||
delay(1); | ||
} | ||
} | ||
} | ||
|
||
bool try_lock() { | ||
// First do a relaxed load to check if lock is free in order to prevent | ||
// unnecessary cache misses if someone does while(!try_lock()) | ||
return !lock_.load(std::memory_order_relaxed) && | ||
!lock_.exchange(true, std::memory_order_acquire); | ||
} | ||
|
||
void unlock() { | ||
lock_.store(false, std::memory_order_release); | ||
} | ||
}; | ||
|
||
|
||
#if defined(USE_STD_CONCURRENCY) | ||
|
||
/** | ||
* @brief Mutex implemntation based on std::mutex | ||
* @ingroup concurrency | ||
* @author Phil Schatzmann | ||
* @copyright GPLv3 | ||
*/ | ||
class StdMutex : public MutexBase { | ||
public: | ||
void lock() override { std_mutex.lock(); } | ||
void unlock() override { std_mutex.unlock(); } | ||
|
||
protected: | ||
std::mutex std_mutex; | ||
}; | ||
|
||
#endif | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#pragma once | ||
#include "AudioConfig.h" | ||
#include "Mutex.h" | ||
|
||
#ifdef ESP32 | ||
# include "freertos/FreeRTOS.h" | ||
# include "freertos/semphr.h" | ||
#else | ||
# include "FreeRTOS.h" | ||
# include "semphr.h" | ||
#endif | ||
|
||
namespace audio_tools { | ||
|
||
/** | ||
* @brief Mutex implemntation using FreeRTOS | ||
* @ingroup concurrency | ||
* @author Phil Schatzmann | ||
* @copyright GPLv3 * | ||
*/ | ||
class Mutex : public MutexBase { | ||
public: | ||
Mutex() { | ||
TRACED(); | ||
xSemaphore = xSemaphoreCreateBinary(); | ||
xSemaphoreGive(xSemaphore); | ||
} | ||
~Mutex() { | ||
TRACED(); | ||
vSemaphoreDelete(xSemaphore); | ||
} | ||
void lock() override { | ||
TRACED(); | ||
xSemaphoreTake(xSemaphore, portMAX_DELAY); | ||
} | ||
void unlock() override { | ||
TRACED(); | ||
xSemaphoreGive(xSemaphore); | ||
} | ||
|
||
protected: | ||
SemaphoreHandle_t xSemaphore = NULL; | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters