-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlocking.fth
More file actions
61 lines (55 loc) · 1.55 KB
/
locking.fth
File metadata and controls
61 lines (55 loc) · 1.55 KB
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
51
52
53
54
55
56
57
58
59
60
61
((
#****h* cm3/getlock
# SYNOPSIS
#
# USAGE
# addr val -- status
#
# Get a lock using the Cortex-M3 LDREX/STREX primitives.
# Takes in the address of the lock, and returns true
# or false, depending on whether or not the lock was
# a success. Adapted from the arm docs
#
# On the Stellaris M3/M4 parts, LDREX/STREX will fail if
# an exception slips in there between LDREX and STREX
#
# This can also be done with bit-banded bits, but LDREX/STREX
# a: don't require the pre-allocation of bits
# b: can use an arbitrary lock value to give you meaningful information about who has the lock.
#***
# Read Arm DHT0008A (ID081709)
# Note that parts with multiple CPUs require a DMB
#
))
code GETLOCK \ addr val -- t/f
mov r0, tos \ Save a copy of 'val'
ldr tos, [ psp ], # 4 \ Refresh TOS.
L$1: ldrex r1, [ tos ] \ Load the lock value
cmp r1, # 0 \ Is the lock free?
b .eq L$2 \ If so, jump to the strex
clrex \ Clear the monitor
mov tos, # 0
bx LR \ Bail.
L$2: strex r2, r0, [ tos ] \ Try and claim the lock
\ 0 is success, 1 is fail.
cmp r2, # 0
b .ne L$1
mov tos, # -1
next,
end-code
: releaselock ( addr -- )
0 SWAP !
;
(( I Don't know where else to put this. ))
\ Safely read a 64-bit counter.
CODE D@SAFE
str tos, [ psp, # $-04 ] ! \ Push a little
L$3: ldr r0, [ tos, # 4 ] \ Get the MSB
ldr r1, [ tos, # 0 ] \ LSB
ldr r2, [ tos, # 4 ] \ Check value
cmp r0, r2
b .ne L$3
str r1, [ psp, # 0 ]
mov tos, r0
next,
END-CODE