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
62
63
64
65
66
67
68
69
70
|
; This contains the CPU-dependant variants of some routines.
; (C) 2005 Andreas Rumpf
; This code was inspired by the Freepascal compiler's sources
; All routines here have the _cdecl calling convention because
; that is the only convention any C compiler supports.
\python{
# as usual I use my own preprocessor :-)
import os
def c(name):
if os.name == 'posix':
return name
else:
return "_" + name
}
segment code
global \c{cpu_inc_locked}
global \c{cpu_dec_locked}
global \c{cpu_lock}
global \c{cpu_unlock}
\c{cpu_dec_locked}:
push ebp
mov ebp,esp
mov eax,[ebp+8] ; first parameter to function
lock dec dword [eax]
setz al
mov esp,ebp
pop ebp
ret
\c{cpu_inc_locked}:
push ebp
mov ebp,esp
mov eax,[ebp+8] ; first parameter to function
lock inc dword [eax]
mov esp,ebp
pop ebp
ret
; This code uses the highest bit of the RC to indicate that the RC is
; locked (spinlock).
\c{cpu_lock}
push ebp
mov ebp, esp
mov eax, [ebp+8] ; first parameter to function
mov edx, [eax] ; load RC
or edx, 0x80000000 ; set highest bit
spin:
xchg [eax], edx ; atomic instruction!
pause ; wait a few cycles
and edx, 0x80000000 ; mask highest bit
jnz spin
mov esp, ebp
pop ebp
ret
\c{cpu_unlock}
push ebp
mov ebp, esp
mov eax, [ebp+8] ; first parameter to function
mov edx, [eax] ; load RC
and edx, 0x7FFFFFFF ; unset highest bit
xchg [eax], edx ; atomic instruction!
mov esp, ebp
pop ebp
ret
|