8000 rp2/mutex_extra: Implement additional mutex functions. · micropython/micropython@8438c87 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8438c87

Browse files
committed
rp2/mutex_extra: Implement additional mutex functions.
These allow entering/exiting a mutex and also disabling/restoring interrupts, in an atomic way. Signed-off-by: Damien George <damien@micropython.org>
1 parent c3989e3 commit 8438c87

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
lines changed

LICENSE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ used during the build process and is not part of the compiled source code.
6969
/FreeRTOS (GPL-2.0 with FreeRTOS exception)
7070
/esp32
7171
/ppp_set_auth.* (Apache-2.0)
72+
/rp2
73+
/mutex_extra.c (BSD-3-clause)
7274
/stm32
7375
/usbd*.c (MCD-ST Liberty SW License Agreement V2)
7476
/stm32_it.* (MIT + BSD-3-clause)

ports/rp2/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ set(MICROPY_SOURCE_PORT
129129
mphalport.c
130130
mpnetworkport.c
131131
mpthreadport.c
132+
mutex_extra.c
132133
pendsv.c
133134
rp2_flash.c
134135
rp2_pio.c

ports/rp2/mutex_extra.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
3+
*
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
7+
#include "mutex_extra.h"
8+
9+
// These functions are taken from lib/pico-sdk/src/common/pico_sync/mutex.c and modified
10+
// so that they atomically obtain the mutex and disable interrupts.
11+
12+
uint32_t __time_critical_func(mutex_enter_blocking_and_disable_interrupts)(mutex_t * mtx) {
13+
lock_owner_id_t caller = lock_get_caller_owner_id();
14+
do {
15+
uint32_t save = spin_lock_blocking(mtx->core.spin_lock);
16+
if (!lock_is_owner_id_valid(mtx->owner)) {
17+
mtx->owner = caller;
18+
spin_unlock_unsafe(mtx->core.spin_lock);
19+
return save;
20+
}
21+
lock_internal_spin_unlock_with_wait(&mtx->core, save);
22+
} while (true);
23+
}
24+
25+
void __time_critical_func(mutex_exit_and_restore_interrupts)(mutex_t * mtx, uint32_t save) {
26+
spin_lock_unsafe_blocking(mtx->core.spin_lock);
27+
assert(lock_is_owner_id_valid(mtx->owner));
28+
mtx->owner = LOCK_INVALID_OWNER_ID;
29+
lock_internal_spin_unlock_with_notify(&mtx->core, save);
30+
}

ports/rp2/mutex_extra.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2023 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, in BCF2 cluding without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
#ifndef MICROPY_INCLUDED_RP2_MUTEX_EXTRA_H
27+
#define MICROPY_INCLUDED_RP2_MUTEX_EXTRA_H
28+
29+
#include "pico/mutex.h"
30+
31+
uint32_t mutex_enter_blocking_and_disable_interrupts(mutex_t *mtx);
32+
void mutex_exit_and_restore_interrupts(mutex_t *mtx, uint32_t save);
33+
34+
#endif // MICROPY_INCLUDED_RP2_MUTEX_EXTRA_H

0 commit comments

Comments
 (0)
0