Browse Source
These allow entering/exiting a mutex and also disabling/restoring interrupts, in an atomic way. Signed-off-by: Damien George <damien@micropython.org>pull/13312/head
Damien George
10 months ago
4 changed files with 67 additions and 0 deletions
@ -0,0 +1,30 @@ |
|||
/*
|
|||
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd. |
|||
* |
|||
* SPDX-License-Identifier: BSD-3-Clause |
|||
*/ |
|||
|
|||
#include "mutex_extra.h" |
|||
|
|||
// These functions are taken from lib/pico-sdk/src/common/pico_sync/mutex.c and modified
|
|||
// so that they atomically obtain the mutex and disable interrupts.
|
|||
|
|||
uint32_t __time_critical_func(mutex_enter_blocking_and_disable_interrupts)(mutex_t * mtx) { |
|||
lock_owner_id_t caller = lock_get_caller_owner_id(); |
|||
do { |
|||
uint32_t save = spin_lock_blocking(mtx->core.spin_lock); |
|||
if (!lock_is_owner_id_valid(mtx->owner)) { |
|||
mtx->owner = caller; |
|||
spin_unlock_unsafe(mtx->core.spin_lock); |
|||
return save; |
|||
} |
|||
lock_internal_spin_unlock_with_wait(&mtx->core, save); |
|||
} while (true); |
|||
} |
|||
|
|||
void __time_critical_func(mutex_exit_and_restore_interrupts)(mutex_t * mtx, uint32_t save) { |
|||
spin_lock_unsafe_blocking(mtx->core.spin_lock); |
|||
assert(lock_is_owner_id_valid(mtx->owner)); |
|||
mtx->owner = LOCK_INVALID_OWNER_ID; |
|||
lock_internal_spin_unlock_with_notify(&mtx->core, save); |
|||
} |
@ -0,0 +1,34 @@ |
|||
/*
|
|||
* This file is part of the MicroPython project, http://micropython.org/
|
|||
* |
|||
* The MIT License (MIT) |
|||
* |
|||
* Copyright (c) 2023 Damien P. George |
|||
* |
|||
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|||
* of this software and associated documentation files (the "Software"), to deal |
|||
* in the Software without restriction, including without limitation the rights |
|||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|||
* copies of the Software, and to permit persons to whom the Software is |
|||
* furnished to do so, subject to the following conditions: |
|||
* |
|||
* The above copyright notice and this permission notice shall be included in |
|||
* all copies or substantial portions of the Software. |
|||
* |
|||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|||
* THE SOFTWARE. |
|||
*/ |
|||
#ifndef MICROPY_INCLUDED_RP2_MUTEX_EXTRA_H |
|||
#define MICROPY_INCLUDED_RP2_MUTEX_EXTRA_H |
|||
|
|||
#include "pico/mutex.h" |
|||
|
|||
uint32_t mutex_enter_blocking_and_disable_interrupts(mutex_t *mtx); |
|||
void mutex_exit_and_restore_interrupts(mutex_t *mtx, uint32_t save); |
|||
|
|||
#endif // MICROPY_INCLUDED_RP2_MUTEX_EXTRA_H
|
Loading…
Reference in new issue