File tree Expand file tree Collapse file tree 4 files changed +27
-34
lines changed Expand file tree Collapse file tree 4 files changed +27
-34
lines changed Original file line number Diff line number Diff line change 44
55namespace audio_tools {
66
7-
87/* *
98 * @brief RAII implementaion using a Mutex: Only a few microcontrollers provide
109 * lock guards, so I decided to roll my own solution where we can just use a
@@ -14,25 +13,25 @@ namespace audio_tools {
1413 * @author Phil Schatzmann
1514 * @copyright GPLv3 *
1615 */
16+
1717class LockGuard {
18- public:
18+ public:
1919 LockGuard (MutexBase &mutex) {
2020 p_mutex = &mutex;
2121 p_mutex->lock ();
2222 }
23+
2324 LockGuard (MutexBase *mutex) {
2425 p_mutex = mutex;
25- if (p_mutex != nullptr )
26- p_mutex->lock ();
26+ if (p_mutex != nullptr ) p_mutex->lock ();
2727 }
28+
2829 ~LockGuard () {
29- if (p_mutex != nullptr )
30- p_mutex->unlock ();
30+ if (p_mutex != nullptr ) p_mutex->unlock ();
3131 }
3232
33- protected:
33+ protected:
3434 MutexBase *p_mutex = nullptr ;
3535};
3636
37- }
38-
37+ } // namespace audio_tools
Original file line number Diff line number Diff line change 11#pragma once
2- #include " AudioConfig.h"
32#include < atomic>
43
4+ #include " AudioConfig.h"
5+
56#ifdef USE_STD_CONCURRENCY
6- # include < mutex>
7+ #include < mutex>
78#endif
89
910namespace audio_tools {
@@ -15,14 +16,12 @@ namespace audio_tools {
1516 * @copyright GPLv3
1617 */
1718class MutexBase {
18- public:
19+ public:
1920 virtual void lock () {}
2021 virtual void unlock () {}
2122};
2223
2324class SpinLock : public MutexBase {
24- volatile std::atomic<bool > lock_ = {0 };
25-
2625 void lock () {
2726 for (;;) {
2827 // Optimistically assume the lock is free on the first try
@@ -46,13 +45,13 @@ class SpinLock : public MutexBase {
4645 !lock_.exchange (true , std::memory_order_acquire);
4746 }
4847
49- void unlock () {
50- lock_.store (false , std::memory_order_release);
51- }
52- };
48+ void unlock () { lock_.store (false , std::memory_order_release); }
5349
50+ protected:
51+ volatile std::atomic<bool > lock_ = {0 };
52+ };
5453
55- #if defined(USE_STD_CONCURRENCY)
54+ #if defined(USE_STD_CONCURRENCY)
5655
5756/* *
5857 * @brief Mutex implemntation based on std::mutex
@@ -61,15 +60,14 @@ class SpinLock : public MutexBase {
6160 * @copyright GPLv3
6261 */
6362class StdMutex : public MutexBase {
64- public:
63+ public:
6564 void lock () override { std_mutex.lock (); }
6665 void unlock () override { std_mutex.unlock (); }
6766
68- protected:
67+ protected:
6968 std::mutex std_mutex;
7069};
7170
7271#endif
7372
74-
75- }
73+ } // namespace audio_tools
Original file line number Diff line number Diff line change @@ -21,20 +21,16 @@ namespace audio_tools {
2121class MutexRTOS : public MutexBase {
2222public:
2323 MutexRTOS () {
24- TRACED ();
2524 xSemaphore = xSemaphoreCreateBinary ();
2625 xSemaphoreGive (xSemaphore);
2726 }
2827 virtual ~MutexRTOS () {
29- TRACED ();
3028 vSemaphoreDelete (xSemaphore);
3129 }
3230 void lock () override {
33- TRACED ();
3431 xSemaphoreTake (xSemaphore, portMAX_DELAY);
3532 }
3633 void unlock () override {
37- TRACED ();
3834 xSemaphoreGive (xSemaphore);
3935 }
4036
Original file line number Diff line number Diff line change 55# include " Print.h"
66#endif
77
8+ #include " AudioTools/Concurrency/LockGuard.h"
89#if defined(RP2040)
9- # include " AudioTools/Concurrency/RP2040.h"
10+ # include " AudioTools/Concurrency/RP2040/MutexRP2040 .h"
1011#elif defined(ESP32)
11- # include " AudioTools/Concurrency/RTOS.h"
12- #else
13- # include " AudioTools/Concurrency/LockGuard.h"
12+ # include " AudioTools/Concurrency/RTOS/MutexRTOS.h"
1413#endif
1514
1615// Logging Implementation
1716#if USE_AUDIO_LOGGING
1817
1918namespace audio_tools {
2019
21- #if defined(RP2040)
20+ #if defined(ESP32)
21+ MutexRTOS audio_logger_mutex;
22+ #elif defined(RP2040)
2223MutexRP2040 audio_logger_mutex;
23- #elif defined(ESP32)
24- MutexESP32 audio_logger_mutex;
2524#else
2625MutexBase audio_logger_mutex; // no locking
2726#endif
@@ -72,6 +71,7 @@ class AudioLogger {
7271 fprintf ( stderr, " %s\n " , print_buffer);
7372#else
7473 log_print_ptr->println (print_buffer);
74+ log_print_ptr->flush ();
7575#endif
7676 print_buffer[0 ]=0 ;
7777 }
You can’t perform that action at this time.
0 commit comments