2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/include/spinlock.h
5 * PURPOSE: Internal Inlined Functions for spinlocks, shared with HAL
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
11 Kii386SpinOnSpinLock(PKSPIN_LOCK SpinLock
, ULONG Flags
);
16 // Spinlock Acquire at IRQL >= DISPATCH_LEVEL
20 KxAcquireSpinLock(IN PKSPIN_LOCK SpinLock
)
22 /* On UP builds, spinlocks don't exist at IRQL >= DISPATCH */
23 UNREFERENCED_PARAMETER(SpinLock
);
27 // Spinlock Release at IRQL >= DISPATCH_LEVEL
31 KxReleaseSpinLock(IN PKSPIN_LOCK SpinLock
)
33 /* On UP builds, spinlocks don't exist at IRQL >= DISPATCH */
34 UNREFERENCED_PARAMETER(SpinLock
);
40 // Spinlock Acquisition at IRQL >= DISPATCH_LEVEL
44 KxAcquireSpinLock(IN PKSPIN_LOCK SpinLock
)
47 /* Make sure that we don't own the lock already */
48 if (((KSPIN_LOCK
)KeGetCurrentThread() | 1) == *SpinLock
)
50 /* We do, bugcheck! */
51 KeBugCheckEx(SPIN_LOCK_ALREADY_OWNED
, (ULONG_PTR
)SpinLock
, 0, 0, 0);
55 /* Try to acquire the lock */
56 while (InterlockedBitTestAndSet((PLONG
)SpinLock
, 0))
58 #if defined(_M_IX86) && defined(DBG)
59 /* On x86 debug builds, we use a much slower but useful routine */
60 Kii386SpinOnSpinLock(SpinLock
, 5);
62 /* It's locked... spin until it's unlocked */
63 while (*(volatile KSPIN_LOCK
*)SpinLock
& 1)
65 /* Yield and keep looping */
71 /* On debug builds, we OR in the KTHREAD */
72 *SpinLock
= (KSPIN_LOCK
)KeGetCurrentThread() | 1;
77 // Spinlock Release at IRQL >= DISPATCH_LEVEL
81 KxReleaseSpinLock(IN PKSPIN_LOCK SpinLock
)
84 /* Make sure that the threads match */
85 if (((KSPIN_LOCK
)KeGetCurrentThread() | 1) != *SpinLock
)
87 /* They don't, bugcheck */
88 KeBugCheckEx(SPIN_LOCK_NOT_OWNED
, (ULONG_PTR
)SpinLock
, 0, 0, 0);
92 InterlockedAnd((PLONG
)SpinLock
, 0);