| 1 |
#if defined(_WIN32) |
| 2 |
|
| 3 |
LONG cmpxchg(LONG *ptr, LONG old, LONG new) { |
| 4 |
volatile LONG *_ptr = (volatile LONG *)(ptr); |
| 5 |
return _InterlockedCompareExchange(_ptr, new, old); |
| 6 |
} |
| 7 |
|
| 8 |
LONG xchg(LONG *ptr, LONG new) { |
| 9 |
volatile LONG *_ptr = (volatile LONG *)(ptr); |
| 10 |
return _InterlockedExchange(_ptr, new); |
| 11 |
} |
| 12 |
|
| 13 |
/* _InterlockedDecrement() returns *ptr AFTER the decrement. * |
| 14 |
* we need value BEFORE decrement. * |
| 15 |
* |
| 16 |
LONG xdec(LONG *ptr) { |
| 17 |
return _InterlockedDecrement(ptr); |
| 18 |
} |
| 19 |
*/ |
| 20 |
LONG xdec(LONG *ptr) { |
| 21 |
LONG r, old; |
| 22 |
do { |
| 23 |
old = *ptr; |
| 24 |
r = cmpxchg(ptr, old, old-1); |
| 25 |
} while (r != old); |
| 26 |
return r; |
| 27 |
} |
| 28 |
|
| 29 |
/*****************************************************************************/ |
| 30 |
#else /* ! _WIN32 */ |
| 31 |
/*****************************************************************************/ |
| 32 |
|
| 33 |
|
| 34 |
#endif /* _WIN32 */ |
| 35 |
/* ex:set ts=2 sw=2 itab=spaces: */ |