lib: move lock/atomic into lib folder
This commit is contained in:
parent
f482174458
commit
e4116bd3e8
2 changed files with 0 additions and 0 deletions
17
lib/atomic.S
Normal file
17
lib/atomic.S
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
/* 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 */
|
||||
jnz nz
|
||||
movq $0, %rax
|
||||
ret
|
||||
nz:
|
||||
movq $1, %rax
|
||||
ret
|
||||
13
lib/lock.c
Normal file
13
lib/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