|
| 1 | +/* |
| 2 | + Newlib retargetable lock class using RP2040 SDK |
| 3 | +
|
| 4 | + Overrides weak functions in Newlib for locking to safely support |
| 5 | + multi-core operation. Does not need any --wrap for memory allocators. |
| 6 | +
|
| 7 | + Copyright (c) 2022 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 | + |
| 25 | +#include <pico/mutex.h> |
| 26 | +#include <sys/lock.h> |
| 27 | + |
| 28 | +recursive_mutex_t __lock___sinit_recursive_mutex; |
| 29 | +recursive_mutex_t __lock___sfp_recursive_mutex; |
| 30 | +recursive_mutex_t __lock___atexit_recursive_mutex; |
| 31 | +mutex_t __lock___at_quick_exit_mutex; |
| 32 | +recursive_mutex_t __lock___malloc_recursive_mutex; |
| 33 | +recursive_mutex_t __lock___env_recursive_mutex; |
| 34 | +mutex_t __lock___tz_mutex; |
| 35 | +mutex_t __lock___dd_hash_mutex; |
| 36 | +mutex_t __lock___arc4random_mutex; |
| 37 | + |
| 38 | + |
| 39 | +__attribute__((constructor)) void __init_all_newlib_mutexes() { |
| 40 | + recursive_mutex_init(&__lock___sinit_recursive_mutex); |
| 41 | + recursive_mutex_init(&__lock___sfp_recursive_mutex); |
| 42 | + recursive_mutex_init(&__lock___atexit_recursive_mutex); |
| 43 | + mutex_init(&__lock___at_quick_exit_mutex); |
| 44 | + recursive_mutex_init(&__lock___malloc_recursive_mutex); |
| 45 | + recursive_mutex_init(&__lock___env_recursive_mutex); |
| 46 | + mutex_init(&__lock___tz_mutex); |
| 47 | + mutex_init(&__lock___dd_hash_mutex); |
| 48 | + mutex_init(&__lock___arc4random_mutex); |
| 49 | +} |
| 50 | + |
| 51 | +void __retarget_lock_init(_LOCK_T *lock) { |
| 52 | + mutex_init((mutex_t*) lock); |
| 53 | +} |
| 54 | + |
| 55 | +void __retarget_lock_init_recursive(_LOCK_T *lock) { |
| 56 | + recursive_mutex_init((recursive_mutex_t*) lock); |
| 57 | +} |
| 58 | + |
| 59 | +void __retarget_lock_close(_LOCK_T lock) { |
| 60 | + (void) lock; |
| 61 | +} |
| 62 | + |
| 63 | +void __retarget_lock_close_recursive(_LOCK_T lock) { |
| 64 | + (void) lock; |
| 65 | +} |
| 66 | + |
| 67 | +void __retarget_lock_acquire(_LOCK_T lock) { |
| 68 | + mutex_enter_blocking((mutex_t*)lock); |
| 69 | +} |
| 70 | + |
| 71 | +void __retarget_lock_acquire_recursive(_LOCK_T lock) { |
| 72 | + recursive_mutex_enter_blocking((recursive_mutex_t*)lock); |
| 73 | +} |
| 74 | + |
| 75 | +int __retarget_lock_try_acquire(_LOCK_T lock) { |
| 76 | + return mutex_try_enter((mutex_t *)lock, NULL); |
| 77 | +} |
| 78 | + |
| 79 | +int __retarget_lock_try_acquire_recursive(_LOCK_T lock) { |
| 80 | + return recursive_mutex_try_enter((recursive_mutex_t*)lock, NULL); |
| 81 | +} |
| 82 | + |
| 83 | +void __retarget_lock_release(_LOCK_T lock) { |
| 84 | + mutex_exit((mutex_t*)lock); |
| 85 | +} |
| 86 | + |
| 87 | +void __retarget_lock_release_recursive(_LOCK_T lock) { |
| 88 | + recursive_mutex_exit((recursive_mutex_t*)lock); |
| 89 | +} |
0 commit comments