12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211 |
- /*++
- Copyright (c) 2012 Minoca Corp.
- This file is licensed under the terms of the GNU General Public License
- version 3. Alternative licensing terms are available. Contact
- info@minocacorp.com for details. See the LICENSE file at the root of this
- project for complete licensing information.
- Module Name:
- lock.c
- Abstract:
- This module implements common synchronization primitives in the kernel.
- Author:
- Evan Green 6-Aug-2012
- Environment:
- Kernel
- --*/
- //
- // ------------------------------------------------------------------- Includes
- //
- #include <minoca/kernel/kernel.h>
- //
- // ---------------------------------------------------------------- Definitions
- //
- #define QUEUED_LOCK_TAG 0x6C51654B // 'lQeK'
- #define SHARED_EXCLUSIVE_LOCK_TAG 0x6553654B // 'eSeK'
- //
- // Define shared exclusive lock states.
- //
- #define SHARED_EXCLUSIVE_LOCK_FREE 0
- #define SHARED_EXCLUSIVE_LOCK_EXCLUSIVE ((ULONG)-1)
- #define SHARED_EXCLUSIVE_LOCK_MAX_WAITERS ((ULONG)-2)
- //
- // ----------------------------------------------- Internal Function Prototypes
- //
- //
- // ------------------------------------------------------ Data Type Definitions
- //
- //
- // -------------------------------------------------------------------- Globals
- //
- //
- // Queued lock directory where all queued locks are stored. This is primarily
- // done to keep the root directory tidy.
- //
- POBJECT_HEADER KeQueuedLockDirectory = NULL;
- //
- // ------------------------------------------------------------------ Functions
- //
- KERNEL_API
- PQUEUED_LOCK
- KeCreateQueuedLock (
- VOID
- )
- /*++
- Routine Description:
- This routine creates a new queued lock under the current thread. These locks
- can be used at up to dispatch level if non-paged memory is used.
- Arguments:
- None.
- Return Value:
- Returns a pointer to the new lock on success.
- NULL on failure.
- --*/
- {
- PQUEUED_LOCK NewLock;
- POBJECT_HEADER NewObject;
- NewObject = ObCreateObject(ObjectQueuedLock,
- KeQueuedLockDirectory,
- NULL,
- 0,
- sizeof(QUEUED_LOCK),
- NULL,
- 0,
- QUEUED_LOCK_TAG);
- NewLock = (PQUEUED_LOCK)NewObject;
- if (NewLock != NULL) {
- //
- // Initialize the lock to signal one thread so the first wait acquires
- // it.
- //
- ObSignalObject(NewObject, SignalOptionSignalOne);
- }
- return NewLock;
- }
- KERNEL_API
- VOID
- KeDestroyQueuedLock (
- PQUEUED_LOCK Lock
- )
- /*++
- Routine Description:
- This routine destroys a queued lock by decrementing its reference count.
- Arguments:
- Lock - Supplies a pointer to the queued lock to destroy.
- Return Value:
- None. When the function returns, the lock must not be used again.
- --*/
- {
- ObReleaseReference(&(Lock->Header));
- return;
- }
- KERNEL_API
- VOID
- KeAcquireQueuedLock (
- PQUEUED_LOCK Lock
- )
- /*++
- Routine Description:
- This routine acquires the queued lock. If the lock is held, the thread
- blocks until it becomes available.
- Arguments:
- Lock - Supplies a pointer to the queued lock to acquire.
- Return Value:
- None. When the function returns, the lock will be held.
- --*/
- {
- KSTATUS Status;
- Status = KeAcquireQueuedLockTimed(Lock, WAIT_TIME_INDEFINITE);
- ASSERT(KSUCCESS(Status));
- return;
- }
- KERNEL_API
- KSTATUS
- KeAcquireQueuedLockTimed (
- PQUEUED_LOCK Lock,
- ULONG TimeoutInMilliseconds
- )
- /*++
- Routine Description:
- This routine acquires the queued lock. If the lock is held, the thread
- blocks until it becomes available or the specified timeout expires.
- Arguments:
- Lock - Supplies a pointer to the queued lock to acquire.
- TimeoutInMilliseconds - Supplies the number of milliseconds that the given
- object should be waited on before timing out. Use WAIT_TIME_INDEFINITE
- to wait forever on the object.
- Return Value:
- STATUS_SUCCESS on success.
- STATUS_TIMEOUT if the specified amount of time expired and the lock could
- not be acquired.
- --*/
- {
- KSTATUS Status;
- PKTHREAD Thread;
- Thread = KeGetCurrentThread();
- ASSERT(KeGetRunLevel() <= RunLevelDispatch);
- ASSERT((Lock->OwningThread != Thread) || (Thread == NULL));
- Status = ObWaitOnObject(&(Lock->Header), 0, TimeoutInMilliseconds);
- if (KSUCCESS(Status)) {
- Lock->OwningThread = Thread;
- }
- return Status;
- }
- KERNEL_API
- VOID
- KeReleaseQueuedLock (
- PQUEUED_LOCK Lock
- )
- /*++
- Routine Description:
- This routine releases a queued lock that has been previously acquired.
- Arguments:
- Lock - Supplies a pointer to the queued lock to release.
- Return Value:
- None.
- --*/
- {
- ASSERT(KeGetRunLevel() <= RunLevelDispatch);
- Lock->OwningThread = NULL;
- ObSignalObject(&(Lock->Header), SignalOptionSignalOne);
- return;
- }
- KERNEL_API
- BOOL
- KeTryToAcquireQueuedLock (
- PQUEUED_LOCK Lock
- )
- /*++
- Routine Description:
- This routine attempts to acquire the queued lock. If the lock is busy, it
- does not add this thread to the queue of waiters.
- Arguments:
- Lock - Supplies a pointer to a queued lock.
- Return Value:
- Returns TRUE if the lock was acquired, or FALSE otherwise.
- --*/
- {
- KSTATUS Status;
- ASSERT(KeGetRunLevel() <= RunLevelDispatch);
- Status = ObWaitOnObject(&(Lock->Header), 0, 0);
- if (!KSUCCESS(Status)) {
- return FALSE;
- }
- Lock->OwningThread = KeGetCurrentThread();
- return TRUE;
- }
- KERNEL_API
- BOOL
- KeIsQueuedLockHeld (
- PQUEUED_LOCK Lock
- )
- /*++
- Routine Description:
- This routine determines whether a queued lock is acquired or free.
- Arguments:
- Lock - Supplies a pointer to the queued lock.
- Return Value:
- TRUE if the queued lock is held.
- FALSE if the queued lock is free.
- --*/
- {
- if (Lock->Header.WaitQueue.State == SignaledForOne) {
- return FALSE;
- }
- return TRUE;
- }
- KERNEL_API
- VOID
- KeInitializeSpinLock (
- PKSPIN_LOCK Lock
- )
- /*++
- Routine Description:
- This routine initializes a spinlock.
- Arguments:
- Lock - Supplies a pointer to the lock to initialize.
- Return Value:
- None.
- --*/
- {
- Lock->LockHeld = 0;
- Lock->OwningThread = NULL;
- //
- // This atomic exchange serves as a memory barrier and serializing
- // instruction.
- //
- RtlAtomicExchange32(&(Lock->LockHeld), 0);
- return;
- }
- KERNEL_API
- VOID
- KeAcquireSpinLock (
- PKSPIN_LOCK Lock
- )
- /*++
- Routine Description:
- This routine acquires a kernel spinlock. It must be acquired at or below
- dispatch level. This routine may yield the processor.
- Arguments:
- Lock - Supplies a pointer to the lock to acquire.
- Return Value:
- None.
- --*/
- {
- ULONG LockValue;
- while (TRUE) {
- LockValue = RtlAtomicCompareExchange32(&(Lock->LockHeld), 1, 0);
- if (LockValue == 0) {
- break;
- }
- ArProcessorYield();
- }
- Lock->OwningThread = KeGetCurrentThread();
- return;
- }
- KERNEL_API
- VOID
- KeReleaseSpinLock (
- PKSPIN_LOCK Lock
- )
- /*++
- Routine Description:
- This routine releases a kernel spinlock.
- Arguments:
- Lock - Supplies a pointer to the lock to release.
- Return Value:
- None.
- --*/
- {
- ULONG LockValue;
- //
- // The interlocked version is a serializing instruction, so this avoids
- // unsafe processor and compiler reordering. Simply setting the lock to
- // FALSE is not safe.
- //
- LockValue = RtlAtomicExchange32(&(Lock->LockHeld), 0);
- //
- // Assert if the lock was not held.
- //
- ASSERT(LockValue != 0);
- return;
- }
- KERNEL_API
- BOOL
- KeTryToAcquireSpinLock (
- PKSPIN_LOCK Lock
- )
- /*++
- Routine Description:
- This routine makes one attempt to acquire a spinlock.
- Arguments:
- Lock - Supplies a pointer to the lock to attempt to acquire.
- Return Value:
- TRUE if the lock was acquired.
- FALSE if the lock was not acquired.
- --*/
- {
- ULONG LockValue;
- LockValue = RtlAtomicCompareExchange32(&(Lock->LockHeld), 1, 0);
- if (LockValue == 0) {
- Lock->OwningThread = KeGetCurrentThread();
- return TRUE;
- }
- return FALSE;
- }
- KERNEL_API
- BOOL
- KeIsSpinLockHeld (
- PKSPIN_LOCK Lock
- )
- /*++
- Routine Description:
- This routine determines whether a spin lock is held or free.
- Arguments:
- Lock - Supplies a pointer to the lock to check.
- Return Value:
- TRUE if the lock has been acquired.
- FALSE if the lock is free.
- --*/
- {
- ULONG Held;
- Held = RtlAtomicOr32(&(Lock->LockHeld), 0);
- if (Held != 0) {
- return TRUE;
- }
- return FALSE;
- }
- KERNEL_API
- PSHARED_EXCLUSIVE_LOCK
- KeCreateSharedExclusiveLock (
- VOID
- )
- /*++
- Routine Description:
- This routine creates a shared-exclusive lock.
- Arguments:
- None.
- Return Value:
- Returns a pointer to a shared-exclusive lock on success, or NULL on failure.
- --*/
- {
- PSHARED_EXCLUSIVE_LOCK SharedExclusiveLock;
- KSTATUS Status;
- SharedExclusiveLock = MmAllocateNonPagedPool(sizeof(SHARED_EXCLUSIVE_LOCK),
- SHARED_EXCLUSIVE_LOCK_TAG);
- if (SharedExclusiveLock == NULL) {
- Status = STATUS_INSUFFICIENT_RESOURCES;
- goto CreateSharedExclusiveLockEnd;
- }
- RtlZeroMemory(SharedExclusiveLock, sizeof(SHARED_EXCLUSIVE_LOCK));
- SharedExclusiveLock->Event = KeCreateEvent(NULL);
- if (SharedExclusiveLock->Event == NULL) {
- Status = STATUS_INSUFFICIENT_RESOURCES;
- goto CreateSharedExclusiveLockEnd;
- }
- KeSignalEvent(SharedExclusiveLock->Event, SignalOptionSignalOne);
- Status = STATUS_SUCCESS;
- CreateSharedExclusiveLockEnd:
- if (!KSUCCESS(Status)) {
- if (SharedExclusiveLock != NULL) {
- KeDestroySharedExclusiveLock(SharedExclusiveLock);
- SharedExclusiveLock = NULL;
- }
- }
- return SharedExclusiveLock;
- }
- KERNEL_API
- VOID
- KeDestroySharedExclusiveLock (
- PSHARED_EXCLUSIVE_LOCK SharedExclusiveLock
- )
- /*++
- Routine Description:
- This routine destroys a shared-exclusive lock.
- Arguments:
- SharedExclusiveLock - Supplies a pointer to a shared-exclusive lock.
- Return Value:
- None.
- --*/
- {
- if (SharedExclusiveLock->Event != NULL) {
- KeDestroyEvent(SharedExclusiveLock->Event);
- }
- MmFreeNonPagedPool(SharedExclusiveLock);
- return;
- }
- KERNEL_API
- VOID
- KeAcquireSharedExclusiveLockShared (
- PSHARED_EXCLUSIVE_LOCK SharedExclusiveLock
- )
- /*++
- Routine Description:
- This routine acquired the given shared-exclusive lock in shared mode.
- Arguments:
- SharedExclusiveLock - Supplies a pointer to the shared-exclusive lock.
- Return Value:
- None.
- --*/
- {
- ULONG ExclusiveWaiters;
- BOOL IsWaiter;
- ULONG PreviousState;
- ULONG PreviousWaiters;
- ULONG SharedWaiters;
- ULONG State;
- IsWaiter = FALSE;
- while (TRUE) {
- State = SharedExclusiveLock->State;
- ExclusiveWaiters = SharedExclusiveLock->ExclusiveWaiters;
- //
- // If no one is trying to acquire exclusive, or this is not the first
- // time around, try to acquire the lock. The reason subsequent attempts
- // are allowed to try to acquire even with exclusive waiters is that
- // without this, shared acquires may go down indefinitely on a free
- // lock (since they soaked up the "signal for one" and got woken up
- // ahead of the exclusive waiter).
- //
- if (((ExclusiveWaiters == 0) || (IsWaiter != FALSE)) &&
- (State < SHARED_EXCLUSIVE_LOCK_EXCLUSIVE - 1)) {
- PreviousState = State;
- State = RtlAtomicCompareExchange32(&(SharedExclusiveLock->State),
- PreviousState + 1,
- PreviousState);
- if (State == PreviousState) {
- //
- // Let all the blocked reader brethren go if this thread was
- // also blocked.
- //
- if (SharedExclusiveLock->SharedWaiters != 0) {
- KeSignalEvent(SharedExclusiveLock->Event,
- SignalOptionPulse);
- }
- break;
- //
- // The addition got foiled, go try again.
- //
- } else {
- continue;
- }
- }
- //
- // Either someone is trying to get it exclusive, or the attempt to
- // get it shared failed. Become a waiter so that the event will be
- // signaled when the lock is released. Use compare-exchange to avoid
- // overflowing.
- //
- if (IsWaiter == FALSE) {
- SharedWaiters = SharedExclusiveLock->SharedWaiters;
- if (SharedWaiters >= SHARED_EXCLUSIVE_LOCK_MAX_WAITERS) {
- continue;
- }
- PreviousWaiters = RtlAtomicCompareExchange32(
- &(SharedExclusiveLock->SharedWaiters),
- SharedWaiters + 1,
- SharedWaiters);
- if (PreviousWaiters != SharedWaiters) {
- continue;
- }
- IsWaiter = TRUE;
- }
- //
- // Recheck the condition now that the waiter count is incremented, as a
- // release may not have seen any waiters and therefore never signaled
- // the event.
- //
- if ((SharedExclusiveLock->ExclusiveWaiters == 0) &&
- (SharedExclusiveLock->State != SHARED_EXCLUSIVE_LOCK_EXCLUSIVE)) {
- continue;
- }
- KeWaitForEvent(SharedExclusiveLock->Event, FALSE, WAIT_TIME_INDEFINITE);
- }
- //
- // This thread is no longer waiting, away it goes.
- //
- if (IsWaiter != FALSE) {
- PreviousWaiters =
- RtlAtomicAdd32(&(SharedExclusiveLock->SharedWaiters), -1);
- ASSERT(PreviousWaiters != 0);
- }
- return;
- }
- KERNEL_API
- BOOL
- KeTryToAcquireSharedExclusiveLockShared (
- PSHARED_EXCLUSIVE_LOCK SharedExclusiveLock
- )
- /*++
- Routine Description:
- This routine makes a single attempt to acquire the given shared-exclusive
- lock in shared mode.
- Arguments:
- SharedExclusiveLock - Supplies a pointer to the shared-exclusive lock.
- Return Value:
- TRUE if the lock was successfully acquired shared.
- FALSE if the lock was not successfully acquired shared.
- --*/
- {
- ULONG ExclusiveWaiters;
- ULONG PreviousState;
- ULONG State;
- State = SharedExclusiveLock->State;
- ExclusiveWaiters = SharedExclusiveLock->ExclusiveWaiters;
- if ((ExclusiveWaiters == 0) &&
- (State < SHARED_EXCLUSIVE_LOCK_EXCLUSIVE - 1)) {
- PreviousState = State;
- State = RtlAtomicCompareExchange32(&(SharedExclusiveLock->State),
- PreviousState + 1,
- PreviousState);
- if (State == PreviousState) {
- //
- // Let all the blocked reader brethren go if this thread was
- // also blocked.
- //
- if (SharedExclusiveLock->SharedWaiters != 0) {
- KeSignalEvent(SharedExclusiveLock->Event,
- SignalOptionPulse);
- }
- return TRUE;
- }
- }
- return FALSE;
- }
- KERNEL_API
- VOID
- KeReleaseSharedExclusiveLockShared (
- PSHARED_EXCLUSIVE_LOCK SharedExclusiveLock
- )
- /*++
- Routine Description:
- This routine releases the given shared-exclusive lock from shared mode.
- Arguments:
- SharedExclusiveLock - Supplies a pointer to the shared-exclusive lock.
- Return Value:
- None.
- --*/
- {
- ULONG PreviousState;
- PreviousState = RtlAtomicAdd32(&(SharedExclusiveLock->State), -1);
- ASSERT((PreviousState < SHARED_EXCLUSIVE_LOCK_EXCLUSIVE) &&
- (PreviousState != SHARED_EXCLUSIVE_LOCK_FREE));
- //
- // If this was the last reader and there are writers waiting, signal the
- // event.
- //
- if ((PreviousState - 1 == SHARED_EXCLUSIVE_LOCK_FREE) &&
- (SharedExclusiveLock->ExclusiveWaiters != 0)) {
- KeSignalEvent(SharedExclusiveLock->Event, SignalOptionSignalOne);
- }
- return;
- }
- KERNEL_API
- VOID
- KeAcquireSharedExclusiveLockExclusive (
- PSHARED_EXCLUSIVE_LOCK SharedExclusiveLock
- )
- /*++
- Routine Description:
- This routine acquired the given shared-exclusive lock in exclusive mode.
- Arguments:
- SharedExclusiveLock - Supplies a pointer to the shared-exclusive lock.
- Return Value:
- None.
- --*/
- {
- ULONG CurrentState;
- ULONG ExclusiveWaiters;
- BOOL IsWaiting;
- ULONG PreviousWaiters;
- ULONG State;
- IsWaiting = FALSE;
- while (TRUE) {
- State = RtlAtomicCompareExchange32(&(SharedExclusiveLock->State),
- SHARED_EXCLUSIVE_LOCK_EXCLUSIVE,
- SHARED_EXCLUSIVE_LOCK_FREE);
- if (State == SHARED_EXCLUSIVE_LOCK_FREE) {
- break;
- }
- //
- // Increment the exclusive waiters count to indicate to readers that
- // the event needs to be signaled. Use compare-exchange to avoid
- // overflowing.
- //
- if (IsWaiting == FALSE) {
- ExclusiveWaiters = SharedExclusiveLock->ExclusiveWaiters;
- if (ExclusiveWaiters >= SHARED_EXCLUSIVE_LOCK_MAX_WAITERS) {
- continue;
- }
- PreviousWaiters = RtlAtomicCompareExchange32(
- &(SharedExclusiveLock->ExclusiveWaiters),
- ExclusiveWaiters + 1,
- ExclusiveWaiters);
- if (PreviousWaiters != ExclusiveWaiters) {
- continue;
- }
- IsWaiting = TRUE;
- }
- //
- // Recheck the state now that the exclusive waiters count has been
- // incremented, in case the release didn't see the increment and never
- // signaled the event.
- //
- CurrentState = SharedExclusiveLock->State;
- if (CurrentState == SHARED_EXCLUSIVE_LOCK_FREE) {
- continue;
- }
- KeWaitForEvent(SharedExclusiveLock->Event, FALSE, WAIT_TIME_INDEFINITE);
- }
- //
- // This lucky writer is no longer waiting.
- //
- if (IsWaiting != FALSE) {
- PreviousWaiters =
- RtlAtomicAdd32(&(SharedExclusiveLock->ExclusiveWaiters), -1);
- ASSERT(PreviousWaiters != 0);
- }
- return;
- }
- KERNEL_API
- BOOL
- KeTryToAcquireSharedExclusiveLockExclusive (
- PSHARED_EXCLUSIVE_LOCK SharedExclusiveLock
- )
- /*++
- Routine Description:
- This routine makes a single attempt to acquire the given shared-exclusive
- lock exclusively.
- Arguments:
- SharedExclusiveLock - Supplies a pointer to the shared-exclusive lock.
- Return Value:
- TRUE if the lock was successfully acquired exclusively.
- FALSE if the lock was not successfully acquired.
- --*/
- {
- ULONG State;
- State = RtlAtomicCompareExchange32(&(SharedExclusiveLock->State),
- SHARED_EXCLUSIVE_LOCK_EXCLUSIVE,
- SHARED_EXCLUSIVE_LOCK_FREE);
- if (State == SHARED_EXCLUSIVE_LOCK_FREE) {
- return TRUE;
- }
- return FALSE;
- }
- KERNEL_API
- VOID
- KeReleaseSharedExclusiveLockExclusive (
- PSHARED_EXCLUSIVE_LOCK SharedExclusiveLock
- )
- /*++
- Routine Description:
- This routine releases the given shared-exclusive lock from exclusive mode.
- Arguments:
- SharedExclusiveLock - Supplies a pointer to the shared-exclusive lock.
- Return Value:
- None.
- --*/
- {
- ASSERT(SharedExclusiveLock->State == SHARED_EXCLUSIVE_LOCK_EXCLUSIVE);
- RtlAtomicExchange32(&(SharedExclusiveLock->State),
- SHARED_EXCLUSIVE_LOCK_FREE);
- if ((SharedExclusiveLock->SharedWaiters != 0) ||
- (SharedExclusiveLock->ExclusiveWaiters != 0)) {
- KeSignalEvent(SharedExclusiveLock->Event, SignalOptionSignalOne);
- }
- return;
- }
- KERNEL_API
- VOID
- KeSharedExclusiveLockConvertToExclusive (
- PSHARED_EXCLUSIVE_LOCK SharedExclusiveLock
- )
- /*++
- Routine Description:
- This routine converts a lock that the caller holds shared into one that
- the caller holds exclusive. This routine will most likely fully release
- and reacquire the lock.
- Arguments:
- SharedExclusiveLock - Supplies a pointer to the shared-exclusive lock.
- Return Value:
- None.
- --*/
- {
- ULONG State;
- //
- // Try a shortcut in the case that this caller is the only one that has it
- // shared.
- //
- State = RtlAtomicCompareExchange32(&(SharedExclusiveLock->State),
- SHARED_EXCLUSIVE_LOCK_EXCLUSIVE,
- 1);
- ASSERT((State >= 1) && (State < SHARED_EXCLUSIVE_LOCK_EXCLUSIVE));
- //
- // If the fast conversion failed, get in line like everybody else.
- //
- if (State != 1) {
- KeReleaseSharedExclusiveLockShared(SharedExclusiveLock);
- KeAcquireSharedExclusiveLockExclusive(SharedExclusiveLock);
- }
- return;
- }
- KERNEL_API
- BOOL
- KeIsSharedExclusiveLockHeld (
- PSHARED_EXCLUSIVE_LOCK SharedExclusiveLock
- )
- /*++
- Routine Description:
- This routine determines whether a shared-exclusive lock is held or free.
- Arguments:
- SharedExclusiveLock - Supplies a pointer to a shared-exclusive lock.
- Return Value:
- Returns TRUE if the shared-exclusive lock is held, or FALSE if not.
- --*/
- {
- if (SharedExclusiveLock->State != SHARED_EXCLUSIVE_LOCK_FREE) {
- return TRUE;
- }
- return FALSE;
- }
- KERNEL_API
- BOOL
- KeIsSharedExclusiveLockHeldExclusive (
- PSHARED_EXCLUSIVE_LOCK SharedExclusiveLock
- )
- /*++
- Routine Description:
- This routine determines whether a shared-exclusive lock is held exclusively
- or not.
- Arguments:
- SharedExclusiveLock - Supplies a pointer to a shared-exclusive lock.
- Return Value:
- Returns TRUE if the shared-exclusive lock is held exclusively, or FALSE
- otherwise.
- --*/
- {
- if (SharedExclusiveLock->State == SHARED_EXCLUSIVE_LOCK_EXCLUSIVE) {
- return TRUE;
- }
- return FALSE;
- }
- KERNEL_API
- BOOL
- KeIsSharedExclusiveLockHeldShared (
- PSHARED_EXCLUSIVE_LOCK SharedExclusiveLock
- )
- /*++
- Routine Description:
- This routine determines whether a shared-exclusive lock is held shared or
- not.
- Arguments:
- SharedExclusiveLock - Supplies a pointer to a shared-exclusive lock.
- Return Value:
- Returns TRUE if the shared-exclusive lock is held shared, or FALSE
- otherwise.
- --*/
- {
- if ((SharedExclusiveLock->State != SHARED_EXCLUSIVE_LOCK_FREE) &&
- (SharedExclusiveLock->State < SHARED_EXCLUSIVE_LOCK_EXCLUSIVE)) {
- return TRUE;
- }
- return FALSE;
- }
- KERNEL_API
- BOOL
- KeIsSharedExclusiveLockContended (
- PSHARED_EXCLUSIVE_LOCK SharedExclusiveLock
- )
- /*++
- Routine Description:
- This routine determines whether a shared-exclusive lock is being waited on
- for shared or exclusive access.
- Arguments:
- SharedExclusiveLock - Supplies a pointer to a shared-exclusive lock.
- Return Value:
- Returns TRUE if other threads are waiting to acquire the lock, or FALSE
- if the lock is uncontented.
- --*/
- {
- if ((SharedExclusiveLock->SharedWaiters != 0) ||
- (SharedExclusiveLock->ExclusiveWaiters != 0)) {
- return TRUE;
- }
- return FALSE;
- }
- //
- // --------------------------------------------------------- Internal Functions
- //
|