Skip to content

Commit ee2c9e6

Browse files
darcagnQuzarDC
authored andcommitted
Change spinlock_trylock to arch-agnostic atomic implementation
1 parent a40a182 commit ee2c9e6

2 files changed

Lines changed: 11 additions & 36 deletions

File tree

include/kos/spinlock.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,14 @@
2626
#include <kos/cdefs.h>
2727
__BEGIN_DECLS
2828

29+
#include <stdatomic.h>
2930
#include <stdbool.h>
3031
#include <kos/thread.h>
3132

3233

3334
/** \brief Spinlock data type. */
3435
typedef volatile int spinlock_t;
3536

36-
/* Include architecture-specific implementations */
37-
#include <arch/spinlock.h>
38-
3937
/** \brief Spinlock initializer.
4038
4139
All created spinlocks should be initialized with this initializer so that
@@ -67,7 +65,14 @@ static inline void spinlock_init(spinlock_t *lock) {
6765
the lock was successfully obtained.
6866
*/
6967
static inline bool spinlock_trylock(spinlock_t *lock) {
70-
return arch_spinlock_trylock(lock);
68+
int locked = 0;
69+
return atomic_compare_exchange_strong_explicit(
70+
lock,
71+
&locked,
72+
1,
73+
memory_order_acquire,
74+
memory_order_relaxed
75+
);
7176
}
7277

7378
/** \brief Spin on a lock.
Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,10 @@
11
/* KallistiOS ##version##
22
3-
arch/dreamcast/include/arch/spinlock.h
4-
Copyright (C) 2001 Megan Potter
3+
kernel/arch/dreamcast/include/arch/spinlock.h
54
65
*/
76

8-
#ifndef __KOS_SPINLOCK_H
9-
#pragma GCC warning "The `<arch/spinlock.h>` header has been moved to `<kos/spinlock.h>`."
107
#include <kos/spinlock.h>
11-
#endif
128

13-
#ifndef __ARCH_SPINLOCK_H
14-
#define __ARCH_SPINLOCK_H
9+
#warning "The `<arch/spinlock.h>` header has been moved to `<kos/spinlock.h>`."
1510

16-
/* Defines processor specific spinlock implementation */
17-
18-
#include <stdbool.h>
19-
20-
/* Use a test-and-set to attempt to acquire a lock atomically.
21-
In one instruction, tas.b writes 0x80 to the spinlock and
22-
sets T flag to 1 if if the previous value was zero, or sets
23-
0 if the previous value was non-zero. Therefore, this function
24-
returns true if we've successfully locked the spinlock, or
25-
false if the spinlock was already taken.
26-
*/
27-
28-
static inline bool arch_spinlock_trylock(spinlock_t *lock) {
29-
bool locked = false;
30-
31-
__asm__ __volatile__("tas.b @%2\n\t"
32-
"movt %0\n\t"
33-
: "=r"(locked), "=m"(*lock)
34-
: "r"(lock)
35-
: "t");
36-
37-
return locked;
38-
}
39-
40-
#endif /* __ARCH_SPINLOCK_H */

0 commit comments

Comments
 (0)