1 | ; $Id: interlock.asm,v 1.9 2001-05-28 12:39:33 phaller Exp $
|
---|
2 |
|
---|
3 | ;/*
|
---|
4 | ; * Interlocked apis
|
---|
5 | ; *
|
---|
6 | ; * Copyright 1999 Sander van Leeuwen
|
---|
7 | ; * Copyright 1999 Patrick Haller
|
---|
8 | ; *
|
---|
9 | ; * Based on WINE code: win32\thread.c (990815)
|
---|
10 | ; *
|
---|
11 | ; * Copyright 1995 Martin von Loewis
|
---|
12 | ; * Copyright 1997 Onno Hovers
|
---|
13 | ; *
|
---|
14 | ; * Project Odin Software License can be found in LICENSE.TXT
|
---|
15 | ; *
|
---|
16 | ; */
|
---|
17 | .586P
|
---|
18 | NAME interlocked
|
---|
19 |
|
---|
20 | CODE32 SEGMENT DWORD PUBLIC USE32 'CODE'
|
---|
21 |
|
---|
22 | public _InterlockedIncrement@4
|
---|
23 |
|
---|
24 | ;* InterlockedIncrement [KERNEL32] *
|
---|
25 | ;* *
|
---|
26 | ;* InterlockedIncrement adds 1 to a long variable and returns *
|
---|
27 | ;* - a negative number if the result < 0 *
|
---|
28 | ;* - zero if the result == 0 *
|
---|
29 | ;* - a positive number if the result > 0 *
|
---|
30 | ;* *
|
---|
31 | ;* The returned number need not be equal to the result!!!! *
|
---|
32 |
|
---|
33 | _InterlockedIncrement@4 proc near
|
---|
34 | mov eax, dword ptr [esp+4] ; LPLONG lpAddend
|
---|
35 | lock inc dword ptr [eax]
|
---|
36 | mov eax, 0 ; Note: must be "MOV eax,0"
|
---|
37 | ; instead of faster "XOR eax,eax"
|
---|
38 | ; because MOV doesn't tough any
|
---|
39 | ; flag register.
|
---|
40 |
|
---|
41 | je @end ; not incremented?
|
---|
42 | jl @decr
|
---|
43 | inc eax ; ?
|
---|
44 | jmp @end
|
---|
45 | @decr: dec eax ; ?
|
---|
46 | @end: ret 4
|
---|
47 | _InterlockedIncrement@4 endp
|
---|
48 |
|
---|
49 | public _InterlockedDecrement@4
|
---|
50 |
|
---|
51 | ;* InterlockedDecrement [KERNEL32] *
|
---|
52 | ;* *
|
---|
53 | ;* InterlockedIncrement adds 1 to a long variable and returns *
|
---|
54 | ;* - a negative number if the result < 0 *
|
---|
55 | ;* - zero if the result == 0 *
|
---|
56 | ;* - a positive number if the result > 0 *
|
---|
57 | ;* *
|
---|
58 | ;* The returned number need not be equal to the result!!!! *
|
---|
59 |
|
---|
60 | _InterlockedDecrement@4 proc near
|
---|
61 | mov eax, dword ptr [esp+4] ; LPLONG lpAddend
|
---|
62 | lock dec dword ptr [eax]
|
---|
63 | mov eax, 0 ; Note: must be "MOV eax,0"
|
---|
64 | ; instead of faster "XOR eax,eax"
|
---|
65 | ; because MOV doesn't tough any
|
---|
66 | ; flag register.
|
---|
67 | je @end ; not decremented?
|
---|
68 | jl @decr
|
---|
69 | inc eax ; ?
|
---|
70 | jmp @end
|
---|
71 | @decr: dec eax ; ?
|
---|
72 | @end: ret 4
|
---|
73 | _InterlockedDecrement@4 endp
|
---|
74 |
|
---|
75 |
|
---|
76 | public _InterlockedExchange@8
|
---|
77 | ; * InterlockedExchange [KERNEL32.???]
|
---|
78 | ; *
|
---|
79 | ; * Atomically exchanges a pair of values.
|
---|
80 | ; *
|
---|
81 | ; * RETURNS
|
---|
82 | ; * Prior value of value pointed to by Target
|
---|
83 |
|
---|
84 | _InterlockedExchange@8 proc near
|
---|
85 | push edx
|
---|
86 | mov eax, [esp+12] ; LONG value
|
---|
87 | mov edx,[esp+8] ; LPLONG target
|
---|
88 | lock xchg eax, dword ptr [edx]
|
---|
89 | pop edx
|
---|
90 | ret 8
|
---|
91 | _InterlockedExchange@8 endp
|
---|
92 |
|
---|
93 |
|
---|
94 |
|
---|
95 | public _InterlockedCompareExchange@12
|
---|
96 | ;/************************************************************************
|
---|
97 | ; * InterlockedCompareExchange [KERNEL32.879]
|
---|
98 | ; *
|
---|
99 | ; * Atomically compares Destination and Comperand, and if found equal exchanges
|
---|
100 | ; * the value of Destination with Exchange
|
---|
101 | ; *
|
---|
102 | ; */
|
---|
103 | _InterlockedCompareExchange@12 proc near
|
---|
104 | push ebp
|
---|
105 | mov ebp, esp
|
---|
106 | push edx
|
---|
107 | push ebx
|
---|
108 |
|
---|
109 | mov ebx, dword ptr [ebp+8] ;PVOID *Destination, /* Address of 32-bit value to exchange */
|
---|
110 | mov eax, [ebp+16] ;PVOID Comperand /* value to compare, 32 bits */
|
---|
111 | mov edx, [ebp+12] ;PVOID Exchange, /* change value, 32 bits */
|
---|
112 | lock cmpxchg dword ptr [ebx],edx
|
---|
113 |
|
---|
114 | pop ebx
|
---|
115 | pop edx
|
---|
116 | pop ebp
|
---|
117 | ret 12
|
---|
118 | _InterlockedCompareExchange@12 endp
|
---|
119 |
|
---|
120 | public _InterlockedExchangeAdd@8
|
---|
121 | ; * InterlockedExchangeAdd [KERNEL32.880]
|
---|
122 | ; *
|
---|
123 | ; * Atomically adds Increment to Addend and returns the previous value of
|
---|
124 | ; * Addend
|
---|
125 | ; *
|
---|
126 | ; * RETURNS
|
---|
127 | ; * Prior value of value pointed to by cwAddendTarget
|
---|
128 | ; */
|
---|
129 | _InterlockedExchangeAdd@8 proc near
|
---|
130 | push edx
|
---|
131 | mov eax, dword ptr [esp+12] ;LONG Increment /* Value to add */
|
---|
132 | mov edx, dword ptr [esp+8] ;PLONG Addend, /* Address of 32-bit value to exchange */
|
---|
133 | lock xadd dword ptr [edx], eax
|
---|
134 | pop edx
|
---|
135 | ret 8
|
---|
136 | _InterlockedExchangeAdd@8 endp
|
---|
137 |
|
---|
138 | CODE32 ENDS
|
---|
139 |
|
---|
140 | END
|
---|