1 /* 2 * Copyright (c) 2025 Google Inc. All rights reserved 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining 5 * a copy of this software and associated documentation files 6 * (the "Software"), to deal in the Software without restriction, 7 * including without limitation the rights to use, copy, modify, merge, 8 * publish, distribute, sublicense, and/or sell copies of the Software, 9 * and to permit persons to whom the Software is furnished to do so, 10 * subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice shall be 13 * included in all copies or substantial portions of the Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 22 */ 23 24 #pragma once 25 26 #include <kernel/spinlock.h> 27 28 /** 29 * Can be safely called in any context. 30 */ 31 void lk_interrupt_save(spin_lock_saved_state_t *statep, 32 spin_lock_save_flags_t flags); 33 34 /** 35 * Can be safely called in any context. 36 * 37 * `state` should be from the corresponding call to `lk_interrupt_save`, 38 * and `flags` should be the same `flags` from that `lk_interrupt_save` call. 39 */ 40 void lk_interrupt_restore(spin_lock_saved_state_t old_state, 41 spin_lock_save_flags_t flags); 42 43 /** 44 * Can be safely called in any context. 45 */ 46 bool lk_ints_disabled(void); 47 48 #if defined(__arm__) || defined(__aarch64__) 49 /** 50 * Can be safely called in any context. 51 */ 52 bool lk_fiqs_disabled(void); 53 #endif 54 55 /** 56 * Interrupts must be disabled ([`InterruptState::save`]) before calling this, 57 * or else this might deadlock. 58 * 59 * # Safety 60 * 61 * `lock` must have been initialized with [`SPIN_LOCK_INITIAL_VALUE`]. 62 * 63 * This function is thread-safe and can be called concurrently from multiple threads. 64 * Only one thread is guaranteed to successfully lock it at a time. 65 */ 66 void lk_spin_lock(spin_lock_t *lock); 67 68 /** 69 * # Safety 70 * 71 * Same as `lk_spin_lock`. 72 */ 73 int lk_spin_trylock(spin_lock_t *lock); 74 75 /** 76 * # Safety 77 * 78 * `lock` must already be locked by either `lk_spin_lock` or `lk_spin_trylock`. 79 */ 80 void lk_spin_unlock(spin_lock_t *lock); 81