1 /*
2  * Copyright (c) 2018,2024 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #include <kernel_internal.h>
7 #include <zephyr/spinlock.h>
8 
z_spin_lock_valid(struct k_spinlock * l)9 bool z_spin_lock_valid(struct k_spinlock *l)
10 {
11 	uintptr_t thread_cpu = l->thread_cpu;
12 
13 	if (thread_cpu != 0U) {
14 		if ((thread_cpu & 3U) == _current_cpu->id) {
15 			return false;
16 		}
17 	}
18 	return true;
19 }
20 
z_spin_unlock_valid(struct k_spinlock * l)21 bool z_spin_unlock_valid(struct k_spinlock *l)
22 {
23 	uintptr_t tcpu = l->thread_cpu;
24 
25 	l->thread_cpu = 0;
26 
27 	if (arch_is_in_isr() && _current->base.thread_state & _THREAD_DUMMY) {
28 		/* Edge case where an ISR aborted _current */
29 		return true;
30 	}
31 	if (tcpu != (_current_cpu->id | (uintptr_t)_current)) {
32 		return false;
33 	}
34 	return true;
35 }
36 
z_spin_lock_set_owner(struct k_spinlock * l)37 void z_spin_lock_set_owner(struct k_spinlock *l)
38 {
39 	l->thread_cpu = _current_cpu->id | (uintptr_t)_current;
40 }
41 
42 #ifdef CONFIG_KERNEL_COHERENCE
z_spin_lock_mem_coherent(struct k_spinlock * l)43 bool z_spin_lock_mem_coherent(struct k_spinlock *l)
44 {
45 	return arch_mem_coherent((void *)l);
46 }
47 #endif /* CONFIG_KERNEL_COHERENCE */
48