coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
spinlock.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #ifndef ARCH_SMP_SPINLOCK_H
4 #define ARCH_SMP_SPINLOCK_H
5 
6 #include <thread.h>
7 
8 /*
9  * Your basic SMP spinlocks, allowing only a single CPU anywhere
10  */
11 
12 typedef struct {
13  volatile unsigned int lock;
14 } spinlock_t;
15 
16 #define SPIN_LOCK_UNLOCKED { 1 }
17 
18 #define DECLARE_SPIN_LOCK(x) \
19  static spinlock_t x = SPIN_LOCK_UNLOCKED;
20 
21 /*
22  * Simple spin lock operations. There are two variants, one clears IRQ's
23  * on the local processor, one does not.
24  *
25  * We make no fairness assumptions. They have a cost.
26  */
27 #define barrier() __asm__ __volatile__("" : : : "memory")
28 #define spin_is_locked(x) (*(volatile int *)(&(x)->lock) <= 0)
29 #define spin_unlock_wait(x) do { barrier(); } while (spin_is_locked(x))
30 #undef barrier
31 
32 #define spin_lock_string \
33  "\n1:\t" \
34  "lock ; decl %0\n\t" \
35  "js 2f\n" \
36  ".section .text.lock,\"ax\"\n" \
37  "2:\t" \
38  "cmpl $0,%0\n\t" \
39  "rep;nop\n\t" \
40  "jle 2b\n\t" \
41  "jmp 1b\n" \
42  ".previous"
43 
44 /*
45  * This works. Despite all the confusion.
46  */
47 #define spin_unlock_string \
48  "movl $1,%0"
49 
51 {
52  __asm__ __volatile__(
54  : "=m" (lock->lock) : : "memory");
55 
56  /* Switching contexts while holding a spinlock will lead to deadlocks */
58 
59 }
60 
62 {
64 
65  __asm__ __volatile__(
67  : "=m" (lock->lock) : : "memory");
68 }
69 
70 #endif /* ARCH_SMP_SPINLOCK_H */
#define spin_lock_string
Definition: spinlock.h:32
static __always_inline void spin_unlock(spinlock_t *lock)
Definition: spinlock.h:61
#define spin_unlock_string
Definition: spinlock.h:47
static __always_inline void spin_lock(spinlock_t *lock)
Definition: spinlock.h:50
#define __always_inline
Definition: compiler.h:35
void thread_coop_enable(void)
Definition: thread.c:358
void thread_coop_disable(void)
Definition: thread.c:372
static void lock(void *unused)
Definition: lockdown.c:58
volatile unsigned int lock
Definition: spinlock.h:13