ReactOS  0.4.15-dev-439-g292f67a
fmutex.c
Go to the documentation of this file.
1 /*
2  * COPYRIGHT: See COPYING in the top level directory
3  * PROJECT: ReactOS HAL
4  * FILE: hal/halppc/generic/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 
26 /* FUNCTIONS *****************************************************************/
27 
28 VOID
31 {
32  KIRQL OldIrql;
33 
34  /* Raise IRQL to APC */
36 
37  /* Decrease the count */
38  if (InterlockedDecrement(&FastMutex->Count))
39  {
40  /* Someone is still holding it, use slow path */
41  FastMutex->Contention++;
42  KeWaitForSingleObject(&FastMutex->Event,
44  KernelMode,
45  FALSE,
46  NULL);
47  }
48 
49  /* Set the owner and IRQL */
50  FastMutex->Owner = KeGetCurrentThread();
51  FastMutex->OldIrql = OldIrql;
52 }
53 
54 VOID
57 {
58  KIRQL OldIrql;
59 
60  /* Erase the owner */
61  FastMutex->Owner = (PVOID)1;
62  OldIrql = FastMutex->OldIrql;
63 
64  /* Increase the count */
65  if (InterlockedIncrement(&FastMutex->Count) <= 0)
66  {
67  /* Someone was waiting for it, signal the waiter */
68  KeSetEventBoostPriority(&FastMutex->Event, IO_NO_INCREMENT);
69  }
70 
71  /* Lower IRQL back */
73 }
74 
75 BOOLEAN
78 {
79  KIRQL OldIrql;
80 
81  /* Raise to APC_LEVEL */
83 
84  /* Check if we can quickly acquire it */
85  if (InterlockedCompareExchange(&FastMutex->Count, 0, 1) == 1)
86  {
87  /* We have, set us as owners */
88  FastMutex->Owner = KeGetCurrentThread();
89  FastMutex->OldIrql = OldIrql;
90  return TRUE;
91  }
92  else
93  {
94  /* Acquire attempt failed */
96  return FALSE;
97  }
98 }
99 
100 /* EOF */
#define KeRaiseIrql(irql, oldIrql)
Definition: env_spec_w32.h:597
#define TRUE
Definition: types.h:120
#define KeLowerIrql(oldIrql)
Definition: env_spec_w32.h:602
BOOLEAN FASTCALL ExiTryToAcquireFastMutex(PFAST_MUTEX FastMutex)
Definition: fmutex.c:77
#define InterlockedCompareExchange
Definition: interlocked.h:104
NTSTATUS NTAPI KeWaitForSingleObject(IN PVOID Object, IN KWAIT_REASON WaitReason, IN KPROCESSOR_MODE WaitMode, IN BOOLEAN Alertable, IN PLARGE_INTEGER Timeout OPTIONAL)
Definition: wait.c:416
#define FASTCALL
Definition: nt_native.h:50
VOID FASTCALL ExReleaseFastMutex(IN PFAST_MUTEX FastMutex)
Definition: fmutex.c:31
UCHAR KIRQL
Definition: env_spec_w32.h:591
unsigned char BOOLEAN
smooth NULL
Definition: ftsmooth.c:416
void * PVOID
Definition: retypes.h:9
#define InterlockedDecrement
Definition: armddk.h:52
_Requires_lock_held_ Interrupt _Releases_lock_ Interrupt _In_ _IRQL_restores_ KIRQL OldIrql
Definition: kefuncs.h:790
* PFAST_MUTEX
Definition: extypes.h:17
#define InterlockedIncrement
Definition: armddk.h:53
VOID FASTCALL ExAcquireFastMutex(IN PFAST_MUTEX FastMutex)
Definition: fmutex.c:23
#define IO_NO_INCREMENT
Definition: iotypes.h:566
VOID NTAPI KeSetEventBoostPriority(IN PKEVENT Event, IN PKTHREAD *WaitingThread OPTIONAL)
Definition: eventobj.c:229
#define KeGetCurrentThread
Definition: hal.h:44
#define APC_LEVEL
Definition: env_spec_w32.h:695