Use correct format for arguments in debug messages
[reactos.git] / reactos / hal / halx86 / generic / fmutex.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS HAL
4 * FILE: ntoskrnl/hal/x86/fmutex.c
5 * PURPOSE: Deprecated HAL Fast Mutex
6 * PROGRAMMERS: Alex Ionescu (alex@relsoft.net)
7 */
8
9 /*
10 * NOTE: Even HAL itself has #defines to use the Exi* APIs inside NTOSKRNL.
11 * These are only exported here for compatibility with really old
12 * drivers. Also note that in theory, these can be made much faster
13 * by using assembly and inlining all the operations, including
14 * raising and lowering irql.
15 */
16
17 /* INCLUDES *****************************************************************/
18
19 #include <hal.h>
20 #define NDEBUG
21 #include <debug.h>
22
23 #undef ExAcquireFastMutex
24 #undef ExReleaseFastMutex
25 #undef ExTryToAcquireFastMutex
26
27 /* FUNCTIONS *****************************************************************/
28
29 VOID
30 FASTCALL
31 ExAcquireFastMutex(PFAST_MUTEX FastMutex)
32 {
33 KIRQL OldIrql;
34
35 /* Raise IRQL to APC */
36 OldIrql = KfRaiseIrql(APC_LEVEL);
37
38 /* Decrease the count */
39 if (InterlockedDecrement(&FastMutex->Count))
40 {
41 /* Someone is still holding it, use slow path */
42 FastMutex->Contention++;
43 KeWaitForSingleObject(&FastMutex->Gate,
44 WrExecutive,
45 KernelMode,
46 FALSE,
47 NULL);
48 }
49
50 /* Set the owner and IRQL */
51 FastMutex->Owner = KeGetCurrentThread();
52 FastMutex->OldIrql = OldIrql;
53 }
54
55 VOID
56 FASTCALL
57 ExReleaseFastMutex(PFAST_MUTEX FastMutex)
58 {
59 KIRQL OldIrql;
60
61 /* Erase the owner */
62 FastMutex->Owner = (PVOID)1;
63 OldIrql = FastMutex->OldIrql;
64
65 /* Increase the count */
66 if (InterlockedIncrement(&FastMutex->Count) <= 0)
67 {
68 /* Someone was waiting for it, signal the waiter */
69 KeSetEventBoostPriority(&FastMutex->Gate, IO_NO_INCREMENT);
70 }
71
72 /* Lower IRQL back */
73 KfLowerIrql(OldIrql);
74 }
75
76 BOOLEAN
77 FASTCALL
78 ExTryToAcquireFastMutex(PFAST_MUTEX FastMutex)
79 {
80 KIRQL OldIrql;
81
82 /* Raise to APC_LEVEL */
83 OldIrql = KfRaiseIrql(APC_LEVEL);
84
85 /* Check if we can quickly acquire it */
86 if (InterlockedCompareExchange(&FastMutex->Count, 0, 1) == 1)
87 {
88 /* We have, set us as owners */
89 FastMutex->Owner = KeGetCurrentThread();
90 FastMutex->OldIrql = OldIrql;
91 return TRUE;
92 }
93 else
94 {
95 /* Acquire attempt failed */
96 KfLowerIrql(OldIrql);
97 return FALSE;
98 }
99 }
100
101 /* EOF */