@@ -18,9 +18,9 @@ const (
18
18
19
19
// Due to hardware errata RP2350-E2 spinlocks are emulated in software as in pico-sdk.
20
20
type spinLock struct {
21
- // lock field must be first field so its address is the same as the struct address.
22
- lock uint8
23
- id uint8
21
+ // state field must be first field so its address is the same as the struct address.
22
+ state uint8
23
+ id uint8
24
24
uint16 // Padding to prevent false sharing
25
25
}
26
26
@@ -29,28 +29,28 @@ func (l *spinLock) Lock() {
29
29
// https://github.com/raspberrypi/pico-sdk/blob/2.2.0/src/rp2_common/hardware_sync_spin_lock/include/hardware/sync/spin_lock.h#L112
30
30
31
31
// r0 is automatically filled with the pointer value "l" here.
32
- // We create a variable to allow access the lock byte and avoid a memory
33
- // fault when accessing l.lock in assembly.
34
- lock := & l .lock
35
- _ = lock
32
+ // We create a variable to permit access to the state byte (l.state) and
33
+ // avoid a memory fault when accessing it in assembly.
34
+ state := & l .state
35
+ _ = state
36
36
37
- // Set a loop start point
37
+ // Set the loop start point.
38
38
arm .Asm ("1:" )
39
- // Exclusively load the state variable (l.lock) and put its value in r2.
39
+ // Exclusively load (lock) the state byte and put its value in r2.
40
40
arm .Asm ("ldaexb r2, [r0]" )
41
- // Store the "locked" state value (1) into r1 to keep things moving .
41
+ // Set the r1 register to '1' for later use .
42
42
arm .Asm ("movs r1, #1" )
43
43
// Check if the lock was already taken (r2 != 0).
44
44
arm .Asm ("cmp r2, #0" )
45
- // Jump back to "1:" if the lock is already held
45
+ // Jump back to the loop start ( "1:") if the lock is already held.
46
46
arm .Asm ("bne 1b" )
47
47
48
- // Attempt to store '1' into the lock address .
48
+ // Attempt to store '1' into the lock state byte .
49
49
// The return code (0 for success, 1 for failure) is placed in r2.
50
50
arm .Asm ("strexb r2, r1, [r0]" )
51
51
// Check if the result was successful (r2 == 0).
52
52
arm .Asm ("cmp r2, #0" )
53
- // Jump back to "1:" if the lock was not acquired.
53
+ // Jump back to the loop start ( "1:") if the lock was not acquired.
54
54
arm .Asm ("bne 1b" )
55
55
56
56
// Memory barrier to ensure everyone knows we're holding the lock now.
@@ -62,13 +62,13 @@ func (l *spinLock) Unlock() {
62
62
// https://github.com/raspberrypi/pico-sdk/blob/2.2.0/src/rp2_common/hardware_sync_spin_lock/include/hardware/sync/spin_lock.h#L197
63
63
64
64
// r0 is automatically filled with the pointer value l here.
65
- // We create a variable to allow access the lock byte and avoid a memory
66
- // fault when accessing l.lock in assembly.
67
- lock := & l .lock
68
- _ = lock
69
- // Fill r1 with 0 and store it to the lock address in r0. stlb requires a
70
- // register as a source so we can't use a literal 0 directly.
65
+ // We create a variable to permit access to the state byte (l.state) and
66
+ // avoid a memory fault when accessing it in assembly.
67
+ state := & l .state
68
+ _ = state
69
+ // Fill r1 with 0 and store it to the state byte address in r0. stlb
70
+ // requires a register as a source so we can't use a literal 0 directly.
71
71
arm .Asm ("movs r1, #0" )
72
- // Release the pseudo-spinlock by writing 0 to and releasing l.lock .
72
+ // Release the pseudo-spinlock by writing 0 to and releasing l.state .
73
73
arm .Asm ("stlb r1, [r0]" )
74
74
}
0 commit comments