3704c365efce4d395ccd1e11bb23e26fbff9880a
[reactos.git] / reactos / ntoskrnl / ex / i386 / interlck.c
1 /* $Id$
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/ex/i386/interlck.c
6 * PURPOSE: No purpose listed.
7 *
8 * PROGRAMMERS: No programmer listed.
9 */
10
11 #include <ntoskrnl.h>
12
13 #ifdef LOCK
14 #undef LOCK
15 #endif
16
17 #if defined(__GNUC__)
18
19 #ifdef CONFIG_SMP
20 #define LOCK "lock ; "
21 #else
22 #define LOCK ""
23 #endif
24
25 #elif defined(_MSC_VER)
26
27 #ifdef CONFIG_SMP
28 #define LOCK lock
29 #else
30 #define LOCK
31 #endif
32
33 #endif
34
35 #if defined(__GNUC__)
36
37 /*
38 * @implemented
39 */
40 INTERLOCKED_RESULT FASTCALL
41 Exfi386InterlockedIncrementLong(IN PLONG Addend);
42
43 __asm__("\n\t.global @Exfi386InterlockedIncrementLong@4\n\t"
44 "@Exfi386InterlockedIncrementLong@4:\n\t"
45 LOCK
46 "addl $1,(%ecx)\n\t"
47 "lahf\n\t"
48 "andl $0xC000, %eax\n\t"
49 "ret\n\t");
50
51 #elif defined(_MSC_VER)
52
53 /*
54 * @implemented
55 */
56 __declspec(naked)
57 INTERLOCKED_RESULT FASTCALL
58 Exfi386InterlockedIncrementLong(IN PLONG Addend)
59 {
60 __asm LOCK add dword ptr [ecx], 1
61 __asm lahf
62 __asm and eax, 0xC000
63 __asm ret
64 }
65
66 #else
67 #error Unknown compiler for inline assembler
68 #endif
69
70
71 #if defined(__GNUC__)
72
73 /*
74 * @implemented
75 */
76 INTERLOCKED_RESULT FASTCALL
77 Exfi386InterlockedDecrementLong(IN PLONG Addend);
78
79 __asm__("\n\t.global @Exfi386InterlockedDecrementLong@4\n\t"
80 "@Exfi386InterlockedDecrementLong@4:\n\t"
81 LOCK
82 "subl $1,(%ecx)\n\t"
83 "lahf\n\t"
84 "andl $0xC000, %eax\n\t"
85 "ret\n\t");
86
87 #elif defined(_MSC_VER)
88
89 /*
90 * @implemented
91 */
92 __declspec(naked)
93 INTERLOCKED_RESULT FASTCALL
94 Exfi386InterlockedDecrementLong(IN PLONG Addend)
95 {
96 __asm LOCK sub dword ptr [ecx], 1
97 __asm lahf
98 __asm and eax, 0xC000
99 __asm ret
100 }
101
102 #else
103 #error Unknown compiler for inline assembler
104 #endif
105
106
107 #if defined(__GNUC__)
108
109 /*
110 * @implemented
111 */
112 ULONG FASTCALL
113 Exfi386InterlockedExchangeUlong(IN PULONG Target,
114 IN ULONG Value);
115
116 __asm__("\n\t.global @Exfi386InterlockedExchangeUlong@8\n\t"
117 "@Exfi386InterlockedExchangeUlong@8:\n\t"
118 LOCK
119 "xchgl %edx,(%ecx)\n\t"
120 "movl %edx,%eax\n\t"
121 "ret\n\t");
122
123 #elif defined(_MSC_VER)
124
125 /*
126 * @implemented
127 */
128 __declspec(naked)
129 ULONG FASTCALL
130 Exfi386InterlockedExchangeUlong(IN PULONG Target,
131 IN ULONG Value)
132 {
133 __asm LOCK xchg [ecx], edx
134 __asm mov eax, edx
135 __asm ret
136 }
137
138 #else
139 #error Unknown compiler for inline assembler
140 #endif
141
142
143 #if defined(__GNUC__)
144
145 INTERLOCKED_RESULT STDCALL
146 Exi386InterlockedIncrementLong(IN PLONG Addend);
147
148 __asm__("\n\t.global _Exi386InterlockedIncrementLong@4\n\t"
149 "_Exi386InterlockedIncrementLong@4:\n\t"
150 "movl 4(%esp),%eax\n\t"
151 LOCK
152 "addl $1,(%eax)\n\t"
153 "lahf\n\t"
154 "andl $0xC000, %eax\n\t"
155 "ret $4\n\t");
156
157 #elif defined(_MSC_VER)
158
159 __declspec(naked)
160 INTERLOCKED_RESULT STDCALL
161 Exi386InterlockedIncrementLong(IN PLONG Addend)
162 {
163 __asm mov eax, Addend
164 __asm LOCK add dword ptr [eax], 1
165 __asm lahf
166 __asm and eax, 0xC000
167 __asm ret 4
168 }
169
170 #else
171 #error Unknown compiler for inline assembler
172 #endif
173
174
175 #if defined(__GNUC__)
176
177 INTERLOCKED_RESULT STDCALL
178 Exi386InterlockedDecrementLong(IN PLONG Addend);
179
180 __asm__("\n\t.global _Exi386InterlockedDecrementLong@4\n\t"
181 "_Exi386InterlockedDecrementLong@4:\n\t"
182 "movl 4(%esp),%eax\n\t"
183 LOCK
184 "subl $1,(%eax)\n\t"
185 "lahf\n\t"
186 "andl $0xC000, %eax\n\t"
187 "ret $4\n\t");
188
189 #elif defined(_MSC_VER)
190
191 __declspec(naked)
192 INTERLOCKED_RESULT STDCALL
193 Exi386InterlockedDecrementLong(IN PLONG Addend)
194 {
195 __asm mov eax, Addend
196 __asm LOCK sub dword ptr [eax], 1
197 __asm lahf
198 __asm and eax, 0xC000
199 __asm ret 4
200 }
201
202 #else
203 #error Unknown compiler for inline assembler
204 #endif
205
206
207 #if defined(__GNUC__)
208
209 ULONG STDCALL
210 Exi386InterlockedExchangeUlong(IN PULONG Target,
211 IN ULONG Value);
212
213 __asm__("\n\t.global _Exi386InterlockedExchangeUlong@8\n\t"
214 "_Exi386InterlockedExchangeUlong@8:\n\t"
215 "movl 4(%esp),%edx\n\t"
216 "movl 8(%esp),%eax\n\t"
217 LOCK
218 "xchgl %eax,(%edx)\n\t"
219 "ret $8\n\t");
220
221 #elif defined(_MSC_VER)
222
223 __declspec(naked)
224 ULONG STDCALL
225 Exi386InterlockedExchangeUlong(IN PULONG Target,
226 IN ULONG Value)
227 {
228 __asm mov edx, Value
229 __asm mov eax, Target
230 __asm LOCK xchg [edx], eax
231 __asm ret 8
232 }
233
234 #else
235 #error Unknown compiler for inline assembler
236 #endif
237
238
239
240 /**********************************************************************
241 * FASTCALL: @InterlockedIncrement@4
242 * STDCALL : _InterlockedIncrement@4
243 */
244 #if defined(__GNUC__)
245 /*
246 * @implemented
247 */
248 LONG FASTCALL
249 InterlockedIncrement(PLONG Addend);
250 /*
251 * FUNCTION: Increments a caller supplied variable of type LONG as an
252 * atomic operation
253 * ARGUMENTS:
254 * Addend = Points to a variable whose value is to be increment
255 * RETURNS: The incremented value
256 */
257
258 __asm__("\n\t.global @InterlockedIncrement@4\n\t"
259 "@InterlockedIncrement@4:\n\t"
260 "movl $1,%eax\n\t"
261 LOCK
262 "xaddl %eax,(%ecx)\n\t"
263 "incl %eax\n\t"
264 "ret\n\t");
265
266 #elif defined(_MSC_VER)
267 /*
268 * @implemented
269 */
270 __declspec(naked)
271 LONG FASTCALL
272 InterlockedIncrement(PLONG Addend)
273 {
274 __asm mov eax, 1
275 __asm LOCK xadd [ecx], eax
276 __asm inc eax
277 __asm ret
278 }
279
280 #else
281 #error Unknown compiler for inline assembler
282 #endif
283
284
285 /**********************************************************************
286 * FASTCALL: @InterlockedDecrement@4
287 * STDCALL : _InterlockedDecrement@4
288 */
289 #if defined(__GNUC__)
290 /*
291 * @implemented
292 */
293 LONG FASTCALL
294 InterlockedDecrement(PLONG Addend);
295
296 __asm__("\n\t.global @InterlockedDecrement@4\n\t"
297 "@InterlockedDecrement@4:\n\t"
298 "movl $-1,%eax\n\t"
299 LOCK
300 "xaddl %eax,(%ecx)\n\t"
301 "decl %eax\n\t"
302 "ret\n\t");
303
304 #elif defined(_MSC_VER)
305
306 /*
307 * @implemented
308 */
309 __declspec(naked)
310 LONG FASTCALL
311 InterlockedDecrement(PLONG Addend)
312 {
313 __asm mov eax, -1
314 __asm LOCK xadd [ecx], eax
315 __asm dec eax
316 __asm ret
317 }
318
319 #else
320 #error Unknown compiler for inline assembler
321 #endif
322
323
324 /**********************************************************************
325 * FASTCALL: @InterlockedExchange@8
326 * STDCALL : _InterlockedExchange@8
327 */
328
329 #if defined(__GNUC__)
330 /*
331 * @implemented
332 */
333 LONG FASTCALL
334 InterlockedExchange(PLONG Target,
335 LONG Value);
336
337 __asm__("\n\t.global @InterlockedExchange@8\n\t"
338 "@InterlockedExchange@8:\n\t"
339 LOCK
340 "xchgl %edx,(%ecx)\n\t"
341 "movl %edx,%eax\n\t"
342 "ret\n\t");
343
344 #elif defined(_MSC_VER)
345 /*
346 * @implemented
347 */
348 __declspec(naked)
349 LONG FASTCALL
350 InterlockedExchange(PLONG Target,
351 LONG Value)
352 {
353 __asm LOCK xchg [ecx], edx
354 __asm mov eax, edx
355 __asm ret
356 }
357
358 #else
359 #error Unknown compiler for inline assembler
360 #endif
361
362 /**********************************************************************
363 * FASTCALL: @InterlockedExchangeAdd@8
364 * STDCALL: _InterlockedExchangeAdd@8
365 */
366 #if defined(__GNUC__)
367 /*
368 * @implemented
369 */
370 LONG FASTCALL
371 InterlockedExchangeAdd(PLONG Addend,
372 LONG Value);
373
374 __asm__("\n\t.global @InterlockedExchangeAdd@8\n\t"
375 "@InterlockedExchangeAdd@8:\n\t"
376 LOCK
377 "xaddl %edx,(%ecx)\n\t"
378 "movl %edx,%eax\n\t"
379 "ret\n\t");
380
381 #elif defined(_MSC_VER)
382 /*
383 * @implemented
384 */
385 __declspec(naked)
386 LONG FASTCALL
387 InterlockedExchangeAdd(PLONG Addend,
388 LONG Value)
389 {
390 __asm LOCK xadd [ecx], edx
391 __asm mov eax, edx
392 __asm ret
393 }
394
395 #else
396 #error Unknown compiler for inline assembler
397 #endif
398
399 /**********************************************************************
400 * FASTCALL: @InterlockedClearBit@8
401 * STDCALL: _InterlockedClearBit@8
402 */
403 #if defined(__GNUC__)
404 /*
405 * @implemented
406 */
407 UCHAR
408 FASTCALL
409 InterlockedClearBit(PLONG Destination,
410 LONG Bit);
411
412 __asm__("\n\t.global @InterlockedClearBit@8\n\t"
413 "@InterlockedClearBit@8:\n\t"
414 LOCK
415 "btr %edx,(%ecx)\n\t"
416 "setc %al\n\t"
417 "ret\n\t");
418
419 #elif defined(_MSC_VER)
420 /*
421 * @implemented
422 */
423 __declspec(naked)
424 UCHAR
425 FASTCALL
426 InterlockedClearBit(PUCHAR Destination,
427 UCHAR Bit)
428 {
429 __asm LOCK btr [ecx], edx
430 __asm setc al
431 __asm ret
432 }
433
434 #else
435 #error Unknown compiler for inline assembler
436 #endif
437
438 /**********************************************************************
439 * FASTCALL: @InterlockedCompareExchange@12
440 * STDCALL: _InterlockedCompareExchange@12
441 */
442 #if defined(__GNUC__)
443
444 LONG FASTCALL
445 InterlockedCompareExchange(PLONG Destination,
446 LONG Exchange,
447 LONG Comperand);
448
449 __asm__("\n\t.global @InterlockedCompareExchange@12\n\t"
450 "@InterlockedCompareExchange@12:\n\t"
451 "movl 4(%esp),%eax\n\t"
452 LOCK
453 "cmpxchg %edx,(%ecx)\n\t"
454 "ret $4\n\t");
455
456 #elif defined(_MSC_VER)
457
458 __declspec(naked)
459 LONG FASTCALL
460 InterlockedCompareExchange(PLONG Destination,
461 LONG Exchange,
462 LONG Comperand)
463 {
464 __asm mov eax, Comperand
465 __asm LOCK cmpxchg [ecx], edx
466 __asm ret 4
467 }
468
469 #else
470 #error Unknown compiler for inline assembler
471 #endif
472
473 /**********************************************************************
474 * FASTCALL: @InterlockedCompareExchange64@8
475 */
476 #if defined(__GNUC__)
477 LONGLONG FASTCALL
478 ExfpInterlockedExchange64(LONGLONG volatile * Destination,
479 PLONGLONG Exchange);
480
481 __asm__("\n\t.global @ExfpInterlockedExchange64@8\n\t"
482 "@ExfpInterlockedExchange64@8:\n\t"
483 "pushl %ebx\n\t"
484 "pushl %esi\n\t"
485 "movl %ecx,%esi\n\t"
486 "movl (%edx),%ebx\n\t"
487 "movl 4(%edx),%ecx\n\t"
488 "\n1:\t"
489 "movl (%esi),%eax\n\t"
490 "movl 4(%esi),%edx\n\t"
491 LOCK
492 "cmpxchg8b (%esi)\n\t"
493 "jnz 1b\n\t"
494 "popl %esi\n\t"
495 "popl %ebx\n\t"
496 "ret\n\t");
497
498 #else
499 #error Unknown compiler for inline assembler
500 #endif
501
502 /**********************************************************************
503 * FASTCALL: @ExfInterlockedCompareExchange@12
504 */
505 #if defined(__GNUC__)
506 LONGLONG FASTCALL
507 ExfInterlockedCompareExchange64(LONGLONG volatile * Destination,
508 PLONGLONG Exchange,
509 PLONGLONG Comperand);
510
511 __asm__("\n\t.global @ExfInterlockedCompareExchange64@12\n\t"
512 "@ExfInterlockedCompareExchange64@12:\n\t"
513 "pushl %ebx\n\t"
514 "pushl %esi\n\t"
515 "movl %ecx,%esi\n\t"
516 "movl (%edx),%ebx\n\t"
517 "movl 4(%edx),%ecx\n\t"
518 "movl 12(%esp),%edx\n\t"
519 "movl (%edx),%eax\n\t"
520 "movl 4(%edx),%edx\n\t"
521 LOCK
522 "cmpxchg8b (%esi)\n\t"
523 "popl %esi\n\t"
524 "popl %ebx\n\t"
525 "ret $4\n\t");
526
527 #else
528 #error Unknown compiler for inline assembler
529 #endif
530
531 /* EOF */