@@ -18,9 +18,9 @@ const (
1818
1919// Due to hardware errata RP2350-E2 spinlocks are emulated in software as in pico-sdk.
2020type 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
2424 uint16 // Padding to prevent false sharing
2525}
2626
@@ -29,28 +29,28 @@ func (l *spinLock) Lock() {
2929 // https://github.com/raspberrypi/pico-sdk/blob/2.2.0/src/rp2_common/hardware_sync_spin_lock/include/hardware/sync/spin_lock.h#L112
3030
3131 // 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
3636
37- // Set a loop start point
37+ // Set the loop start point.
3838 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.
4040 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 .
4242 arm .Asm ("movs r1, #1" )
4343 // Check if the lock was already taken (r2 != 0).
4444 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.
4646 arm .Asm ("bne 1b" )
4747
48- // Attempt to store '1' into the lock address .
48+ // Attempt to store '1' into the lock state byte .
4949 // The return code (0 for success, 1 for failure) is placed in r2.
5050 arm .Asm ("strexb r2, r1, [r0]" )
5151 // Check if the result was successful (r2 == 0).
5252 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.
5454 arm .Asm ("bne 1b" )
5555
5656 // Memory barrier to ensure everyone knows we're holding the lock now.
@@ -62,13 +62,13 @@ func (l *spinLock) Unlock() {
6262 // https://github.com/raspberrypi/pico-sdk/blob/2.2.0/src/rp2_common/hardware_sync_spin_lock/include/hardware/sync/spin_lock.h#L197
6363
6464 // 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.
7171 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 .
7373 arm .Asm ("stlb r1, [r0]" )
7474}
0 commit comments