Skip to content

Commit 4cc6c36

Browse files
Fix Serial1/2 debug output mode in CoreMutex (#883)
Fixes #882
1 parent 0cd5b0a commit 4cc6c36

File tree

2 files changed

+59
-31
lines changed

2 files changed

+59
-31
lines changed

cores/rp2040/CoreMutex.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
CoreMutex for the Raspberry Pi Pico RP2040
3+
4+
Implements a deadlock-safe multicore mutex for sharing things like the
5+
USB or UARTs.
6+
7+
Copyright (c) 2021 Earle F. Philhower, III <[email protected]>
8+
9+
This library is free software; you can redistribute it and/or
10+
modify it under the terms of the GNU Lesser General Public
11+
License as published by the Free Software Foundation; either
12+
version 2.1 of the License, or (at your option) any later version.
13+
14+
This library is distributed in the hope that it will be useful,
15+
but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17+
Lesser General Public License for more details.
18+
19+
You should have received a copy of the GNU Lesser General Public
20+
License along with this library; if not, write to the Free Software
21+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22+
*/
23+
24+
#include "Arduino.h"
25+
#include "CoreMutex.h"
26+
27+
CoreMutex::CoreMutex(mutex_t *mutex, bool debugEnable) {
28+
_mutex = mutex;
29+
_acquired = false;
30+
if (__isFreeRTOS) {
31+
auto m = __get_freertos_mutex_for_ptr(mutex);
32+
__freertos_mutex_take(m);
33+
} else {
34+
uint32_t owner;
35+
if (!mutex_try_enter(_mutex, &owner)) {
36+
if (owner == get_core_num()) { // Deadlock!
37+
if (debugEnable) {
38+
DEBUGCORE("CoreMutex - Deadlock detected!\n");
39+
}
40+
return;
41+
}
42+
mutex_enter_blocking(_mutex);
43+
}
44+
}
45+
_acquired = true;
46+
}
47+
48+
CoreMutex::~CoreMutex() {
49+
if (_acquired) {
50+
if (__isFreeRTOS) {
51+
auto m = __get_freertos_mutex_for_ptr(_mutex);
52+
__freertos_mutex_give(m);
53+
} else {
54+
mutex_exit(_mutex);
55+
}
56+
}
57+
}

cores/rp2040/CoreMutex.h

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,37 +28,8 @@
2828

2929
class CoreMutex {
3030
public:
31-
CoreMutex(mutex_t *mutex, bool debugEnable = true) {
32-
_mutex = mutex;
33-
_acquired = false;
34-
if (__isFreeRTOS) {
35-
auto m = __get_freertos_mutex_for_ptr(mutex);
36-
__freertos_mutex_take(m);
37-
} else {
38-
uint32_t owner;
39-
if (!mutex_try_enter(_mutex, &owner)) {
40-
if (owner == get_core_num()) { // Deadlock!
41-
if (debugEnable) {
42-
DEBUGCORE("CoreMutex - Deadlock detected!\n");
43-
}
44-
return;
45-
}
46-
mutex_enter_blocking(_mutex);
47-
}
48-
}
49-
_acquired = true;
50-
}
51-
52-
~CoreMutex() {
53-
if (_acquired) {
54-
if (__isFreeRTOS) {
55-
auto m = __get_freertos_mutex_for_ptr(_mutex);
56-
__freertos_mutex_give(m);
57-
} else {
58-
mutex_exit(_mutex);
59-
}
60-
}
61-
}
31+
CoreMutex(mutex_t *mutex, bool debugEnable = true);
32+
~CoreMutex();
6233

6334
operator bool() {
6435
return _acquired;

0 commit comments

Comments
 (0)