ReactOS  r76032
spinlock.h
Go to the documentation of this file.
1 /*
2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/include/internal/spinlock.h
5 * PURPOSE: Internal Inlined Functions for spinlocks, shared with HAL
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
7 */
8 
9 VOID
10 NTAPI
12 
13 #ifndef CONFIG_SMP
14 
15 //
16 // Spinlock Acquire at IRQL >= DISPATCH_LEVEL
17 //
19 VOID
21 {
22  /* On UP builds, spinlocks don't exist at IRQL >= DISPATCH */
23  UNREFERENCED_PARAMETER(SpinLock);
24 
25  /* Add an explicit memory barrier to prevent the compiler from reordering
26  memory accesses across the borders of spinlocks */
28 }
29 
30 //
31 // Spinlock Release at IRQL >= DISPATCH_LEVEL
32 //
34 VOID
36 {
37  /* On UP builds, spinlocks don't exist at IRQL >= DISPATCH */
38  UNREFERENCED_PARAMETER(SpinLock);
39 
40  /* Add an explicit memory barrier to prevent the compiler from reordering
41  memory accesses across the borders of spinlocks */
43 }
44 
45 #else
46 
47 //
48 // Spinlock Acquisition at IRQL >= DISPATCH_LEVEL
49 //
51 VOID
53 {
54 #if DBG
55  /* Make sure that we don't own the lock already */
56  if (((KSPIN_LOCK)KeGetCurrentThread() | 1) == *SpinLock)
57  {
58  /* We do, bugcheck! */
59  KeBugCheckEx(SPIN_LOCK_ALREADY_OWNED, (ULONG_PTR)SpinLock, 0, 0, 0);
60  }
61 #endif
62 
63  /* Try to acquire the lock */
64  while (InterlockedBitTestAndSet((PLONG)SpinLock, 0))
65  {
66 #if defined(_M_IX86) && DBG
67  /* On x86 debug builds, we use a much slower but useful routine */
68  Kii386SpinOnSpinLock(SpinLock, 5);
69 #else
70  /* It's locked... spin until it's unlocked */
71  while (*(volatile KSPIN_LOCK *)SpinLock & 1)
72  {
73  /* Yield and keep looping */
75  }
76 #endif
77  }
78 #if DBG
79  /* On debug builds, we OR in the KTHREAD */
80  *SpinLock = (KSPIN_LOCK)KeGetCurrentThread() | 1;
81 #endif
82 }
83 
84 //
85 // Spinlock Release at IRQL >= DISPATCH_LEVEL
86 //
88 VOID
90 {
91 #if DBG
92  /* Make sure that the threads match */
93  if (((KSPIN_LOCK)KeGetCurrentThread() | 1) != *SpinLock)
94  {
95  /* They don't, bugcheck */
96  KeBugCheckEx(SPIN_LOCK_NOT_OWNED, (ULONG_PTR)SpinLock, 0, 0, 0);
97  }
98 #endif
99  /* Clear the lock */
100  InterlockedAnd((PLONG)SpinLock, 0);
101 }
102 
103 #endif
#define IN
Definition: typedefs.h:38
#define InterlockedAnd
Definition: interlocked.h:62
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:315
FORCEINLINE VOID KxReleaseSpinLock(IN PKSPIN_LOCK SpinLock)
Definition: spinlock.h:35
KSPIN_LOCK * PKSPIN_LOCK
Definition: env_spec_w32.h:73
_In_ PKSERVICE_ROUTINE _In_opt_ PVOID _In_opt_ PKSPIN_LOCK SpinLock
Definition: iofuncs.h:798
uint32_t ULONG_PTR
Definition: typedefs.h:63
FORCEINLINE VOID YieldProcessor(VOID)
Definition: ke.h:32
_Must_inspect_result_ _In_ ULONG Flags
Definition: wsk.h:170
NTSTATUS(* NTAPI)(IN PFILE_FULL_EA_INFORMATION EaBuffer, IN ULONG EaLength, OUT PULONG ErrorOffset)
Definition: IoEaTest.cpp:117
#define KeMemoryBarrierWithoutFence()
Definition: ke.h:66
#define FORCEINLINE
Definition: ntbasedef.h:213
FORCEINLINE VOID KxAcquireSpinLock(IN PKSPIN_LOCK SpinLock)
Definition: spinlock.h:20
#define InterlockedBitTestAndSet
Definition: interlocked.h:30
VOID NTAPI Kii386SpinOnSpinLock(PKSPIN_LOCK SpinLock, ULONG Flags)
ULONG KSPIN_LOCK
Definition: env_spec_w32.h:72
unsigned int ULONG
Definition: retypes.h:1
#define KeGetCurrentThread
Definition: hal.h:44
signed int * PLONG
Definition: retypes.h:5
VOID NTAPI KeBugCheckEx(_In_ ULONG BugCheckCode, _In_ ULONG_PTR BugCheckParameter1, _In_ ULONG_PTR BugCheckParameter2, _In_ ULONG_PTR BugCheckParameter3, _In_ ULONG_PTR BugCheckParameter4)
Definition: rtlcompat.c:90