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 4
4
5
5
namespace audio_tools {
6
6
7
-
8
7
/* *
9
8
* @brief RAII implementaion using a Mutex: Only a few microcontrollers provide
10
9
* lock guards, so I decided to roll my own solution where we can just use a
@@ -14,25 +13,25 @@ namespace audio_tools {
14
13
* @author Phil Schatzmann
15
14
* @copyright GPLv3 *
16
15
*/
16
+
17
17
class LockGuard {
18
- public:
18
+ public:
19
19
LockGuard (MutexBase &mutex) {
20
20
p_mutex = &mutex;
21
21
p_mutex->lock ();
22
22
}
23
+
23
24
LockGuard (MutexBase *mutex) {
24
25
p_mutex = mutex;
25
- if (p_mutex != nullptr )
26
- p_mutex->lock ();
26
+ if (p_mutex != nullptr ) p_mutex->lock ();
27
27
}
28
+
28
29
~LockGuard () {
29
- if (p_mutex != nullptr )
30
- p_mutex->unlock ();
30
+ if (p_mutex != nullptr ) p_mutex->unlock ();
31
31
}
32
32
33
- protected:
33
+ protected:
34
34
MutexBase *p_mutex = nullptr ;
35
35
};
36
36
37
- }
38
-
37
+ } // namespace audio_tools
Original file line number Diff line number Diff line change 1
1
#pragma once
2
- #include " AudioConfig.h"
3
2
#include < atomic>
4
3
4
+ #include " AudioConfig.h"
5
+
5
6
#ifdef USE_STD_CONCURRENCY
6
- # include < mutex>
7
+ #include < mutex>
7
8
#endif
8
9
9
10
namespace audio_tools {
@@ -15,14 +16,12 @@ namespace audio_tools {
15
16
* @copyright GPLv3
16
17
*/
17
18
class MutexBase {
18
- public:
19
+ public:
19
20
virtual void lock () {}
20
21
virtual void unlock () {}
21
22
};
22
23
23
24
class SpinLock : public MutexBase {
24
- volatile std::atomic<bool > lock_ = {0 };
25
-
26
25
void lock () {
27
26
for (;;) {
28
27
// Optimistically assume the lock is free on the first try
@@ -46,13 +45,13 @@ class SpinLock : public MutexBase {
46
45
!lock_.exchange (true , std::memory_order_acquire);
47
46
}
48
47
49
- void unlock () {
50
- lock_.store (false , std::memory_order_release);
51
- }
52
- };
48
+ void unlock () { lock_.store (false , std::memory_order_release); }
53
49
50
+ protected:
51
+ volatile std::atomic<bool > lock_ = {0 };
52
+ };
54
53
55
- #if defined(USE_STD_CONCURRENCY)
54
+ #if defined(USE_STD_CONCURRENCY)
56
55
57
56
/* *
58
57
* @brief Mutex implemntation based on std::mutex
@@ -61,15 +60,14 @@ class SpinLock : public MutexBase {
61
60
* @copyright GPLv3
62
61
*/
63
62
class StdMutex : public MutexBase {
64
- public:
63
+ public:
65
64
void lock () override { std_mutex.lock (); }
66
65
void unlock () override { std_mutex.unlock (); }
67
66
68
- protected:
67
+ protected:
69
68
std::mutex std_mutex;
70
69
};
71
70
72
71
#endif
73
72
74
-
75
- }
73
+ } // namespace audio_tools
Original file line number Diff line number Diff line change @@ -21,20 +21,16 @@ namespace audio_tools {
21
21
class MutexRTOS : public MutexBase {
22
22
public:
23
23
MutexRTOS () {
24
- TRACED ();
25
24
xSemaphore = xSemaphoreCreateBinary ();
26
25
xSemaphoreGive (xSemaphore);
27
26
}
28
27
virtual ~MutexRTOS () {
29
- TRACED ();
30
28
vSemaphoreDelete (xSemaphore);
31
29
}
32
30
void lock () override {
33
- TRACED ();
34
31
xSemaphoreTake (xSemaphore, portMAX_DELAY);
35
32
}
36
33
void unlock () override {
37
- TRACED ();
38
34
xSemaphoreGive (xSemaphore);
39
35
}
40
36
Original file line number Diff line number Diff line change 5
5
# include " Print.h"
6
6
#endif
7
7
8
+ #include " AudioTools/Concurrency/LockGuard.h"
8
9
#if defined(RP2040)
9
- # include " AudioTools/Concurrency/RP2040.h"
10
+ # include " AudioTools/Concurrency/RP2040/MutexRP2040 .h"
10
11
#elif defined(ESP32)
11
- # include " AudioTools/Concurrency/RTOS.h"
12
- #else
13
- # include " AudioTools/Concurrency/LockGuard.h"
12
+ # include " AudioTools/Concurrency/RTOS/MutexRTOS.h"
14
13
#endif
15
14
16
15
// Logging Implementation
17
16
#if USE_AUDIO_LOGGING
18
17
19
18
namespace audio_tools {
20
19
21
- #if defined(RP2040)
20
+ #if defined(ESP32)
21
+ MutexRTOS audio_logger_mutex;
22
+ #elif defined(RP2040)
22
23
MutexRP2040 audio_logger_mutex;
23
- #elif defined(ESP32)
24
- MutexESP32 audio_logger_mutex;
25
24
#else
26
25
MutexBase audio_logger_mutex; // no locking
27
26
#endif
@@ -72,6 +71,7 @@ class AudioLogger {
72
71
fprintf ( stderr, " %s\n " , print_buffer);
73
72
#else
74
73
log_print_ptr->println (print_buffer);
74
+ log_print_ptr->flush ();
75
75
#endif
76
76
print_buffer[0 ]=0 ;
77
77
}
You can’t perform that action at this time.
0 commit comments