-
Notifications
You must be signed in to change notification settings - Fork 1
/
spin_lock.S
50 lines (43 loc) · 1.29 KB
/
spin_lock.S
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "apple-linux-convergence.S"
.p2align 2
.text
/* Demonstration of use of load-linked and store-conditional doing
something interesting. In this case, creating a spin lock. A spin
lock is a simple but grossly inefficient form of a mutex. If the
"lock" is found to be owned (non-zero) by someone else, the calling
thread spins - checking the ownership of the "lock" in a tight loop.
The spinning uses up time. A better mutex would add some kind of
queing for threads that don't own the lock. And, some kind of waking
would also be needed. Threads on the queue would be "asleep."
*/
#if defined(__APPLE__)
.global _Lock
.global _Unlock
#else
.global Lock
.global Unlock
#endif
#if defined(__APPLE__)
_Lock:
#else
Lock:
#endif
START_PROC
mov w3, 1
1: ldaxr w1, [x0]
cbnz w1, 1b // lock taken - spin.
stlxr w2, w3, [x0]
cbnz w2, 1b // shucks - somebody meddled.
ret
END_PROC
#if defined(__APPLE__)
_Unlock:
#else
Unlock:
#endif
START_PROC
str wzr, [x0]
dmb ish // ensure all cores are aware.
ret
END_PROC
.end