mirror of
https://github.com/pkivolowitz/asm_book.git
synced 2026-06-21 02:26:59 +08:00
updated dmb
This commit is contained in:
parent
c2fe010bc1
commit
92670f45f0
3 changed files with 25 additions and 21 deletions
|
|
@ -105,25 +105,24 @@ Bummer.
|
||||||
Here is the source code to the spin-lock for ARM V8.
|
Here is the source code to the spin-lock for ARM V8.
|
||||||
|
|
||||||
```text
|
```text
|
||||||
#if defined(__APPLE__) // 1
|
#if defined(__APPLE__) // 1
|
||||||
_Lock: // 2
|
_Lock: // 2
|
||||||
#else // 3
|
#else // 3
|
||||||
Lock: // 4
|
Lock: // 4
|
||||||
#endif // 5
|
#endif // 5
|
||||||
START_PROC // 6
|
START_PROC // 6
|
||||||
1: ldaxr w1, [x0] // 7
|
mov w3, 1 // 7
|
||||||
cbnz w1, 1b // lock taken - spin. // 8
|
1: ldaxr w1, [x0] // 8
|
||||||
add w1, w1, 1 // 9
|
cbnz w1, 1b // lock taken - spin. // 9
|
||||||
stlxr w2, w1, [x0] // 10
|
stlxr w2, w3, [x0] // 10
|
||||||
cbnz w2, 1b // shucks - somebody meddled. // 11
|
cbnz w2, 1b // shucks - somebody meddled. // 11
|
||||||
// considered using dmb here // 12
|
ret // 12
|
||||||
ret // 13
|
END_PROC // 13
|
||||||
END_PROC // 14
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Once again, line 7 does a `ldaxr` dereferencing the lock itself (once
|
Line 8 does a `ldaxr` dereferencing the lock itself (once again an
|
||||||
again an `int32_t`) and marks the location of the lock as being
|
`int32_t`) and marks the location of the lock as being hopefully,
|
||||||
hopefully, exclusive.
|
exclusive.
|
||||||
|
|
||||||
Having gotten the value of the lock, on line 8, its value is inspected
|
Having gotten the value of the lock, on line 8, its value is inspected
|
||||||
and if found to be non-zero, we branch back to attempting to get it
|
and if found to be non-zero, we branch back to attempting to get it
|
||||||
|
|
@ -149,7 +148,7 @@ Unlock: // 4
|
||||||
#endif // 5
|
#endif // 5
|
||||||
START_PROC // 6
|
START_PROC // 6
|
||||||
str wzr, [x0] // 7
|
str wzr, [x0] // 7
|
||||||
// considered using dmb here // 8
|
dmb ish // 8
|
||||||
ret // 9
|
ret // 9
|
||||||
END_PROC // 10
|
END_PROC // 10
|
||||||
```
|
```
|
||||||
|
|
@ -159,5 +158,11 @@ of the lock requires that no bad actor simply stomps on the lock by
|
||||||
calling `Unlock` without first owning the lock. Just say no to lock
|
calling `Unlock` without first owning the lock. Just say no to lock
|
||||||
stompers.
|
stompers.
|
||||||
|
|
||||||
|
Line 8 sets up a data memory barrier across each processor - it makes
|
||||||
|
sure threads running on different cores see the update correctly. This
|
||||||
|
code seemed to work without this line but intuition suggests it could
|
||||||
|
be important. In `Lock()` the `stlxr` instruction has an implied data
|
||||||
|
memory barrier.
|
||||||
|
|
||||||
Please see the source code located [here](./spin_lock.S) for some
|
Please see the source code located [here](./spin_lock.S) for some
|
||||||
additional comments regarding the implementation.
|
additional comments regarding the implementation.
|
||||||
|
|
|
||||||
Binary file not shown.
|
|
@ -33,7 +33,6 @@ Lock:
|
||||||
cbnz w1, 1b // lock taken - spin.
|
cbnz w1, 1b // lock taken - spin.
|
||||||
stlxr w2, w3, [x0]
|
stlxr w2, w3, [x0]
|
||||||
cbnz w2, 1b // shucks - somebody meddled.
|
cbnz w2, 1b // shucks - somebody meddled.
|
||||||
// considered using dmb here
|
|
||||||
ret
|
ret
|
||||||
END_PROC
|
END_PROC
|
||||||
|
|
||||||
|
|
@ -44,8 +43,8 @@ Unlock:
|
||||||
#endif
|
#endif
|
||||||
START_PROC
|
START_PROC
|
||||||
str wzr, [x0]
|
str wzr, [x0]
|
||||||
// considered using dmb here
|
dmb ish // ensure all cores are aware.
|
||||||
ret
|
ret
|
||||||
END_PROC
|
END_PROC
|
||||||
|
|
||||||
.end
|
.end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue