kernel: add support for basic type spinlock_t
A spinlock is the most basic type of lock. It will attempt to acquire, and upon failure, will continue attempting indefinitely until it is successful.
This commit is contained in:
parent
95ce2af5c6
commit
8e6d0d4fbc
3 changed files with 42 additions and 0 deletions
18
init/atomic.S
Normal file
18
init/atomic.S
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#define __ASM__
|
||||
|
||||
/* int atomic_cmpxchg(volatile int *ptr, int cmpval, int newval) */
|
||||
.global atomic_cmpxchg
|
||||
atomic_cmpxchg:
|
||||
/* if (*ptr == cmpval) *ptr = newval, but atomic :) */
|
||||
movl %esi, %eax
|
||||
lock cmpxchg %edx, (%rdi)
|
||||
|
||||
/* if ZF is set, the exchange was successful */
|
||||
pushfq
|
||||
popq %rax
|
||||
andl $0x40, %eax
|
||||
shrl $6, %eax
|
||||
xorl $1, %eax
|
||||
|
||||
ret
|
||||
13
init/lock.c
Normal file
13
init/lock.c
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#include <kernel/common.h>
|
||||
#include <kernel/lock.h>
|
||||
|
||||
void spinlock_acquire(spinlock_t *lock)
|
||||
{
|
||||
while (atomic_cmpxchg(lock, 0, 1))
|
||||
;
|
||||
}
|
||||
|
||||
void spinlock_release(spinlock_t *lock)
|
||||
{
|
||||
atomic_cmpxchg(lock, 1, 0);
|
||||
}
|
||||
Loading…
Reference in a new issue