ReactOS  0.4.14-dev-358-gbef841c
control.c
Go to the documentation of this file.
1 /*
2  * COPYRIGHT: See COPYING in the top level directory
3  * PROJECT: ReactOS NDIS library
4  * FILE: ndis/control.c
5  * PURPOSE: Program control routines
6  * PROGRAMMERS: Casper S. Hornstrup (chorns@users.sourceforge.net)
7  * Vizzini (vizzini@plasmic.com)
8  * REVISIONS:
9  * CSH 01/08-2000 Created
10  * 3 Oct 2003 Vizzini - Formatting and minor bugfixes
11  */
12 
13 #include "ndissys.h"
14 
15 /*
16  * @implemented
17  */
18 VOID
19 EXPORT
22 /*
23  * FUNCTION: Initialize a NDIS_RW_LOCK
24  * ARGUMENTS:
25  * Lock: pointer to the lock to initialize
26  * NOTES:
27  * NDIS 5.0
28  */
29 {
31 
33 }
34 
35 /*
36  * @implemented
37  */
38 VOID
39 EXPORT
44 /*
45  * FUNCTION:
46  * ARGUMENTS:
47  * NOTES:
48  * NDIS 5.0
49  */
50 {
51  ULONG RefCount;
52  UCHAR ProcessorNumber;
53  volatile UCHAR BusyLoop;
54 
56 
57  if (fWrite) {
58  if (Lock->Context == PsGetCurrentThread()) {
59  LockState->LockState = 2;
60  } else {
62  /* Check if any other processor helds a shared lock. */
63  for (ProcessorNumber = KeNumberProcessors; ProcessorNumber--; ) {
64  if (ProcessorNumber != KeGetCurrentProcessorNumber()) {
65  /* Wait till the shared lock is released. */
66  while (Lock->RefCount[ProcessorNumber].RefCount != 0) {
67  for (BusyLoop = 32; BusyLoop--; )
68  ;
69  }
70  }
71  }
73  LockState->LockState = 4;
74  }
75  } else {
78  /* Racing with a exclusive write lock case. */
79  if (Lock->SpinLock != 0) {
80  if (RefCount == 1) {
81  if (Lock->Context != PsGetCurrentThread()) {
82  /* Wait for the exclusive lock to be released. */
87  }
88  }
89  }
91  LockState->LockState = 3;
92  }
93 }
94 
95 /*
96  * @implemented
97  */
98 VOID
99 EXPORT
103 /*
104  * FUNCTION:
105  * ARGUMENTS:
106  * NOTES:
107  * NDIS 5.0
108  */
109 {
110  switch (LockState->LockState) {
111  case 2: /* Exclusive write lock, recursive */
112  return;
113 
114  case 3: /* Shared read lock */
116  LockState->LockState = -1;
117  if (LockState->OldIrql < DISPATCH_LEVEL)
118  KeLowerIrql(LockState->OldIrql);
119  return;
120 
121  case 4: /* Exclusive write lock */
122  Lock->Context = NULL;
123  LockState->LockState = -1;
125  return;
126  }
127 }
128 
129 /*
130  * @implemented
131  */
132 #undef NdisAcquireSpinLock
133 VOID
134 EXPORT
137 /*
138  * FUNCTION: Acquires a spin lock for exclusive access to a resource
139  * ARGUMENTS:
140  * SpinLock = Pointer to the initialized NDIS spin lock to be acquired
141  */
142 {
143  KeAcquireSpinLock(&SpinLock->SpinLock, &SpinLock->OldIrql);
144 }
145 
146 /*
147  * @implemented
148  */
149 #undef NdisAllocateSpinLock
150 VOID
151 EXPORT
154 /*
155  * FUNCTION: Initializes for an NDIS spin lock
156  * ARGUMENTS:
157  * SpinLock = Pointer to an NDIS spin lock structure
158  */
159 {
160  KeInitializeSpinLock(&SpinLock->SpinLock);
161 }
162 
163 /*
164  * @implemented
165  */
166 #undef NdisDprAcquireSpinLock
167 VOID
168 EXPORT
171 /*
172  * FUNCTION: Acquires a spin lock from IRQL DISPATCH_LEVEL
173  * ARGUMENTS:
174  * SpinLock = Pointer to the initialized NDIS spin lock to be acquired
175  */
176 {
178  SpinLock->OldIrql = DISPATCH_LEVEL;
179 }
180 
181 /*
182  * @implemented
183  */
184 #undef NdisDprReleaseSpinLock
185 VOID
186 EXPORT
189 /*
190  * FUNCTION: Releases an acquired spin lock from IRQL DISPATCH_LEVEL
191  * ARGUMENTS:
192  * SpinLock = Pointer to the acquired NDIS spin lock to be released
193  */
194 {
196 }
197 
198 /*
199  * @implemented
200  */
201 #undef NdisFreeSpinLock
202 VOID
203 EXPORT
206 /*
207  * FUNCTION: Releases a spin lock initialized with NdisAllocateSpinLock
208  * ARGUMENTS:
209  * SpinLock = Pointer to an initialized NDIS spin lock
210  */
211 {
212  /* Nothing to do here! */
213 }
214 
215 
216 /*
217  * @implemented
218  */
219 VOID
220 EXPORT
223 /*
224  * FUNCTION: Initializes an event to be used for synchronization
225  * ARGUMENTS:
226  * Event = Pointer to an NDIS event structure to be initialized
227  */
228 {
230 }
231 
232 
233 /*
234  * @implemented
235  */
236 #undef NdisReleaseSpinLock
237 VOID
238 EXPORT
241 /*
242  * FUNCTION: Releases a spin lock previously acquired with NdisAcquireSpinLock
243  * ARGUMENTS:
244  * SpinLock = Pointer to the acquired NDIS spin lock to be released
245  */
246 {
247  KeReleaseSpinLock(&SpinLock->SpinLock, SpinLock->OldIrql);
248 }
249 
250 
251 /*
252  * @implemented
253  */
254 VOID
255 EXPORT
258 /*
259  * FUNCTION: Clears the signaled state of an event
260  * ARGUMENTS:
261  * Event = Pointer to the initialized event object to be reset
262  */
263 {
264  KeClearEvent(&Event->Event);
265 }
266 
267 
268 /*
269  * @implemented
270  */
271 VOID
272 EXPORT
275 /*
276  * FUNCTION: Sets an event to a signaled state if not already signaled
277  * ARGUMENTS:
278  * Event = Pointer to the initialized event object to be set
279  */
280 {
282 }
283 
284 
285 /*
286  * @implemented
287  */
288 BOOLEAN
289 EXPORT
292  IN UINT MsToWait)
293 /*
294  * FUNCTION: Waits for an event to become signaled
295  * ARGUMENTS:
296  * Event = Pointer to the initialized event object to wait for
297  * MsToWait = Maximum milliseconds to wait for the event to become signaled
298  * RETURNS:
299  * TRUE if the event is in the signaled state
300  */
301 {
304 
305  Timeout.QuadPart = Int32x32To64(MsToWait, -10000);
306 
308 
309  return (Status == STATUS_SUCCESS);
310 }
311 
312 /* EOF */
PVOID Context
Definition: rwlock.h:17
#define IN
Definition: typedefs.h:38
BOOLEAN EXPORT NdisWaitEvent(IN PNDIS_EVENT Event, IN UINT MsToWait)
Definition: control.c:290
#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
VOID EXPORT NdisAllocateSpinLock(IN PNDIS_SPIN_LOCK SpinLock)
Definition: control.c:152
#define PsGetCurrentThread()
Definition: env_spec_w32.h:81
VOID EXPORT NdisInitializeReadWriteLock(IN PNDIS_RW_LOCK Lock)
Definition: control.c:20
VOID EXPORT NdisDprReleaseSpinLock(IN PNDIS_SPIN_LOCK SpinLock)
Definition: control.c:187
VOID EXPORT NdisReleaseSpinLock(IN PNDIS_SPIN_LOCK SpinLock)
Definition: control.c:239
VOID NTAPI KeAcquireSpinLockAtDpcLevel(IN PKSPIN_LOCK SpinLock)
Definition: spinlock.c:192
LONG NTSTATUS
Definition: precomp.h:26
LONG NTAPI KeSetEvent(IN PKEVENT Event, IN KPRIORITY Increment, IN BOOLEAN Wait)
Definition: eventobj.c:159
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
_In_ PKSERVICE_ROUTINE _In_opt_ PVOID _In_opt_ PKSPIN_LOCK SpinLock
Definition: iofuncs.h:798
FORCEINLINE ULONG KeGetCurrentProcessorNumber(VOID)
Definition: ke.h:325
VOID EXPORT NdisReleaseReadWriteLock(IN PNDIS_RW_LOCK Lock, IN PLOCK_STATE LockState)
Definition: control.c:100
VOID EXPORT NdisDprAcquireSpinLock(IN PNDIS_SPIN_LOCK SpinLock)
Definition: control.c:169
IN OUT PLONG IN OUT PLONG Addend IN OUT PLONG IN LONG IN OUT PLONG IN LONG Increment IN PNDIS_RW_LOCK Lock IN PNDIS_RW_LOCK IN PLOCK_STATE LockState
Definition: CrNtStubs.h:99
_Acquires_exclusive_lock_ Lock _In_ BOOLEAN fWrite
Definition: ndis.h:4586
FORCEINLINE VOID KeInitializeSpinLock(_Out_ PKSPIN_LOCK SpinLock)
Definition: kefuncs.h:251
unsigned char BOOLEAN
smooth NULL
Definition: ftsmooth.c:416
VOID EXPORT NdisFreeSpinLock(IN PNDIS_SPIN_LOCK SpinLock)
Definition: control.c:204
KSPIN_LOCK SpinLock
Definition: rwlock.h:16
VOID EXPORT NdisAcquireSpinLock(IN PNDIS_SPIN_LOCK SpinLock)
Definition: control.c:135
VOID NTAPI KeReleaseSpinLockFromDpcLevel(IN PKSPIN_LOCK SpinLock)
Definition: spinlock.c:215
#define KeAcquireSpinLock(sl, irql)
Definition: env_spec_w32.h:609
unsigned char UCHAR
Definition: xmlstorage.h:181
VOID EXPORT NdisResetEvent(IN PNDIS_EVENT Event)
Definition: control.c:256
IN OUT PLONG IN OUT PLONG Addend IN OUT PLONG IN LONG IN OUT PLONG IN LONG Increment IN PNDIS_RW_LOCK Lock
Definition: CrNtStubs.h:75
CCHAR KeNumberProcessors
Definition: krnlinit.c:35
Status
Definition: gdiplustypes.h:24
#define DISPATCH_LEVEL
Definition: env_spec_w32.h:696
VOID EXPORT NdisSetEvent(IN PNDIS_EVENT Event)
Definition: control.c:273
unsigned int RefCount
Definition: rwlock.h:9
static ULONG Timeout
Definition: ping.c:61
#define KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
#define InterlockedIncrement
Definition: armddk.h:53
NDIS_RW_LOCK_REFCOUNT RefCount[MAXIMUM_PROCESSORS]
Definition: rwlock.h:22
VOID EXPORT NdisAcquireReadWriteLock(IN PNDIS_RW_LOCK Lock, IN BOOLEAN fWrite, IN PLOCK_STATE LockState)
Definition: control.c:40
unsigned int UINT
Definition: ndis.h:50
#define KeReleaseSpinLock(sl, irql)
Definition: env_spec_w32.h:627
unsigned int ULONG
Definition: retypes.h:1
#define IO_NO_INCREMENT
Definition: iotypes.h:566
#define ASSERT_IRQL(x)
Definition: debug.h:42
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:261
_In_ UINT MsToWait
Definition: ndis.h:5881
VOID EXPORT NdisInitializeEvent(IN PNDIS_EVENT Event)
Definition: control.c:221
VOID NTAPI KeClearEvent(IN PKEVENT Event)
Definition: eventobj.c:22
return STATUS_SUCCESS
Definition: btrfs.c:2938
signed int * PLONG
Definition: retypes.h:5
#define Int32x32To64(a, b)