ReactOS  r75214
resource.c File Reference
#include <ntoskrnl.h>
#include <debug.h>
Include dependency graph for resource.c:

Go to the source code of this file.

Macros

#define NDEBUG
 
#define IsExclusiveWaiting(r)   (r->NumberOfExclusiveWaiters > 0)
 
#define IsSharedWaiting(r)   (r->NumberOfSharedWaiters > 0)
 
#define IsOwnedExclusive(r)   (r->Flag & ResourceOwnedExclusive)
 
#define IsBoostAllowed(r)   (!(r->Flag & ResourceHasDisabledPriorityBoost))
 
#define ExpVerifyResource(r)
 
#define ExpCheckForApcsDisabled(b, r, t)
 

Functions

FORCEINLINE VOID ExAcquireResourceLock (IN PERESOURCE Resource, IN PKLOCK_QUEUE_HANDLE LockHandle)
 
FORCEINLINE VOID ExReleaseResourceLock (IN PERESOURCE Resource, IN PKLOCK_QUEUE_HANDLE LockHandle)
 
VOID NTAPI INIT_FUNCTION ExpResourceInitialization (VOID)
 
VOID NTAPI ExpAllocateExclusiveWaiterEvent (IN PERESOURCE Resource, IN PKLOCK_QUEUE_HANDLE LockHandle)
 
VOID NTAPI ExpAllocateSharedWaiterSemaphore (IN PERESOURCE Resource, IN PKLOCK_QUEUE_HANDLE LockHandle)
 
VOID NTAPI ExpExpandResourceOwnerTable (IN PERESOURCE Resource, IN PKLOCK_QUEUE_HANDLE LockHandle)
 
POWNER_ENTRY FASTCALL ExpFindFreeEntry (IN PERESOURCE Resource, IN PKLOCK_QUEUE_HANDLE LockHandle)
 
POWNER_ENTRY FASTCALL ExpFindEntryForThread (IN PERESOURCE Resource, IN ERESOURCE_THREAD Thread, IN PKLOCK_QUEUE_HANDLE LockHandle, IN BOOLEAN FirstEntryInelligible)
 
VOID FASTCALL ExpBoostOwnerThread (IN PKTHREAD Thread, IN PKTHREAD OwnerThread)
 
VOID FASTCALL ExpWaitForResource (IN PERESOURCE Resource, IN PVOID Object)
 
BOOLEAN NTAPI ExAcquireResourceExclusiveLite (IN PERESOURCE Resource, IN BOOLEAN Wait)
 
BOOLEAN NTAPI ExAcquireResourceSharedLite (IN PERESOURCE Resource, IN BOOLEAN Wait)
 
BOOLEAN NTAPI ExAcquireSharedStarveExclusive (IN PERESOURCE Resource, IN BOOLEAN Wait)
 
BOOLEAN NTAPI ExAcquireSharedWaitForExclusive (IN PERESOURCE Resource, IN BOOLEAN Wait)
 
VOID NTAPI ExConvertExclusiveToSharedLite (IN PERESOURCE Resource)
 
NTSTATUS NTAPI ExDeleteResourceLite (IN PERESOURCE Resource)
 
VOID NTAPI ExDisableResourceBoostLite (IN PERESOURCE Resource)
 
ULONG NTAPI ExGetExclusiveWaiterCount (IN PERESOURCE Resource)
 
ULONG NTAPI ExGetSharedWaiterCount (IN PERESOURCE Resource)
 
NTSTATUS NTAPI ExInitializeResourceLite (IN PERESOURCE Resource)
 
BOOLEAN NTAPI ExIsResourceAcquiredExclusiveLite (IN PERESOURCE Resource)
 
ULONG NTAPI ExIsResourceAcquiredSharedLite (IN PERESOURCE Resource)
 
NTSTATUS NTAPI ExReinitializeResourceLite (IN PERESOURCE Resource)
 
VOID FASTCALL ExReleaseResourceLite (IN PERESOURCE Resource)
 
VOID NTAPI ExReleaseResourceForThreadLite (IN PERESOURCE Resource, IN ERESOURCE_THREAD Thread)
 
VOID NTAPI ExSetResourceOwnerPointer (IN PERESOURCE Resource, IN PVOID OwnerPointer)
 
BOOLEAN NTAPI ExTryToAcquireResourceExclusiveLite (IN PERESOURCE Resource)
 
PVOID NTAPI ExEnterCriticalRegionAndAcquireResourceExclusive (IN PERESOURCE Resource)
 
PVOID NTAPI ExEnterCriticalRegionAndAcquireResourceShared (IN PERESOURCE Resource)
 
PVOID NTAPI ExEnterCriticalRegionAndAcquireSharedWaitForExclusive (IN PERESOURCE Resource)
 
VOID FASTCALL ExReleaseResourceAndLeaveCriticalRegion (IN PERESOURCE Resource)
 

Variables

LARGE_INTEGER ExShortTime = {{-100000, -1}}
 
LARGE_INTEGER ExpTimeout
 
ULONG ExpResourceTimeoutCount = 90 * 3600 / 2
 
KSPIN_LOCK ExpResourceSpinLock
 
LIST_ENTRY ExpSystemResourcesList
 
BOOLEAN ExResourceStrict = TRUE
 

Macro Definition Documentation

#define ExpCheckForApcsDisabled (   b,
  r,
  t 
)
#define IsBoostAllowed (   r)    (!(r->Flag & ResourceHasDisabledPriorityBoost))

Definition at line 19 of file resource.c.

Referenced by ExpWaitForResource().

#define IsExclusiveWaiting (   r)    (r->NumberOfExclusiveWaiters > 0)
#define IsSharedWaiting (   r)    (r->NumberOfSharedWaiters > 0)
#define NDEBUG

Definition at line 12 of file resource.c.

Function Documentation

BOOLEAN NTAPI ExAcquireResourceExclusiveLite ( IN PERESOURCE  Resource,
IN BOOLEAN  Wait 
)

Definition at line 770 of file resource.c.

Referenced by __brelse(), __RxAcquireFcb(), _do_load_tree(), _free_fileref(), _free_tree(), acquire_for_lazy_write(), add_balance_item(), add_calc_job(), add_data_reloc(), add_device(), add_dir_child(), add_metadata_reloc(), add_metadata_reloc_extent_item(), AddUnregisteredProvider(), alloc_chunk(), allocate_cache(), ApphelpCacheAcquireLock(), balance_data_chunk(), balance_metadata_chunk(), balance_thread(), BroadcastOpen(), BuildAndSubmitIrp(), calc_thread(), CcPinRead(), CdAcquireForCreateSection(), CdAcquireResource(), CdfsAcquireForLazyWrite(), CdfsCleanup(), CdfsCreate(), CdfsShortNameCacheGet(), CdfsVerifyVolume(), clean_space_cache(), close_file(), CmpLockHiveFlusherExclusive(), CmpLockRegistryExclusive(), CountAvailableClusters(), create_snapshot(), create_subvol(), CreateRedirectedFile(), delete_fileref(), delete_reparse_point(), disk_arrival(), disk_removal(), dismount_volume(), do_calc(), do_flush(), do_rollback(), do_write(), DoQuery(), drv_cleanup(), drv_flush_buffers(), drv_set_ea(), DxEngLockHdev(), ExAcquireTimeRefreshLock(), ExEnterCriticalRegionAndAcquireResourceExclusive(), Ext2AcquireFileForCcFlush(), Ext2AcquireFileForModWrite(), Ext2AcquireForCreateSection(), Ext2AcquireForLazyWrite(), Ext2AddEntry(), Ext2CheckDismount(), Ext2Cleanup(), Ext2CleanupAllMcbs(), Ext2Close(), Ext2Create(), Ext2CreateFile(), Ext2DeleteFile(), Ext2DeleteReparsePoint(), Ext2DismountVolume(), Ext2DropBH(), Ext2FirstUnusedMcb(), Ext2FloppyFlush(), Ext2Flush(), Ext2FlushFiles(), Ext2FlushVolume(), Ext2FreeBlock(), Ext2FreeInode(), Ext2GetRetrievalPointerBase(), Ext2GetRetrievalPointers(), Ext2InsertMcb(), Ext2InvalidateVolumes(), Ext2LinkHeadMcb(), Ext2LinkTailMcb(), Ext2LoadGroup(), Ext2LockVolume(), Ext2LookupFile(), Ext2MediaEjectControl(), Ext2MountVolume(), Ext2NewBlock(), Ext2NewInode(), Ext2NotifyChangeDirectory(), Ext2OplockRequest(), Ext2PreAcquireForCreateSection(), Ext2ProcessGlobalProperty(), Ext2ProcessVolumeProperty(), Ext2PurgeVolume(), Ext2QueryRetrievalPointers(), Ext2QueryUnusedBH(), Ext2QueryUnusedFcb(), Ext2ReadFile(), Ext2ReadVolume(), Ext2ReleaseFcb(), Ext2RemoveEntry(), Ext2RemoveMcb(), Ext2SetFileInformation(), Ext2SetFileType(), Ext2SetLinkInfo(), Ext2SetParentEntry(), Ext2SetRenameInfo(), Ext2SetReparsePoint(), Ext2SetVolumeInformation(), Ext2ShutDown(), Ext2UnlinkFcb(), Ext2UnlinkMcb(), Ext2UnlockVolume(), Ext2UpdateGroupDirStat(), Ext2VerifyVolume(), Ext2WriteFile(), Ext2WriteVolume(), FatAcquireExclusiveFcb(), FatAcquireExclusiveVcb(), FatiCleanup(), FatiCleanVcbs(), FatiCommonClose(), FatiOpenExistingFcb(), FatiOverwriteFile(), FatPendingClose(), FatSetEndOfFileInfo(), fcb_get_last_dir_index(), FFSAddEntry(), FFSAddMcbEntry(), FFSAllocateCcb(), FFSAllocateIrpContext(), FFSAllocateMcb(), FFSCheckDismount(), FFSCleanup(), FFSClose(), FFSCreateFile(), FFSCreateVolume(), FFSDismountVolume(), FFSFlush(), FFSFlushFiles(), FFSFreeCcb(), FFSFreeFcb(), FFSFreeIrpContext(), FFSFreeMcb(), FFSInvalidateVolumes(), FFSLockVolume(), FFSLookupMcbEntry(), FFSMediaEjectControl(), FFSMountVolume(), FFSNotifyChangeDirectory(), FFSPurgeVolume(), FFSRemoveEntry(), FFSRemoveMcbEntry(), FFSSetInformation(), FFSSetParentEntry(), FFSShutDown(), FFSUnlockVolume(), FFSv1AllocateFcb(), FFSv2AllocateFcb(), FFSVerifyVolume(), FFSWriteFile(), FFSWriteVolume(), file_create2(), fill_in_hard_link_information(), finish_removing_device(), FsRtlAcquireFileExclusiveCommon(), FsRtlAcquireFileForCcFlushEx(), FsRtlAcquireFileForModWriteEx(), FsRtlCopyWrite(), FsRtlPrepareMdlWriteDev(), get_block_bh_mdl(), get_block_bh_pin(), get_tree_new_address(), GetNextClusterExtend(), insert_cache_extent(), insert_extent(), insert_extent_chunk(), insert_fileref_child(), insert_prealloc_extent(), insert_tree_extent(), insert_tree_extent_skinny(), invalidate_volumes(), IoEnumerateRegisteredFiltersList(), IopActionInitChildServices(), IopAttachFilterDriversCallback(), IopBootLog(), IopCreateLogFile(), IopGetSetSecurityObject(), IopLoadUnloadDriver(), IopSaveBootLogToFile(), IopSetDeviceSecurityDescriptor(), IoRegisterFileSystem(), IoRegisterFsRegistrationChange(), IoShutdownSystem(), IoUnregisterFileSystem(), IoUnregisterFsRegistrationChange(), KsAcquireDeviceSecurityLock(), KspSynchronizedEventRoutine(), lock_volume(), lzo_write_compressed_bit(), MiLoadUserSymbols(), MiProcessLoaderEntry(), mount_vol(), move_across_subvols(), MupCheckForUnregisteredProvider(), MupCleanup(), MupCleanupFcb(), MupCleanupVcb(), MupCloseFcb(), MupCloseUncProvider(), MupCloseVcb(), MupDecodeFileObject(), MupDereferenceCcb(), MupDereferenceMasterQueryContext(), MupForwardIoRequest(), MupInvalidatePrefixTable(), notify_change_directory(), NpAcquireExclusiveVcb(), NpCommonFlushBuffers(), NpCommonRead(), NpCommonWrite(), NpDisconnect(), NpListen(), NpQueryClientProcess(), NpSetClientProcess(), NpTransceive(), NtfsCleanup(), NtfsCleanupFile(), NtfsClose(), NtfsCreate(), NtfsQueryDirectory(), ObpAcquireObjectLock(), ObpEnterObjectTypeMutex(), open_file(), OpenMupFileSystem(), pnp_cancel_remove_device(), pnp_query_remove_device(), QSI_DEF(), QueryPathCompletionRoutine(), ReadVolumeLabel(), reduce_tree_extent(), refresh_mountmgr(), RegisterUncProvider(), remove_balance_item(), RfsdAllocateCcb(), RfsdAllocateFcb(), RfsdAllocateIrpContext(), RfsdAllocateMcb(), RfsdCheckDismount(), RfsdCleanup(), RfsdClose(), RfsdCreateFile(), RfsdCreateVolume(), RfsdDismountVolume(), RfsdFlush(), RfsdFlushFiles(), RfsdFreeCcb(), RfsdFreeFcb(), RfsdFreeIrpContext(), RfsdFreeMcb(), RfsdInvalidateVolumes(), RfsdLockVolume(), RfsdMediaEjectControl(), RfsdMountVolume(), RfsdNotifyChangeDirectory(), RfsdPurgeVolume(), RfsdSetInformation(), RfsdShutDown(), RfsdUnlockVolume(), RfsdVerifyVolume(), RfsdWriteFile(), RfsdWriteVolume(), RtlEnterHeapLock(), RtlTryEnterHeapLock(), RxFindOrCreateFcb(), RxpAcquirePrefixTableLockExclusive(), RxStartMinirdr(), send_notification_fcb(), set_basic_information(), set_disposition_information(), set_end_of_file_information(), set_file_security(), set_inode_info(), set_label(), set_link_information(), set_rename_information(), set_reparse_point(), set_sparse(), set_zero_data(), test_vol(), TestResourceExclusiveAccess(), TestResourceSharedAccess(), TestResourceWithOwner(), TestResourceWithThreads(), try_extend_data(), uninit(), update_changed_extent_ref(), update_chunk_caches(), update_chunk_usage(), update_chunks(), update_volumes(), UserEnterExclusive(), VfatAcquireForCcFlush(), VfatAcquireForLazyWrite(), VfatAcquireForReadAhead(), VfatCleanup(), VfatCleanupFile(), VfatClose(), VfatCreate(), VfatCreateFile(), VfatDismountVolume(), VfatFlush(), VfatFlushVolume(), VfatMount(), VfatSetInformation(), VfatSetVolumeInformation(), VfatShutdown(), VfatWrite(), WCacheDirect__(), WCacheDiscardBlocks__(), WCacheFlushAll__(), WCacheFlushBlocks__(), WCachePurgeAll__(), WCacheReadBlocks__(), WCacheRelease__(), WCacheStartDirect__(), WCacheWriteBlocks__(), write_file2(), write_metadata_items(), WriteCluster(), and zlib_write_compressed_bit().

772 {
776 
777  /* Sanity check */
778  ASSERT((Resource->Flag & ResourceNeverExclusive) == 0);
779 
780  /* Get the thread */
781  Thread = ExGetCurrentResourceThread();
782 
783  /* Sanity check and validation */
786 
787  /* Acquire the lock */
788  ExAcquireResourceLock(Resource, &LockHandle);
789  ExpCheckForApcsDisabled(LockHandle.OldIrql, Resource, (PKTHREAD)Thread);
790 
791  /* Check if there is a shared owner or exclusive owner */
792 TryAcquire:
793  if (Resource->ActiveEntries)
794  {
795  /* Check if it's exclusively owned, and we own it */
796  if ((IsOwnedExclusive(Resource)) &&
797  (Resource->OwnerEntry.OwnerThread == Thread))
798  {
799  /* Increase the owning count */
800  Resource->OwnerEntry.OwnerCount++;
801  Success = TRUE;
802  }
803  else
804  {
805  /*
806  * If the caller doesn't want us to wait, we can't acquire the
807  * resource because someone else then us owns it. If we can wait,
808  * then we'll wait.
809  */
810  if (!Wait)
811  {
812  Success = FALSE;
813  }
814  else
815  {
816  /* Check if it has exclusive waiters */
817  if (!Resource->ExclusiveWaiters)
818  {
819  /* It doesn't, allocate the event and try acquiring again */
821  goto TryAcquire;
822  }
823 
824  /* Has exclusive waiters, wait on it */
825  Resource->NumberOfExclusiveWaiters++;
826  ExReleaseResourceLock(Resource, &LockHandle);
827  ExpWaitForResource(Resource, Resource->ExclusiveWaiters);
828 
829  /* Set owner and return success */
830  Resource->OwnerEntry.OwnerThread = ExGetCurrentResourceThread();
831  return TRUE;
832  }
833  }
834  }
835  else
836  {
837  /* Nobody owns it, so let's! */
838  ASSERT(Resource->ActiveEntries == 0);
839  ASSERT(Resource->ActiveCount == 0);
841  Resource->ActiveEntries = 1;
842  Resource->ActiveCount = 1;
843  Resource->OwnerEntry.OwnerThread = Thread;
844  Resource->OwnerEntry.OwnerCount = 1;
845  Success = TRUE;
846  }
847 
848  /* Release the lock and return */
849  ExReleaseResourceLock(Resource, &LockHandle);
850  return Success;
851 }
#define ExGetCurrentResourceThread()
Definition: env_spec_w32.h:633
ULONG_PTR ERESOURCE_THREAD
Definition: extypes.h:208
#define TRUE
Definition: types.h:120
#define ResourceNeverExclusive
Definition: extypes.h:245
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel)?(CompletionRoutine!=NULL):TRUE)
BOOLEAN NTAPI KeIsExecutingDpc(VOID)
Definition: dpc.c:946
#define FALSE
Definition: types.h:117
#define ExpVerifyResource(r)
Definition: resource.c:150
_Acquires_shared_lock_ _Curr_ PERESOURCE Resource
Definition: exfuncs.h:440
unsigned char BOOLEAN
FORCEINLINE VOID ExReleaseResourceLock(IN PERESOURCE Resource, IN PKLOCK_QUEUE_HANDLE LockHandle)
Definition: resource.c:37
_In_opt_ PFILE_OBJECT _In_opt_ PETHREAD Thread
Definition: fltkernel.h:2653
VOID NTAPI ExpAllocateExclusiveWaiterEvent(IN PERESOURCE Resource, IN PKLOCK_QUEUE_HANDLE LockHandle)
Definition: resource.c:196
#define IsOwnedExclusive(r)
Definition: resource.c:18
#define ExpCheckForApcsDisabled(b, r, t)
Definition: resource.c:151
FORCEINLINE VOID ExAcquireResourceLock(IN PERESOURCE Resource, IN PKLOCK_QUEUE_HANDLE LockHandle)
Definition: resource.c:25
_In_ BOOLEAN Wait
Definition: cctypes.h:23
*LockHandle LockHandle _Out_ PKLOCK_QUEUE_HANDLE LockHandle
Definition: kefuncs.h:742
VOID FASTCALL ExpWaitForResource(IN PERESOURCE Resource, IN PVOID Object)
Definition: resource.c:618
#define ResourceOwnedExclusive
Definition: dldetect.h:32
BOOLEAN NTAPI ExAcquireResourceSharedLite ( IN PERESOURCE  Resource,
IN BOOLEAN  Wait 
)

Definition at line 885 of file resource.c.

Referenced by __RxAcquireFcb(), acquire_for_lazy_write(), add_children_to_move_list(), balance_data_chunk(), balance_thread(), CcPinRead(), CdAcquireForCache(), CdAcquireResource(), CdFastQueryBasicInfo(), CdFastQueryNetworkInfo(), CdFastQueryStdInfo(), CmpLockHiveFlusherShared(), CmpLockRegistry(), delete_reparse_point(), do_read_job(), DoQuery(), drv_cleanup(), drv_create(), drv_query_ea(), drv_query_information(), drv_query_volume_information(), drv_read(), Ex2ProcessUserPerfStat(), ExEnterCriticalRegionAndAcquireResourceShared(), Ext2AcquireForReadAhead(), Ext2bhReaperThread(), Ext2DeleteReparsePoint(), Ext2ExceptionHandler(), Ext2FastIoQueryBasicInfo(), Ext2FastIoQueryNetworkOpenInfo(), Ext2FastIoQueryStandardInfo(), Ext2FastIoWrite(), Ext2FcbReaperThread(), Ext2LockControl(), Ext2McbReaperThread(), Ext2OplockRequest(), Ext2QueryDirectory(), Ext2QueryFileInformation(), Ext2QueryVolumeInformation(), Ext2ReadFile(), Ext2SearchMcb(), Ext2SetReparsePoint(), Ext2WriteFile(), fast_io_acquire_for_mod_write(), fast_query_basic_info(), fast_query_standard_info(), FatAcquireSharedFcb(), FatAcquireSharedVcb(), FFSAcquireForLazyWrite(), FFSAcquireForReadAhead(), FFSFastIoQueryBasicInfo(), FFSFastIoQueryNetworkOpenInfo(), FFSFastIoQueryStandardInfo(), FFSQueryDirectory(), FFSQueryInformation(), FFSQueryVolumeInformation(), FFSReadFile(), FFSReadVolume(), FFSWriteFile(), FFSWriteVolume(), fill_in_file_stream_information(), fill_in_hard_link_information(), find_device_from_uuid(), find_file_in_dir(), FltUnregisterFilter(), FsRtlAcquireFileForCcFlushEx(), FsRtlCopyRead(), FsRtlCopyWrite(), FsRtlPrepareMdlWriteDev(), get_chunk_from_address(), get_devices(), get_inode_info(), get_object_id(), get_reparse_point(), get_reparse_tag(), get_usage(), GetNextCluster(), insert_cache_extent(), insert_extent(), insert_prealloc_extent(), invalidate_volumes(), IopMountVolume(), IopParseDevice(), IopSetDeviceSecurityDescriptor(), is_device_part_of_mounted_btrfs_raid(), is_volume_mounted(), KsAcquireDeviceSecurityLock(), load_chunk_root(), lzo_write_compressed_bit(), MmGetSystemRoutineAddress(), mount_vol(), move_across_subvols(), notify_change_directory(), NpAcquireSharedVcb(), NtfsQueryDirectory(), NtfsQueryInformation(), NtfsQueryVolumeInformation(), ObpAcquireObjectLockShared(), open_file(), query_directory(), query_filesystems(), query_ranges(), raid_generations_okay(), remove_device(), RfsdAcquireForLazyWrite(), RfsdAcquireForReadAhead(), RfsdFastIoQueryBasicInfo(), RfsdFastIoQueryNetworkOpenInfo(), RfsdFastIoQueryStandardInfo(), RfsdQueryDirectory(), RfsdQueryInformation(), RfsdQueryVolumeInformation(), RfsdReadFile(), RfsdReadVolume(), RfsdWriteFile(), RfsdWriteVolume(), RtlEnterHeapLock(), RtlTryEnterHeapLock(), RxCommonRead(), RxpAcquirePrefixTableLockShared(), send_disks_pnp_message(), set_end_of_file_information(), set_link_information(), set_rename_information(), set_reparse_point(), set_sparse(), set_zero_data(), TestResourceExclusiveAccess(), TestResourceSharedAccess(), TestResourceWithThreads(), update_chunk_usage(), update_volumes(), UserEnterShared(), verify_vcb(), verify_volume(), VfatFastIoQueryBasicInfo(), VfatFastIoQueryStandardInfo(), VfatGetRetrievalPointers(), VfatQueryInformation(), VfatQueryVolumeInformation(), VfatRead(), VfatWrite(), WCacheStartDirect__(), write_file2(), and zlib_write_compressed_bit().

887 {
891  BOOLEAN FirstEntryBusy;
892 
893  /* Get the thread */
894  Thread = ExGetCurrentResourceThread();
895 
896  /* Sanity check and validation */
899 
900  /* Acquire the lock */
901  ExAcquireResourceLock(Resource, &LockHandle);
902  ExpCheckForApcsDisabled(LockHandle.OldIrql, Resource, (PKTHREAD)Thread);
903 
904  /* Check how many active entries we've got */
905  while (Resource->ActiveEntries != 0)
906  {
907  /* Check if it's exclusively owned */
909  {
910  /* Check if we own it */
911  if (Resource->OwnerEntry.OwnerThread == Thread)
912  {
913  /* Increase the owning count */
914  Resource->OwnerEntry.OwnerCount++;
915 
916  /* Release the lock and return */
917  ExReleaseResourceLock(Resource, &LockHandle);
918  return TRUE;
919  }
920 
921  /* Find a free entry */
922  Owner = ExpFindFreeEntry(Resource, &LockHandle);
923  if (!Owner) continue;
924  }
925  else
926  {
927  /* Resource is shared, find who owns it */
928  FirstEntryBusy = IsExclusiveWaiting(Resource);
930  Thread,
931  &LockHandle,
932  FirstEntryBusy);
933  if (!Owner) continue;
934 
935  /* Is it us? */
936  if (Owner->OwnerThread == Thread)
937  {
938  /* Increase acquire count and return */
939  Owner->OwnerCount++;
940  ASSERT(Owner->OwnerCount != 0);
941 
942  /* Release the lock and return */
943  ExReleaseResourceLock(Resource, &LockHandle);
944  return TRUE;
945  }
946 
947  /* Try to find if there are exclusive waiters */
948  if (!FirstEntryBusy)
949  {
950  /* There are none, so acquire it */
951  Owner->OwnerThread = Thread;
952  Owner->OwnerCount = 1;
953 
954  /* Check how many active entries we had */
955  if (Resource->ActiveEntries == 0)
956  {
957  /* Set initial counts */
958  ASSERT(Resource->ActiveCount == 0);
959  Resource->ActiveEntries = 1;
960  Resource->ActiveCount = 1;
961  }
962  else
963  {
964  /* Increase active entries */
965  ASSERT(Resource->ActiveCount == 1);
966  Resource->ActiveEntries++;
967  }
968 
969  /* Release the lock and return */
970  ExReleaseResourceLock(Resource, &LockHandle);
971  return TRUE;
972  }
973  }
974 
975  /* If we got here, then we need to wait. Are we allowed? */
976  if (!Wait)
977  {
978  /* Release the lock and return */
979  ExReleaseResourceLock(Resource, &LockHandle);
980  return FALSE;
981  }
982 
983  /* Check if we have a shared waiters semaphore */
984  if (!Resource->SharedWaiters)
985  {
986  /* Allocate it and try another acquire */
988  }
989  else
990  {
991  /* We have shared waiters, wait for it */
992  break;
993  }
994  }
995 
996  /* Did we get here because we don't have active entries? */
997  if (Resource->ActiveEntries == 0)
998  {
999  /* Acquire it */
1000  ASSERT(Resource->ActiveEntries == 0);
1001  ASSERT(Resource->ActiveCount == 0);
1002  Resource->ActiveEntries = 1;
1003  Resource->ActiveCount = 1;
1004  Resource->OwnerEntry.OwnerThread = Thread;
1005  Resource->OwnerEntry.OwnerCount = 1;
1006 
1007  /* Release the lock and return */
1008  ExReleaseResourceLock(Resource, &LockHandle);
1009  return TRUE;
1010  }
1011 
1012  /* Now wait for the resource */
1013  Owner->OwnerThread = Thread;
1014  Owner->OwnerCount = 1;
1015  Resource->NumberOfSharedWaiters++;
1016 
1017  /* Release the lock and return */
1018  ExReleaseResourceLock(Resource, &LockHandle);
1019  ExpWaitForResource(Resource, Resource->SharedWaiters);
1020  return TRUE;
1021 }
#define ExGetCurrentResourceThread()
Definition: env_spec_w32.h:633
Definition: extypes.h:210
ULONG_PTR ERESOURCE_THREAD
Definition: extypes.h:208
#define IsExclusiveWaiting(r)
Definition: resource.c:16
#define TRUE
Definition: types.h:120
POWNER_ENTRY FASTCALL ExpFindFreeEntry(IN PERESOURCE Resource, IN PKLOCK_QUEUE_HANDLE LockHandle)
Definition: resource.c:416
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel)?(CompletionRoutine!=NULL):TRUE)
POWNER_ENTRY FASTCALL ExpFindEntryForThread(IN PERESOURCE Resource, IN ERESOURCE_THREAD Thread, IN PKLOCK_QUEUE_HANDLE LockHandle, IN BOOLEAN FirstEntryInelligible)
Definition: resource.c:479
BOOLEAN NTAPI KeIsExecutingDpc(VOID)
Definition: dpc.c:946
#define FALSE
Definition: types.h:117
#define ExpVerifyResource(r)
Definition: resource.c:150
_Acquires_shared_lock_ _Curr_ PERESOURCE Resource
Definition: exfuncs.h:440
smooth NULL
Definition: ftsmooth.c:513
unsigned char BOOLEAN
ULONG OwnerCount
Definition: extypes.h:216
FORCEINLINE VOID ExReleaseResourceLock(IN PERESOURCE Resource, IN PKLOCK_QUEUE_HANDLE LockHandle)
Definition: resource.c:37
_In_opt_ PFILE_OBJECT _In_opt_ PETHREAD Thread
Definition: fltkernel.h:2653
#define IsOwnedExclusive(r)
Definition: resource.c:18
#define ExpCheckForApcsDisabled(b, r, t)
Definition: resource.c:151
FORCEINLINE VOID ExAcquireResourceLock(IN PERESOURCE Resource, IN PKLOCK_QUEUE_HANDLE LockHandle)
Definition: resource.c:25
VOID NTAPI ExpAllocateSharedWaiterSemaphore(IN PERESOURCE Resource, IN PKLOCK_QUEUE_HANDLE LockHandle)
Definition: resource.c:256
_Out_writes_bytes_to_opt_ AbsoluteSecurityDescriptorSize PSECURITY_DESCRIPTOR _Inout_ PULONG _Out_writes_bytes_to_opt_ DaclSize PACL _Inout_ PULONG _Out_writes_bytes_to_opt_ SaclSize PACL _Inout_ PULONG _Out_writes_bytes_to_opt_ OwnerSize PSID Owner
Definition: rtlfuncs.h:1557
_In_ BOOLEAN Wait
Definition: cctypes.h:23
ERESOURCE_THREAD OwnerThread
Definition: extypes.h:211
*LockHandle LockHandle _Out_ PKLOCK_QUEUE_HANDLE LockHandle
Definition: kefuncs.h:742
VOID FASTCALL ExpWaitForResource(IN PERESOURCE Resource, IN PVOID Object)
Definition: resource.c:618
BOOLEAN NTAPI ExAcquireSharedStarveExclusive ( IN PERESOURCE  Resource,
IN BOOLEAN  Wait 
)

Definition at line 1063 of file resource.c.

Referenced by __RxAcquireFcb(), CdAcquireForCreateSection(), CdAcquireResource(), CmpLockRegistry(), Ext2FloppyFlush(), Ext2FlushVolume(), Ext2PurgeFile(), Ext2PurgeVolume(), Ext2WriteFile(), Ext2WriteVolume(), FFSFloppyFlush(), FFSFlushVolume(), FFSPurgeFile(), FFSPurgeVolume(), FFSv1WriteInode(), FFSWriteFile(), FFSWriteVolume(), get_block_bh_mdl(), get_block_bh_pin(), RfsdFloppyFlush(), RfsdFlushVolume(), RfsdPurgeFile(), RfsdPurgeVolume(), RfsdWriteFile(), RfsdWriteVolume(), TestResourceExclusiveAccess(), TestResourceSharedAccess(), and TestResourceWithThreads().

1065 {
1069 
1070  /* Get the thread */
1071  Thread = ExGetCurrentResourceThread();
1072 
1073  /* Sanity check and validation */
1076 
1077  /* Acquire the lock */
1078  ExAcquireResourceLock(Resource, &LockHandle);
1079 
1080  /* See if anyone owns it */
1081 TryAcquire:
1082  if (Resource->ActiveEntries == 0)
1083  {
1084  /* Nobody owns it, so let's take control */
1085  ASSERT(Resource->ActiveEntries == 0);
1086  ASSERT(Resource->ActiveCount == 0);
1087  Resource->ActiveCount = 1;
1088  Resource->ActiveEntries = 1;
1089  Resource->OwnerEntry.OwnerThread = Thread;
1090  Resource->OwnerEntry.OwnerCount = 1;
1091 
1092  /* Release the lock and return */
1093  ExReleaseResourceLock(Resource, &LockHandle);
1094  return TRUE;
1095  }
1096 
1097  /* Check if it's exclusively owned */
1099  {
1100  /* Check if we own it */
1101  if (Resource->OwnerEntry.OwnerThread == Thread)
1102  {
1103  /* Increase the owning count */
1104  Resource->OwnerEntry.OwnerCount++;
1105 
1106  /* Release the lock and return */
1107  ExReleaseResourceLock(Resource, &LockHandle);
1108  return TRUE;
1109  }
1110 
1111  /* Find a free entry */
1112  Owner = ExpFindFreeEntry(Resource, &LockHandle);
1113  if (!Owner) goto TryAcquire;
1114  }
1115  else
1116  {
1117  /* Resource is shared, find who owns it */
1118  Owner = ExpFindEntryForThread(Resource, Thread, &LockHandle, FALSE);
1119  if (!Owner) goto TryAcquire;
1120 
1121  /* Is it us? */
1122  if (Owner->OwnerThread == Thread)
1123  {
1124  /* Increase acquire count and return */
1125  Owner->OwnerCount++;
1126  ASSERT(Owner->OwnerCount != 0);
1127 
1128  /* Release the lock and return */
1129  ExReleaseResourceLock(Resource, &LockHandle);
1130  return TRUE;
1131  }
1132 
1133  /* Acquire it */
1134  Owner->OwnerThread = Thread;
1135  Owner->OwnerCount = 1;
1136 
1137  /* Check how many active entries we had */
1138  if (Resource->ActiveEntries == 0)
1139  {
1140  /* Set initial counts */
1141  ASSERT(Resource->ActiveCount == 0);
1142  Resource->ActiveEntries = 1;
1143  Resource->ActiveCount = 1;
1144  }
1145  else
1146  {
1147  /* Increase active entries */
1148  ASSERT(Resource->ActiveCount == 1);
1149  Resource->ActiveEntries++;
1150  }
1151 
1152  /* Release the lock and return */
1153  ExReleaseResourceLock(Resource, &LockHandle);
1154  return TRUE;
1155  }
1156 
1157  /* If we got here, then we need to wait. Are we allowed? */
1158  if (!Wait)
1159  {
1160  /* Release the lock and return */
1161  ExReleaseResourceLock(Resource, &LockHandle);
1162  return FALSE;
1163  }
1164 
1165  /* Check if we have a shared waiters semaphore */
1166  if (!Resource->SharedWaiters)
1167  {
1168  /* Allocate it and try another acquire */
1170  goto TryAcquire;
1171  }
1172 
1173  /* Now wait for the resource */
1174  Owner->OwnerThread = Thread;
1175  Owner->OwnerCount = 1;
1176  Resource->NumberOfSharedWaiters++;
1177 
1178  /* Release the lock and return */
1179  ExReleaseResourceLock(Resource, &LockHandle);
1180  ExpWaitForResource(Resource, Resource->SharedWaiters);
1181  return TRUE;
1182 }
#define ExGetCurrentResourceThread()
Definition: env_spec_w32.h:633
Definition: extypes.h:210
ULONG_PTR ERESOURCE_THREAD
Definition: extypes.h:208
#define TRUE
Definition: types.h:120
POWNER_ENTRY FASTCALL ExpFindFreeEntry(IN PERESOURCE Resource, IN PKLOCK_QUEUE_HANDLE LockHandle)
Definition: resource.c:416
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel)?(CompletionRoutine!=NULL):TRUE)
POWNER_ENTRY FASTCALL ExpFindEntryForThread(IN PERESOURCE Resource, IN ERESOURCE_THREAD Thread, IN PKLOCK_QUEUE_HANDLE LockHandle, IN BOOLEAN FirstEntryInelligible)
Definition: resource.c:479
BOOLEAN NTAPI KeIsExecutingDpc(VOID)
Definition: dpc.c:946
#define FALSE
Definition: types.h:117
#define ExpVerifyResource(r)
Definition: resource.c:150
_Acquires_shared_lock_ _Curr_ PERESOURCE Resource
Definition: exfuncs.h:440
ULONG OwnerCount
Definition: extypes.h:216
FORCEINLINE VOID ExReleaseResourceLock(IN PERESOURCE Resource, IN PKLOCK_QUEUE_HANDLE LockHandle)
Definition: resource.c:37
_In_opt_ PFILE_OBJECT _In_opt_ PETHREAD Thread
Definition: fltkernel.h:2653
#define IsOwnedExclusive(r)
Definition: resource.c:18
FORCEINLINE VOID ExAcquireResourceLock(IN PERESOURCE Resource, IN PKLOCK_QUEUE_HANDLE LockHandle)
Definition: resource.c:25
VOID NTAPI ExpAllocateSharedWaiterSemaphore(IN PERESOURCE Resource, IN PKLOCK_QUEUE_HANDLE LockHandle)
Definition: resource.c:256
_Out_writes_bytes_to_opt_ AbsoluteSecurityDescriptorSize PSECURITY_DESCRIPTOR _Inout_ PULONG _Out_writes_bytes_to_opt_ DaclSize PACL _Inout_ PULONG _Out_writes_bytes_to_opt_ SaclSize PACL _Inout_ PULONG _Out_writes_bytes_to_opt_ OwnerSize PSID Owner
Definition: rtlfuncs.h:1557
_In_ BOOLEAN Wait
Definition: cctypes.h:23
ERESOURCE_THREAD OwnerThread
Definition: extypes.h:211
*LockHandle LockHandle _Out_ PKLOCK_QUEUE_HANDLE LockHandle
Definition: kefuncs.h:742
VOID FASTCALL ExpWaitForResource(IN PERESOURCE Resource, IN PVOID Object)
Definition: resource.c:618
BOOLEAN NTAPI ExAcquireSharedWaitForExclusive ( IN PERESOURCE  Resource,
IN BOOLEAN  Wait 
)

Definition at line 1217 of file resource.c.

Referenced by __RxAcquireFcb(), ExEnterCriticalRegionAndAcquireSharedWaitForExclusive(), FsRtlAcquireFileForModWriteEx(), TestResourceExclusiveAccess(), TestResourceSharedAccess(), and TestResourceWithThreads().

1219 {
1223 
1224  /* Get the thread */
1225  Thread = ExGetCurrentResourceThread();
1226 
1227  /* Sanity check and validation */
1230 
1231  /* Acquire the lock */
1232  ExAcquireResourceLock(Resource, &LockHandle);
1233 
1234  /* See if nobody owns us */
1235 TryAcquire:
1236  if (!Resource->ActiveEntries)
1237  {
1238  /* Nobody owns it, so let's take control */
1239  ASSERT(Resource->ActiveEntries == 0);
1240  ASSERT(Resource->ActiveCount == 0);
1241  Resource->ActiveCount = 1;
1242  Resource->ActiveEntries = 1;
1243  Resource->OwnerEntry.OwnerThread = Thread;
1244  Resource->OwnerEntry.OwnerCount = 1;
1245 
1246  /* Release the lock and return */
1247  ExReleaseResourceLock(Resource, &LockHandle);
1248  return TRUE;
1249  }
1250 
1251  /* Check if it's exclusively owned */
1253  {
1254  /* Check if we own it */
1255  if (Resource->OwnerEntry.OwnerThread == Thread)
1256  {
1257  /* Increase the owning count */
1258  Resource->OwnerEntry.OwnerCount++;
1259 
1260  /* Release the lock and return */
1261  ExReleaseResourceLock(Resource, &LockHandle);
1262  return TRUE;
1263  }
1264 
1265  /* Find a free entry */
1266  Owner = ExpFindFreeEntry(Resource, &LockHandle);
1267  if (!Owner) goto TryAcquire;
1268  }
1269  else
1270  {
1271  /* Try to find if there are exclusive waiters */
1273  {
1274  /* We have to wait for the exclusive waiter to be done */
1275  if (!Wait)
1276  {
1277  /* So bail out if we're not allowed */
1278  ExReleaseResourceLock(Resource, &LockHandle);
1279  return FALSE;
1280  }
1281 
1282  /* Check if we have a shared waiters semaphore */
1283  if (!Resource->SharedWaiters)
1284  {
1285  /* Allocate one and try again */
1287  goto TryAcquire;
1288  }
1289 
1290  /* Now wait for the resource */
1291  Resource->NumberOfSharedWaiters++;
1292  ExReleaseResourceLock(Resource, &LockHandle);
1293  ExpWaitForResource(Resource, Resource->SharedWaiters);
1294 
1295  /* Get the lock back */
1296  ExAcquireResourceLock(Resource, &LockHandle);
1297 
1298  /* Find who owns it now */
1299  while (!(Owner = ExpFindEntryForThread(Resource, Thread, &LockHandle, TRUE)));
1300 
1301  /* Sanity checks */
1303  ASSERT(Resource->ActiveEntries > 0);
1304  ASSERT(Owner->OwnerThread != Thread);
1305 
1306  /* Take control */
1307  Owner->OwnerThread = Thread;
1308  Owner->OwnerCount = 1;
1309 
1310  /* Release the lock and return */
1311  ExReleaseResourceLock(Resource, &LockHandle);
1312  return TRUE;
1313  }
1314  else
1315  {
1316  /* Resource is shared, find who owns it */
1317  Owner = ExpFindEntryForThread(Resource, Thread, &LockHandle, FALSE);
1318  if (!Owner) goto TryAcquire;
1319 
1320  /* Is it us? */
1321  if (Owner->OwnerThread == Thread)
1322  {
1323  /* Increase acquire count and return */
1324  Owner->OwnerCount++;
1325  ASSERT(Owner->OwnerCount != 0);
1326 
1327  /* Release the lock and return */
1328  ExReleaseResourceLock(Resource, &LockHandle);
1329  return TRUE;
1330  }
1331 
1332  /* No exclusive waiters, so acquire it */
1333  Owner->OwnerThread = Thread;
1334  Owner->OwnerCount = 1;
1335 
1336  /* Check how many active entries we had */
1337  if (Resource->ActiveEntries == 0)
1338  {
1339  /* Set initial counts */
1340  ASSERT(Resource->ActiveCount == 0);
1341  Resource->ActiveEntries = 1;
1342  Resource->ActiveCount = 1;
1343  }
1344  else
1345  {
1346  /* Increase active entries */
1347  ASSERT(Resource->ActiveCount == 1);
1348  Resource->ActiveEntries++;
1349  }
1350 
1351  /* Release the lock and return */
1352  ExReleaseResourceLock(Resource, &LockHandle);
1353  return TRUE;
1354  }
1355  }
1356 
1357  /* We have to wait for the exclusive waiter to be done */
1358  if (!Wait)
1359  {
1360  /* So bail out if we're not allowed */
1361  ExReleaseResourceLock(Resource, &LockHandle);
1362  return FALSE;
1363  }
1364 
1365  /* Check if we have a shared waiters semaphore */
1366  if (!Resource->SharedWaiters)
1367  {
1368  /* Allocate one and try again */
1370  goto TryAcquire;
1371  }
1372 
1373  /* Take control */
1374  Owner->OwnerThread = Thread;
1375  Owner->OwnerCount = 1;
1376  Resource->NumberOfSharedWaiters++;
1377 
1378  /* Release the lock and return */
1379  ExReleaseResourceLock(Resource, &LockHandle);
1380  ExpWaitForResource(Resource, Resource->SharedWaiters);
1381  return TRUE;
1382 }
#define ExGetCurrentResourceThread()
Definition: env_spec_w32.h:633
Definition: extypes.h:210
ULONG_PTR ERESOURCE_THREAD
Definition: extypes.h:208
#define IsExclusiveWaiting(r)
Definition: resource.c:16
#define TRUE
Definition: types.h:120
POWNER_ENTRY FASTCALL ExpFindFreeEntry(IN PERESOURCE Resource, IN PKLOCK_QUEUE_HANDLE LockHandle)
Definition: resource.c:416
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel)?(CompletionRoutine!=NULL):TRUE)
POWNER_ENTRY FASTCALL ExpFindEntryForThread(IN PERESOURCE Resource, IN ERESOURCE_THREAD Thread, IN PKLOCK_QUEUE_HANDLE LockHandle, IN BOOLEAN FirstEntryInelligible)
Definition: resource.c:479
BOOLEAN NTAPI KeIsExecutingDpc(VOID)
Definition: dpc.c:946
#define FALSE
Definition: types.h:117
#define ExpVerifyResource(r)
Definition: resource.c:150
_Acquires_shared_lock_ _Curr_ PERESOURCE Resource
Definition: exfuncs.h:440
ULONG OwnerCount
Definition: extypes.h:216
FORCEINLINE VOID ExReleaseResourceLock(IN PERESOURCE Resource, IN PKLOCK_QUEUE_HANDLE LockHandle)
Definition: resource.c:37
_In_opt_ PFILE_OBJECT _In_opt_ PETHREAD Thread
Definition: fltkernel.h:2653
#define IsOwnedExclusive(r)
Definition: resource.c:18
FORCEINLINE VOID ExAcquireResourceLock(IN PERESOURCE Resource, IN PKLOCK_QUEUE_HANDLE LockHandle)
Definition: resource.c:25
VOID NTAPI ExpAllocateSharedWaiterSemaphore(IN PERESOURCE Resource, IN PKLOCK_QUEUE_HANDLE LockHandle)
Definition: resource.c:256
_Out_writes_bytes_to_opt_ AbsoluteSecurityDescriptorSize PSECURITY_DESCRIPTOR _Inout_ PULONG _Out_writes_bytes_to_opt_ DaclSize PACL _Inout_ PULONG _Out_writes_bytes_to_opt_ SaclSize PACL _Inout_ PULONG _Out_writes_bytes_to_opt_ OwnerSize PSID Owner
Definition: rtlfuncs.h:1557
_In_ BOOLEAN Wait
Definition: cctypes.h:23
ERESOURCE_THREAD OwnerThread
Definition: extypes.h:211
*LockHandle LockHandle _Out_ PKLOCK_QUEUE_HANDLE LockHandle
Definition: kefuncs.h:742
VOID FASTCALL ExpWaitForResource(IN PERESOURCE Resource, IN PVOID Object)
Definition: resource.c:618
VOID NTAPI ExConvertExclusiveToSharedLite ( IN PERESOURCE  Resource)

Definition at line 1402 of file resource.c.

Referenced by FsRtlAcquireFileForModWriteEx(), RxExclusivePrefixTableLockToShared(), TestResourceExclusiveAccess(), and TestResourceSharedAccess().

1403 {
1404  ULONG OldWaiters;
1406 
1407  /* Sanity checks */
1411  ASSERT(Resource->OwnerEntry.OwnerThread == (ERESOURCE_THREAD)PsGetCurrentThread());
1412 
1413  /* Lock the resource */
1414  ExAcquireResourceLock(Resource, &LockHandle);
1415 
1416  /* Erase the exclusive flag */
1417  Resource->Flag &= ~ResourceOwnedExclusive;
1418 
1419  /* Check if we have shared waiters */
1421  {
1422  /* Make the waiters active owners */
1423  OldWaiters = Resource->NumberOfSharedWaiters;
1424  Resource->ActiveEntries += OldWaiters;
1425  Resource->NumberOfSharedWaiters = 0;
1426 
1427  /* Release lock and wake the waiters */
1428  ExReleaseResourceLock(Resource, &LockHandle);
1429  KeReleaseSemaphore(Resource->SharedWaiters, 0, OldWaiters, FALSE);
1430  }
1431  else
1432  {
1433  /* Release lock */
1434  ExReleaseResourceLock(Resource, &LockHandle);
1435  }
1436 }
ULONG_PTR ERESOURCE_THREAD
Definition: extypes.h:208
#define PsGetCurrentThread()
Definition: env_spec_w32.h:81
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel)?(CompletionRoutine!=NULL):TRUE)
BOOLEAN NTAPI KeIsExecutingDpc(VOID)
Definition: dpc.c:946
#define FALSE
Definition: types.h:117
#define ExpVerifyResource(r)
Definition: resource.c:150
_Acquires_shared_lock_ _Curr_ PERESOURCE Resource
Definition: exfuncs.h:440
FORCEINLINE VOID ExReleaseResourceLock(IN PERESOURCE Resource, IN PKLOCK_QUEUE_HANDLE LockHandle)
Definition: resource.c:37
#define IsOwnedExclusive(r)
Definition: resource.c:18
FORCEINLINE VOID ExAcquireResourceLock(IN PERESOURCE Resource, IN PKLOCK_QUEUE_HANDLE LockHandle)
Definition: resource.c:25
LONG NTAPI KeReleaseSemaphore(IN PKSEMAPHORE Semaphore, IN KPRIORITY Increment, IN LONG Adjustment, IN BOOLEAN Wait)
Definition: semphobj.c:54
unsigned int ULONG
Definition: retypes.h:1
*LockHandle LockHandle _Out_ PKLOCK_QUEUE_HANDLE LockHandle
Definition: kefuncs.h:742
#define IsSharedWaiting(r)
Definition: resource.c:17
#define ResourceOwnedExclusive
Definition: dldetect.h:32
NTSTATUS NTAPI ExDeleteResourceLite ( IN PERESOURCE  Resource)

Definition at line 1456 of file resource.c.

Referenced by _free_fcb(), _free_fileref(), CcUnpinDataForThread(), CcUnpinRepinnedBcb(), CdDeleteFcbNonpaged(), CdDeleteVcb(), CdfsDestroyFCB(), CdInitializeGlobalData(), CdUnload(), CleanupUserImpl(), CmpDestroyHive(), CmpInitializeHive(), CmUnloadKey(), do_write(), DriverEntry(), DriverUnload(), drop_chunk(), EngDeleteSemaphore(), Ext2DestroyVcb(), Ext2FreeFcb(), Ext2InitializeVcb(), FFSFreeFcb(), FFSFreeVcb(), FFSInitializeVcb(), FltRegisterFilter(), FltUnregisterFilter(), mount_vol(), MupFreeMasterQueryContext(), MupUninitializeData(), NpDeleteCcb(), NtfsDestroyFCB(), ObpDeleteObjectType(), RfsdFreeFcb(), RfsdFreeVcb(), RfsdInitializeVcb(), RtlDeleteHeapLock(), RxFinalizeFcbTable(), RxFinalizeNetFcb(), START_TEST(), UDFVInit(), UDFVRelease(), uninit(), vfatDestroyFCB(), VfatDismountVolume(), WCacheInit__(), and WCacheRelease__().

1457 {
1459 
1460  /* Sanity checks */
1465 
1466  /* Lock the resource */
1468 
1469  /* Remove the resource */
1470  RemoveEntryList(&Resource->SystemResourcesList);
1471 
1472  /* Release the lock */
1473  KeReleaseInStackQueuedSpinLock(&LockHandle);
1474 
1475  /* Free every structure */
1476  if (Resource->OwnerTable) ExFreePoolWithTag(Resource->OwnerTable, TAG_RESOURCE_TABLE);
1477  if (Resource->SharedWaiters) ExFreePoolWithTag(Resource->SharedWaiters, TAG_RESOURCE_SEMAPHORE);
1478  if (Resource->ExclusiveWaiters) ExFreePoolWithTag(Resource->ExclusiveWaiters, TAG_RESOURCE_EVENT);
1479 
1480  /* Return success */
1481  return STATUS_SUCCESS;
1482 }
#define STATUS_SUCCESS
Definition: contextmenu.cpp:55
#define TAG_RESOURCE_SEMAPHORE
Definition: tag.h:20
#define IsExclusiveWaiting(r)
Definition: resource.c:16
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel)?(CompletionRoutine!=NULL):TRUE)
VOID FASTCALL KeAcquireInStackQueuedSpinLock(IN PKSPIN_LOCK SpinLock, IN PKLOCK_QUEUE_HANDLE LockHandle)
Definition: spinlock.c:130
BOOLEAN NTAPI KeIsExecutingDpc(VOID)
Definition: dpc.c:946
FORCEINLINE BOOLEAN RemoveEntryList(_In_ PLIST_ENTRY Entry)
Definition: rtlfuncs.h:105
#define FALSE
Definition: types.h:117
#define ExpVerifyResource(r)
Definition: resource.c:150
_Acquires_shared_lock_ _Curr_ PERESOURCE Resource
Definition: exfuncs.h:440
KSPIN_LOCK ExpResourceSpinLock
Definition: resource.c:75
VOID FASTCALL KeReleaseInStackQueuedSpinLock(IN PKLOCK_QUEUE_HANDLE LockHandle)
Definition: spinlock.c:166
#define TAG_RESOURCE_EVENT
Definition: tag.h:19
*LockHandle LockHandle _Out_ PKLOCK_QUEUE_HANDLE LockHandle
Definition: kefuncs.h:742
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1097
#define IsSharedWaiting(r)
Definition: resource.c:17
#define TAG_RESOURCE_TABLE
Definition: tag.h:18
VOID NTAPI ExDisableResourceBoostLite ( IN PERESOURCE  Resource)

Definition at line 1501 of file resource.c.

1502 {
1504 
1505  /* Sanity check */
1507 
1508  /* Lock the resource */
1509  ExAcquireResourceLock(Resource, &LockHandle);
1510 
1511  /* Remove the flag */
1513 
1514  /* Release the lock */
1515  ExReleaseResourceLock(Resource, &LockHandle);
1516 }
#define ResourceHasDisabledPriorityBoost
Definition: extypes.h:169
#define ExpVerifyResource(r)
Definition: resource.c:150
_Acquires_shared_lock_ _Curr_ PERESOURCE Resource
Definition: exfuncs.h:440
FORCEINLINE VOID ExReleaseResourceLock(IN PERESOURCE Resource, IN PKLOCK_QUEUE_HANDLE LockHandle)
Definition: resource.c:37
FORCEINLINE VOID ExAcquireResourceLock(IN PERESOURCE Resource, IN PKLOCK_QUEUE_HANDLE LockHandle)
Definition: resource.c:25
*LockHandle LockHandle _Out_ PKLOCK_QUEUE_HANDLE LockHandle
Definition: kefuncs.h:742
PVOID NTAPI ExEnterCriticalRegionAndAcquireResourceExclusive ( IN PERESOURCE  Resource)

Definition at line 2189 of file resource.c.

Referenced by _Acquires_lock_(), and NtAdjustPrivilegesToken().

2190 {
2191  /* Enter critical region */
2193 
2194  /* Acquire the resource */
2196 
2197  /* Return the Win32 Thread */
2198  return KeGetCurrentThread()->Win32Thread;
2199 }
#define TRUE
Definition: types.h:120
BOOLEAN NTAPI ExAcquireResourceExclusiveLite(IN PERESOURCE Resource, IN BOOLEAN Wait)
Definition: resource.c:770
_Acquires_shared_lock_ _Curr_ PERESOURCE Resource
Definition: exfuncs.h:440
#define KeEnterCriticalRegion()
Definition: ke_x.h:83
#define KeGetCurrentThread
Definition: hal.h:44
PVOID NTAPI ExEnterCriticalRegionAndAcquireResourceShared ( IN PERESOURCE  Resource)

Definition at line 2218 of file resource.c.

2219 {
2220  /* Enter critical region */
2222 
2223  /* Acquire the resource */
2225 
2226  /* Return the Win32 Thread */
2227  return KeGetCurrentThread()->Win32Thread;
2228 }
#define TRUE
Definition: types.h:120
_Acquires_shared_lock_ _Curr_ PERESOURCE Resource
Definition: exfuncs.h:440
#define KeEnterCriticalRegion()
Definition: ke_x.h:83
BOOLEAN NTAPI ExAcquireResourceSharedLite(IN PERESOURCE Resource, IN BOOLEAN Wait)
Definition: resource.c:885
#define KeGetCurrentThread
Definition: hal.h:44
PVOID NTAPI ExEnterCriticalRegionAndAcquireSharedWaitForExclusive ( IN PERESOURCE  Resource)

Definition at line 2249 of file resource.c.

2250 {
2251  /* Enter critical region */
2253 
2254  /* Acquire the resource */
2256 
2257  /* Return the Win32 Thread */
2258  return KeGetCurrentThread()->Win32Thread;
2259 }
BOOLEAN NTAPI ExAcquireSharedWaitForExclusive(IN PERESOURCE Resource, IN BOOLEAN Wait)
Definition: resource.c:1217
#define TRUE
Definition: types.h:120
_Acquires_shared_lock_ _Curr_ PERESOURCE Resource
Definition: exfuncs.h:440
#define KeEnterCriticalRegion()
Definition: ke_x.h:83
#define KeGetCurrentThread
Definition: hal.h:44
ULONG NTAPI ExGetExclusiveWaiterCount ( IN PERESOURCE  Resource)

Definition at line 1535 of file resource.c.

Referenced by FatAcquireExclusiveFcb(), FatAcquireSharedFcb(), and FatPendingClose().

1536 {
1537  /* Return the count */
1538  return Resource->NumberOfExclusiveWaiters;
1539 }
_Acquires_shared_lock_ _Curr_ PERESOURCE Resource
Definition: exfuncs.h:440
ULONG NTAPI ExGetSharedWaiterCount ( IN PERESOURCE  Resource)

Definition at line 1558 of file resource.c.

Referenced by FatAcquireExclusiveFcb(), FatAcquireSharedFcb(), and FatPendingClose().

1559 {
1560  /* Return the count */
1561  return Resource->NumberOfSharedWaiters;
1562 }
_Acquires_shared_lock_ _Curr_ PERESOURCE Resource
Definition: exfuncs.h:440
NTSTATUS NTAPI ExInitializeResourceLite ( IN PERESOURCE  Resource)

Definition at line 1582 of file resource.c.

1583 {
1585 
1586  /* Clear the structure */
1587  RtlZeroMemory(Resource, sizeof(ERESOURCE));
1588 
1589  /* Initialize the lock */
1590  KeInitializeSpinLock(&Resource->SpinLock);
1591 
1592  /* Add it into the system list */
1594  InsertTailList(&ExpSystemResourcesList, &Resource->SystemResourcesList);
1595  KeReleaseInStackQueuedSpinLock(&LockHandle);
1596 
1597  /* Return success */
1598  return STATUS_SUCCESS;
1599 }
#define STATUS_SUCCESS
Definition: contextmenu.cpp:55
#define InsertTailList(ListHead, Entry)
VOID FASTCALL KeAcquireInStackQueuedSpinLock(IN PKSPIN_LOCK SpinLock, IN PKLOCK_QUEUE_HANDLE LockHandle)
Definition: spinlock.c:130
_Acquires_shared_lock_ _Curr_ PERESOURCE Resource
Definition: exfuncs.h:440
KSPIN_LOCK ExpResourceSpinLock
Definition: resource.c:75
FORCEINLINE VOID KeInitializeSpinLock(_Out_ PKSPIN_LOCK SpinLock)
Definition: kefuncs.h:251
VOID FASTCALL KeReleaseInStackQueuedSpinLock(IN PKLOCK_QUEUE_HANDLE LockHandle)
Definition: spinlock.c:166
LIST_ENTRY ExpSystemResourcesList
Definition: resource.c:76
ULONG ERESOURCE
Definition: env_spec_w32.h:594
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
*LockHandle LockHandle _Out_ PKLOCK_QUEUE_HANDLE LockHandle
Definition: kefuncs.h:742
BOOLEAN NTAPI ExIsResourceAcquiredExclusiveLite ( IN PERESOURCE  Resource)

Definition at line 1619 of file resource.c.

Referenced by CcSetBcbOwnerPointer(), CmpLockHiveFlusherExclusive(), CmpLockHiveFlusherShared(), CmpTestHiveFlusherLockExclusive(), CmpTestRegistryLockExclusive(), drv_create(), EngIsSemaphoreOwnedByCurrentThread(), Ext2AllocateFcb(), Ext2InsertFcb(), IopGetDriverObject(), IopLoadServiceModule(), open_fileref(), UDFAcquireResourceExclusiveWithCheck(), UDFAcquireResourceSharedWithCheck(), UDFCommonFlush(), UDFFastIoAcqCreateSec(), UDFIsResourceAcquired(), UserIsEntered(), UserIsEnteredExclusive(), and write_file2().

1620 {
1621  BOOLEAN IsAcquired = FALSE;
1622 
1623  /* Sanity check */
1625 
1626  /* Check if it's exclusively acquired */
1627  if ((IsOwnedExclusive(Resource)) &&
1628  (Resource->OwnerEntry.OwnerThread == ExGetCurrentResourceThread()))
1629  {
1630  /* It is acquired */
1631  IsAcquired = TRUE;
1632  }
1633 
1634  /* Return if it's acquired */
1635  return IsAcquired;
1636 }
#define ExGetCurrentResourceThread()
Definition: env_spec_w32.h:633
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define ExpVerifyResource(r)
Definition: resource.c:150
_Acquires_shared_lock_ _Curr_ PERESOURCE Resource
Definition: exfuncs.h:440
unsigned char BOOLEAN
#define IsOwnedExclusive(r)
Definition: resource.c:18
ULONG NTAPI ExIsResourceAcquiredSharedLite ( IN PERESOURCE  Resource)

Definition at line 1658 of file resource.c.

Referenced by CcSetBcbOwnerPointer(), CmpTestHiveFlusherLockShared(), CmpTestRegistryLock(), do_read_job(), drv_cleanup(), drv_read(), FsRtlAcquireFileForCcFlushEx(), UDFAcquireResourceExclusiveWithCheck(), UDFAcquireResourceSharedWithCheck(), UDFCommonFlush(), UDFIsResourceAcquired(), and UserIsEntered().

1659 {
1661  ULONG i, Size;
1662  ULONG Count = 0;
1665 
1666  /* Sanity check */
1668 
1669  /* Check if nobody owns us */
1670  if (!Resource->ActiveEntries) return 0;
1671 
1672  /* Get the thread */
1673  Thread = ExGetCurrentResourceThread();
1674 
1675  /* Check if we are in the thread list */
1676  if (Resource->OwnerEntry.OwnerThread == Thread)
1677  {
1678  /* Found it, return count */
1679  Count = Resource->OwnerEntry.OwnerCount;
1680  }
1681  else
1682  {
1683  /* We can't own an exclusive resource at this point */
1684  if (IsOwnedExclusive(Resource)) return 0;
1685 
1686  /* Lock the resource */
1687  ExAcquireResourceLock(Resource, &LockHandle);
1688 
1689  /* Not in the list, do a full table look up */
1690  Owner = Resource->OwnerTable;
1691  if (Owner)
1692  {
1693  /* Get the resource index */
1694  i = ((PKTHREAD)Thread)->ResourceIndex;
1695  Size = Owner->TableSize;
1696 
1697  /* Check if the index is valid and check if we don't match */
1698  if ((i >= Size) || (Owner[i].OwnerThread != Thread))
1699  {
1700  /* Sh*t! We need to do a full search */
1701  for (i = 1; i < Size; i++)
1702  {
1703  /* Move to next owner */
1704  Owner++;
1705 
1706  /* Try to find a match */
1707  if (Owner->OwnerThread == Thread)
1708  {
1709  /* Finally! */
1710  Count = Owner->OwnerCount;
1711  break;
1712  }
1713  }
1714  }
1715  else
1716  {
1717  /* We found the match directlry */
1718  Count = Owner[i].OwnerCount;
1719  }
1720  }
1721 
1722  /* Release the lock */
1723  ExReleaseResourceLock(Resource, &LockHandle);
1724  }
1725 
1726  /* Return count */
1727  return Count;
1728 }
#define ExGetCurrentResourceThread()
Definition: env_spec_w32.h:633
Definition: extypes.h:210
ULONG_PTR ERESOURCE_THREAD
Definition: extypes.h:208
_Inout_ __drv_aliasesMem PSLIST_ENTRY _Inout_ PSLIST_ENTRY _In_ ULONG Count
Definition: exfuncs.h:1015
struct _KTHREAD * PKTHREAD
Definition: nt_native.h:28
GLenum GLclampf GLint i
Definition: glfuncs.h:14
#define ExpVerifyResource(r)
Definition: resource.c:150
_Acquires_shared_lock_ _Curr_ PERESOURCE Resource
Definition: exfuncs.h:440
UINTN Size
Definition: acefiex.h:550
ULONG TableSize
Definition: extypes.h:218
ULONG OwnerCount
Definition: extypes.h:216
FORCEINLINE VOID ExReleaseResourceLock(IN PERESOURCE Resource, IN PKLOCK_QUEUE_HANDLE LockHandle)
Definition: resource.c:37
_In_opt_ PFILE_OBJECT _In_opt_ PETHREAD Thread
Definition: fltkernel.h:2653
#define IsOwnedExclusive(r)
Definition: resource.c:18
FORCEINLINE VOID ExAcquireResourceLock(IN PERESOURCE Resource, IN PKLOCK_QUEUE_HANDLE LockHandle)
Definition: resource.c:25
_Out_writes_bytes_to_opt_ AbsoluteSecurityDescriptorSize PSECURITY_DESCRIPTOR _Inout_ PULONG _Out_writes_bytes_to_opt_ DaclSize PACL _Inout_ PULONG _Out_writes_bytes_to_opt_ SaclSize PACL _Inout_ PULONG _Out_writes_bytes_to_opt_ OwnerSize PSID Owner
Definition: rtlfuncs.h:1557
ERESOURCE_THREAD OwnerThread
Definition: extypes.h:211
unsigned int ULONG
Definition: retypes.h:1
*LockHandle LockHandle _Out_ PKLOCK_QUEUE_HANDLE LockHandle
Definition: kefuncs.h:742
VOID NTAPI ExpAllocateExclusiveWaiterEvent ( IN PERESOURCE  Resource,
IN PKLOCK_QUEUE_HANDLE  LockHandle 
)

Definition at line 196 of file resource.c.

Referenced by ExAcquireResourceExclusiveLite().

198 {
199  PKEVENT Event;
200 
201  /* Release the lock */
203 
204  /* Loop as long as we keep running out of memory */
205  do
206  {
207  /* Allocate the event */
209  sizeof(KEVENT),
211  if (Event)
212  {
213  /* Initialize it */
215 
216  /* Set it */
217  if (InterlockedCompareExchangePointer((PVOID*)&Resource->ExclusiveWaiters,
218  Event,
219  NULL))
220  {
221  /* Someone already set it, free our event */
222  DPRINT1("WARNING: Handling race condition\n");
224  }
225 
226  break;
227  }
228 
229  /* Wait a bit before trying again */
231  } while (TRUE);
232 
233  /* Re-acquire the lock */
235 }
DWORD *typedef PVOID
Definition: winlogon.h:52
LARGE_INTEGER ExShortTime
Definition: resource.c:72
#define TRUE
Definition: types.h:120
ACPI_EFI_EVENT Event
Definition: acefiex.h:607
#define FALSE
Definition: types.h:117
#define InterlockedCompareExchangePointer
Definition: interlocked.h:129
_Acquires_shared_lock_ _Curr_ PERESOURCE Resource
Definition: exfuncs.h:440
NTSTATUS NTAPI KeDelayExecutionThread(IN KPROCESSOR_MODE WaitMode, IN BOOLEAN Alertable, IN PLARGE_INTEGER Interval OPTIONAL)
Definition: wait.c:283
smooth NULL
Definition: ftsmooth.c:513
FORCEINLINE VOID ExReleaseResourceLock(IN PERESOURCE Resource, IN PKLOCK_QUEUE_HANDLE LockHandle)
Definition: resource.c:37
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
#define TAG_RESOURCE_EVENT
Definition: tag.h:19
FORCEINLINE VOID ExAcquireResourceLock(IN PERESOURCE Resource, IN PKLOCK_QUEUE_HANDLE LockHandle)
Definition: resource.c:25
#define KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
#define DPRINT1
Definition: precomp.h:8
*LockHandle LockHandle _Out_ PKLOCK_QUEUE_HANDLE LockHandle
Definition: kefuncs.h:742
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1097
VOID NTAPI ExpAllocateSharedWaiterSemaphore ( IN PERESOURCE  Resource,
IN PKLOCK_QUEUE_HANDLE  LockHandle 
)

Definition at line 256 of file resource.c.

Referenced by ExAcquireResourceSharedLite(), ExAcquireSharedStarveExclusive(), and ExAcquireSharedWaitForExclusive().

258 {
259  PKSEMAPHORE Semaphore;
260 
261  /* Release the lock */
263 
264  /* Loop as long as we keep running out of memory */
265  do
266  {
267  /* Allocate the semaphore */
269  sizeof(KSEMAPHORE),
271  if (Semaphore)
272  {
273  /* Initialize it */
274  KeInitializeSemaphore(Semaphore, 0, MAXLONG);
275 
276  /* Set it */
277  if (InterlockedCompareExchangePointer((PVOID*)&Resource->SharedWaiters,
278  Semaphore,
279  NULL))
280  {
281  /* Someone already set it, free our semaphore */
282  DPRINT1("WARNING: Handling race condition\n");
284  }
285 
286  break;
287  }
288 
289  /* Wait a bit before trying again */
291  } while (TRUE);
292 
293  /* Re-acquire the lock */
295 }
DWORD *typedef PVOID
Definition: winlogon.h:52
#define TAG_RESOURCE_SEMAPHORE
Definition: tag.h:20
LARGE_INTEGER ExShortTime
Definition: resource.c:72
#define TRUE
Definition: types.h:120
#define MAXLONG
Definition: umtypes.h:104
#define FALSE
Definition: types.h:117
#define InterlockedCompareExchangePointer
Definition: interlocked.h:129
_Acquires_shared_lock_ _Curr_ PERESOURCE Resource
Definition: exfuncs.h:440
NTSTATUS NTAPI KeDelayExecutionThread(IN KPROCESSOR_MODE WaitMode, IN BOOLEAN Alertable, IN PLARGE_INTEGER Interval OPTIONAL)
Definition: wait.c:283
smooth NULL
Definition: ftsmooth.c:513
VOID NTAPI KeInitializeSemaphore(IN PKSEMAPHORE Semaphore, IN LONG Count, IN LONG Limit)
Definition: semphobj.c:22
FORCEINLINE VOID ExReleaseResourceLock(IN PERESOURCE Resource, IN PKLOCK_QUEUE_HANDLE LockHandle)
Definition: resource.c:37
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
FORCEINLINE VOID ExAcquireResourceLock(IN PERESOURCE Resource, IN PKLOCK_QUEUE_HANDLE LockHandle)
Definition: resource.c:25
#define DPRINT1
Definition: precomp.h:8
*LockHandle LockHandle _Out_ PKLOCK_QUEUE_HANDLE LockHandle
Definition: kefuncs.h:742
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1097
VOID FASTCALL ExpBoostOwnerThread ( IN PKTHREAD  Thread,
IN PKTHREAD  OwnerThread 
)

Definition at line 572 of file resource.c.

Referenced by ExpWaitForResource().

574 {
575  /* Make sure the owner thread is a pointer, not an ID */
576  if (!((ULONG_PTR)OwnerThread & 0x3))
577  {
578  /* Check if we can actually boost it */
579  if ((OwnerThread->Priority < Thread->Priority) &&
580  (OwnerThread->Priority < 14))
581  {
582  /* Acquire the thread lock */
584 
585  /* Set the new priority */
586  OwnerThread->PriorityDecrement += 14 - OwnerThread->Priority;
587 
588  /* Update quantum */
589  OwnerThread->Quantum = OwnerThread->QuantumReset;
590 
591  /* Update the kernel state */
592  KiSetPriorityThread(OwnerThread, 14);
593 
594  /* Release the thread lock */
596  }
597  }
598 }
FORCEINLINE VOID KiReleaseThreadLock(IN PKTHREAD Thread)
Definition: ke_x.h:244
uint32_t ULONG_PTR
Definition: typedefs.h:64
VOID FASTCALL KiSetPriorityThread(IN PKTHREAD Thread, IN KPRIORITY Priority)
Definition: thrdschd.c:511
FORCEINLINE VOID KiAcquireThreadLock(IN PKTHREAD Thread)
Definition: ke_x.h:234
_In_opt_ PFILE_OBJECT _In_opt_ PETHREAD Thread
Definition: fltkernel.h:2653
VOID NTAPI ExpExpandResourceOwnerTable ( IN PERESOURCE  Resource,
IN PKLOCK_QUEUE_HANDLE  LockHandle 
)

Definition at line 316 of file resource.c.

Referenced by ExpFindEntryForThread(), and ExpFindFreeEntry().

318 {
320  KIRQL OldIrql;
321  ULONG NewSize, OldSize;
322 
323  /* Get the owner table */
324  Owner = Resource->OwnerTable;
325  if (!Owner)
326  {
327  /* Start with the default size of 3 */
328  OldSize = 0;
329  NewSize = 3;
330  }
331  else
332  {
333  /* Add 4 more entries */
334  OldSize = Owner->TableSize;
335  NewSize = OldSize + 4;
336  }
337 
338  /* Release the lock */
340 
341  /* Allocate memory for the table */
343  NewSize * sizeof(OWNER_ENTRY),
345 
346  /* Zero the table */
347  RtlZeroMemory(Table + OldSize,
348  (NewSize - OldSize) * sizeof(OWNER_ENTRY));
349 
350  /* Lock the resource */
352 
353  /* Make sure nothing has changed */
354  if ((Owner != Resource->OwnerTable) ||
355  ((Owner) && (OldSize != Owner->TableSize)))
356  {
357  /* Resource changed while we weren't holding the lock; bail out */
360  }
361  else
362  {
363  /* Copy the table */
364  if (Owner) RtlCopyMemory(Table, Owner, OldSize * sizeof(OWNER_ENTRY));
365 
366  /* Acquire dispatcher lock to prevent thread boosting */
367  OldIrql = KiAcquireDispatcherLock();
368 
369  /* Set the new table data */
370  Table->TableSize = NewSize;
371  Resource->OwnerTable = Table;
372 
373  /* Release dispatcher lock */
374  KiReleaseDispatcherLock(OldIrql);
375 
376  /* Sanity check */
378 
379  /* Release lock */
381 
382  /* Free the old table */
383  if (Owner) ExFreePoolWithTag(Owner, TAG_RESOURCE_TABLE);
384 
385  /* Set the resource index */
386  if (!OldSize) OldSize = 1;
387  }
388 
389  /* Set the resource index */
390  KeGetCurrentThread()->ResourceIndex = (UCHAR)OldSize;
391 
392  /* Lock the resource again */
394 }
Definition: extypes.h:210
NTSYSAPI VOID NTAPI RtlCopyMemory(VOID UNALIGNED *Destination, CONST VOID UNALIGNED *Source, ULONG Length)
UCHAR KIRQL
Definition: env_spec_w32.h:591
#define ExpVerifyResource(r)
Definition: resource.c:150
_Acquires_shared_lock_ _Curr_ PERESOURCE Resource
Definition: exfuncs.h:440
VOID * Table
Definition: acefiex.h:630
ULONG TableSize
Definition: extypes.h:218
FORCEINLINE VOID ExReleaseResourceLock(IN PERESOURCE Resource, IN PKLOCK_QUEUE_HANDLE LockHandle)
Definition: resource.c:37
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
unsigned char UCHAR
Definition: xmlstorage.h:181
_Requires_lock_held_ Interrupt _Releases_lock_ Interrupt _In_ _IRQL_restores_ KIRQL OldIrql
Definition: kefuncs.h:803
FORCEINLINE VOID ExAcquireResourceLock(IN PERESOURCE Resource, IN PKLOCK_QUEUE_HANDLE LockHandle)
Definition: resource.c:25
_Must_inspect_result_ _In_ USHORT NewSize
Definition: fltkernel.h:975
FORCEINLINE VOID KiReleaseDispatcherLock(IN KIRQL OldIrql)
Definition: ke_x.h:152
FORCEINLINE KIRQL KiAcquireDispatcherLock(VOID)
Definition: ke_x.h:144
_Out_writes_bytes_to_opt_ AbsoluteSecurityDescriptorSize PSECURITY_DESCRIPTOR _Inout_ PULONG _Out_writes_bytes_to_opt_ DaclSize PACL _Inout_ PULONG _Out_writes_bytes_to_opt_ SaclSize PACL _Inout_ PULONG _Out_writes_bytes_to_opt_ OwnerSize PSID Owner
Definition: rtlfuncs.h:1557
unsigned int ULONG
Definition: retypes.h:1
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
*LockHandle LockHandle _Out_ PKLOCK_QUEUE_HANDLE LockHandle
Definition: kefuncs.h:742
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1097
#define KeGetCurrentThread
Definition: hal.h:44
#define TAG_RESOURCE_TABLE
Definition: tag.h:18
POWNER_ENTRY FASTCALL ExpFindEntryForThread ( IN PERESOURCE  Resource,
IN ERESOURCE_THREAD  Thread,
IN PKLOCK_QUEUE_HANDLE  LockHandle,
IN BOOLEAN  FirstEntryInelligible 
)

Definition at line 479 of file resource.c.

Referenced by ExAcquireResourceSharedLite(), ExAcquireSharedStarveExclusive(), ExAcquireSharedWaitForExclusive(), and ExSetResourceOwnerPointer().

483 {
484  POWNER_ENTRY FreeEntry, Owner, Limit;
485 
486  /* Start by looking in the static array */
487  Owner = &Resource->OwnerEntry;
488  if (Owner->OwnerThread == Thread) return Owner;
489 
490  /* Check if this is a free entry */
491  if ((FirstEntryInelligible) || (Owner->OwnerThread))
492  {
493  /* No free entry */
494  FreeEntry = NULL;
495  }
496  else
497  {
498  /* Use the first entry as our free entry */
499  FreeEntry = Owner;
500  }
501 
502  /* Get the current table pointer */
503  Owner = Resource->OwnerTable;
504  if (Owner)
505  {
506  /* Set the limit, move to the next owner and loop owner entries */
507  Limit = &Owner[Owner->TableSize];
508  Owner++;
509  while (Owner->OwnerThread != Thread)
510  {
511  /* Check if we don't have a free entry */
512  if (!FreeEntry)
513  {
514  /* Check if this entry is free */
515  if (!Owner->OwnerThread)
516  {
517  /* Save it as our free entry */
518  FreeEntry = Owner;
519  }
520  }
521 
522  /* Move to the next one */
523  Owner++;
524 
525  /* Check if the entry is free */
526  if (Owner == Limit) goto Expand;
527  }
528 
529  /* Update the resource entry */
530  KeGetCurrentThread()->ResourceIndex = (UCHAR)(Owner - Resource->OwnerTable);
531  return Owner;
532  }
533  else
534  {
535 Expand:
536  /* Check if it's OK to do an expansion */
537  if (!LockHandle) return NULL;
538 
539  /* If we found a free entry by now, return it */
540  if (FreeEntry)
541  {
542  /* Set the resource index */
543  KeGetCurrentThread()->ResourceIndex = (UCHAR)(FreeEntry - Resource->OwnerTable);
544  return FreeEntry;
545  }
546 
547  /* No free entry, expand the table */
549  return NULL;
550  }
551 }
Definition: extypes.h:210
_Acquires_shared_lock_ _Curr_ PERESOURCE Resource
Definition: exfuncs.h:440
smooth NULL
Definition: ftsmooth.c:513
ULONG TableSize
Definition: extypes.h:218
_In_opt_ PFILE_OBJECT _In_opt_ PETHREAD Thread
Definition: fltkernel.h:2653
unsigned char UCHAR
Definition: xmlstorage.h:181
_Out_writes_bytes_to_opt_ AbsoluteSecurityDescriptorSize PSECURITY_DESCRIPTOR _Inout_ PULONG _Out_writes_bytes_to_opt_ DaclSize PACL _Inout_ PULONG _Out_writes_bytes_to_opt_ SaclSize PACL _Inout_ PULONG _Out_writes_bytes_to_opt_ OwnerSize PSID Owner
Definition: rtlfuncs.h:1557
ERESOURCE_THREAD OwnerThread
Definition: extypes.h:211
VOID NTAPI ExpExpandResourceOwnerTable(IN PERESOURCE Resource, IN PKLOCK_QUEUE_HANDLE LockHandle)
Definition: resource.c:316
*LockHandle LockHandle _Out_ PKLOCK_QUEUE_HANDLE LockHandle
Definition: kefuncs.h:742
#define KeGetCurrentThread
Definition: hal.h:44
_In_ LONG _In_ LONG Limit
Definition: kefuncs.h:328
POWNER_ENTRY FASTCALL ExpFindFreeEntry ( IN PERESOURCE  Resource,
IN PKLOCK_QUEUE_HANDLE  LockHandle 
)

Definition at line 416 of file resource.c.

Referenced by ExAcquireResourceSharedLite(), ExAcquireSharedStarveExclusive(), and ExAcquireSharedWaitForExclusive().

418 {
420 
421  /* Sanity check */
422  ASSERT(LockHandle != 0);
423  ASSERT(Resource->OwnerEntry.OwnerThread != 0);
424 
425  /* Get the current table pointer */
426  Owner = Resource->OwnerTable;
427  if (Owner)
428  {
429  /* Set the limit, move to the next owner and loop owner entries */
430  Limit = &Owner[Owner->TableSize];
431  Owner++;
432  while (Owner->OwnerThread)
433  {
434  /* Move to the next one */
435  Owner++;
436 
437  /* Check if the entry is free */
438  if (Owner == Limit) goto Expand;
439  }
440 
441  /* Update the resource entry */
442  KeGetCurrentThread()->ResourceIndex = (UCHAR)(Owner - Resource->OwnerTable);
443  }
444  else
445  {
446 Expand:
447  /* No free entry, expand the table */
449  Owner = NULL;
450  }
451 
452  /* Return the entry found */
453  return Owner;
454 }
Definition: extypes.h:210
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel)?(CompletionRoutine!=NULL):TRUE)
_Acquires_shared_lock_ _Curr_ PERESOURCE Resource
Definition: exfuncs.h:440
smooth NULL
Definition: ftsmooth.c:513
ULONG TableSize
Definition: extypes.h:218
unsigned char UCHAR
Definition: xmlstorage.h:181
_Out_writes_bytes_to_opt_ AbsoluteSecurityDescriptorSize PSECURITY_DESCRIPTOR _Inout_ PULONG _Out_writes_bytes_to_opt_ DaclSize PACL _Inout_ PULONG _Out_writes_bytes_to_opt_ SaclSize PACL _Inout_ PULONG _Out_writes_bytes_to_opt_ OwnerSize PSID Owner
Definition: rtlfuncs.h:1557
ERESOURCE_THREAD OwnerThread
Definition: extypes.h:211
VOID NTAPI ExpExpandResourceOwnerTable(IN PERESOURCE Resource, IN PKLOCK_QUEUE_HANDLE LockHandle)
Definition: resource.c:316
*LockHandle LockHandle _Out_ PKLOCK_QUEUE_HANDLE LockHandle
Definition: kefuncs.h:742
#define KeGetCurrentThread
Definition: hal.h:44
_In_ LONG _In_ LONG Limit
Definition: kefuncs.h:328
VOID NTAPI INIT_FUNCTION ExpResourceInitialization ( VOID  )

Definition at line 169 of file resource.c.

Referenced by ExpInitSystemPhase0().

170 {
171  /* Setup the timeout */
172  ExpTimeout.QuadPart = Int32x32To64(4, -10000000);
175 }
KSPIN_LOCK ExpResourceSpinLock
Definition: resource.c:75
FORCEINLINE VOID KeInitializeSpinLock(_Out_ PKSPIN_LOCK SpinLock)
Definition: kefuncs.h:251
LARGE_INTEGER ExpTimeout
Definition: resource.c:73
#define InitializeListHead(ListHead)
Definition: env_spec_w32.h:944
LIST_ENTRY ExpSystemResourcesList
Definition: resource.c:76
#define Int32x32To64(a, b)
LONGLONG QuadPart
Definition: typedefs.h:113
VOID FASTCALL ExpWaitForResource ( IN PERESOURCE  Resource,
IN PVOID  Object 
)

Definition at line 618 of file resource.c.

Referenced by ExAcquireResourceExclusiveLite(), ExAcquireResourceSharedLite(), ExAcquireSharedStarveExclusive(), and ExAcquireSharedWaitForExclusive().

620 {
621  ULONG i;
622  ULONG Size;
624  ULONG WaitCount = 0;
627  PKTHREAD Thread, OwnerThread;
628 #if DBG
630 #endif
631 
632  /* Increase contention count and use a 5 second timeout */
633  Resource->ContentionCount++;
634  Timeout.QuadPart = 500 * -10000;
635  for (;;)
636  {
637  /* Wait for ownership */
638  Status = KeWaitForSingleObject(Object,
639  WrResource,
640  KernelMode,
641  FALSE,
642  &Timeout);
643  if (Status != STATUS_TIMEOUT) break;
644 
645  /* Increase wait count */
646  WaitCount++;
647  Timeout = ExpTimeout;
648 
649  /* Check if we've exceeded the limit */
650  if (WaitCount > ExpResourceTimeoutCount)
651  {
652  /* Reset wait count */
653  WaitCount = 0;
654 #if DBG
655  /* Lock the resource */
656  ExAcquireResourceLock(Resource, &LockHandle);
657 
658  /* Dump debug information */
659  DPRINT1("Resource @ %p\n", Resource);
660  DPRINT1(" ActiveEntries = %04lx Flags = %s%s%s\n",
661  Resource->ActiveEntries,
662  IsOwnedExclusive(Resource) ? "IsOwnedExclusive " : "",
663  IsSharedWaiting(Resource) ? "SharedWaiter " : "",
664  IsExclusiveWaiting(Resource) ? "ExclusiveWaiter " : "");
665  DPRINT1(" NumberOfExclusiveWaiters = %04lx\n",
666  Resource->NumberOfExclusiveWaiters);
667  DPRINT1(" Thread = %08lx, Count = %02x\n",
668  Resource->OwnerEntry.OwnerThread,
669  Resource->OwnerEntry.OwnerCount);
670 
671  /* Dump out the table too */
672  Owner = Resource->OwnerTable;
673  if (Owner)
674  {
675  /* Loop every entry */
676  Size = Owner->TableSize;
677  for (i = 1; i < Size; i++)
678  {
679  /* Print the data */
680  Owner++;
681  DPRINT1(" Thread = %08lx, Count = %02x\n",
682  Owner->OwnerThread,
683  Owner->OwnerCount);
684  }
685  }
686 
687  /* Break */
688  DbgBreakPoint();
689  DPRINT1("EX - Rewaiting\n");
690  ExReleaseResourceLock(Resource, &LockHandle);
691 #endif
692  }
693 
694  /* Check if we can boost */
696  {
697  /* Get the current kernel thread and lock the dispatcher */
698  Thread = KeGetCurrentThread();
699  Thread->WaitIrql = KiAcquireDispatcherLock();
700  Thread->WaitNext = TRUE;
701 
702  /* Get the owner thread and boost it */
703  OwnerThread = (PKTHREAD)Resource->OwnerEntry.OwnerThread;
704  if (OwnerThread) ExpBoostOwnerThread(Thread, OwnerThread);
705 
706  /* If it's a shared resource */
708  {
709  /* Get the table */
710  Owner = Resource->OwnerTable;
711  if (Owner)
712  {
713  /* Loop every entry */
714  Size = Owner->TableSize;
715  for (i = 1; i < Size; i++)
716  {
717  /* Move to next entry */
718  Owner++;
719 
720  /* Get the thread */
721  OwnerThread = (PKTHREAD)Owner->OwnerThread;
722 
723  /* Boost it */
724  if (OwnerThread) ExpBoostOwnerThread(Thread, OwnerThread);
725  }
726  }
727  }
728  }
729  }
730 }
Definition: extypes.h:210
#define IsExclusiveWaiting(r)
Definition: resource.c:16
#define TRUE
Definition: types.h:120
#define IsBoostAllowed(r)
Definition: resource.c:19
void DbgBreakPoint()
Definition: mach.c:558
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 STATUS_TIMEOUT
Definition: ntstatus.h:81
struct _KTHREAD * PKTHREAD
Definition: nt_native.h:28
GLenum GLclampf GLint i
Definition: glfuncs.h:14
#define FALSE
Definition: types.h:117
_Acquires_shared_lock_ _Curr_ PERESOURCE Resource
Definition: exfuncs.h:440
ULONG WaitNext
Definition: ketypes.h:952
UINTN Size
Definition: acefiex.h:550
ULONG TableSize
Definition: extypes.h:218
ULONG OwnerCount
Definition: extypes.h:216
LARGE_INTEGER ExpTimeout
Definition: resource.c:73
if(!(yy_init))
Definition: macro.lex.yy.c:704
FORCEINLINE VOID ExReleaseResourceLock(IN PERESOURCE Resource, IN PKLOCK_QUEUE_HANDLE LockHandle)
Definition: resource.c:37
_In_opt_ PFILE_OBJECT _In_opt_ PETHREAD Thread
Definition: fltkernel.h:2653
#define IsOwnedExclusive(r)
Definition: resource.c:18
static IUnknown Object
Definition: main.c:512
FORCEINLINE VOID ExAcquireResourceLock(IN PERESOURCE Resource, IN PKLOCK_QUEUE_HANDLE LockHandle)
Definition: resource.c:25
Status
Definition: gdiplustypes.h:24
VOID FASTCALL ExpBoostOwnerThread(IN PKTHREAD Thread, IN PKTHREAD OwnerThread)
Definition: resource.c:572
LONG NTSTATUS
Definition: DriverTester.h:11
static ULONG Timeout
Definition: ping.c:61
FORCEINLINE KIRQL KiAcquireDispatcherLock(VOID)
Definition: ke_x.h:144
_Out_writes_bytes_to_opt_ AbsoluteSecurityDescriptorSize PSECURITY_DESCRIPTOR _Inout_ PULONG _Out_writes_bytes_to_opt_ DaclSize PACL _Inout_ PULONG _Out_writes_bytes_to_opt_ SaclSize PACL _Inout_ PULONG _Out_writes_bytes_to_opt_ OwnerSize PSID Owner
Definition: rtlfuncs.h:1557
ERESOURCE_THREAD OwnerThread
Definition: extypes.h:211
#define DPRINT1
Definition: precomp.h:8
ULONG ExpResourceTimeoutCount
Definition: resource.c:74
unsigned int ULONG
Definition: retypes.h:1
*LockHandle LockHandle _Out_ PKLOCK_QUEUE_HANDLE LockHandle
Definition: kefuncs.h:742
#define IsSharedWaiting(r)
Definition: resource.c:17
#define KeGetCurrentThread
Definition: hal.h:44
LONGLONG QuadPart
Definition: typedefs.h:113
KIRQL WaitIrql
Definition: ketypes.h:999
NTSTATUS NTAPI ExReinitializeResourceLite ( IN PERESOURCE  Resource)

Definition at line 1756 of file resource.c.

Referenced by START_TEST().

1757 {
1758  PKEVENT Event;
1759  PKSEMAPHORE Semaphore;
1760  ULONG i, Size;
1762 
1763  /* Get the owner table */
1764  Owner = Resource->OwnerTable;
1765  if (Owner)
1766  {
1767  /* Get the size and loop it */
1768  Size = Owner->TableSize;
1769  for (i = 0; i < Size; i++)
1770  {
1771  /* Zero the table */
1772  Owner[i].OwnerThread = 0;
1773  Owner[i].OwnerCount = 0;
1774  }
1775  }
1776 
1777  /* Zero the flags and count */
1778  Resource->Flag = 0;
1779  Resource->ActiveCount = 0;
1780  Resource->ActiveEntries = 0;
1781 
1782  /* Reset the semaphore */
1783  Semaphore = Resource->SharedWaiters;
1784  if (Semaphore) KeInitializeSemaphore(Semaphore, 0, MAXLONG);
1785 
1786  /* Reset the event */
1787  Event = Resource->ExclusiveWaiters;
1788  if (Event) KeInitializeEvent(Event, SynchronizationEvent, FALSE);
1789 
1790  /* Clear the resource data */
1791  Resource->OwnerEntry.OwnerThread = 0;
1792  Resource->OwnerEntry.OwnerCount = 0;
1793  Resource->ContentionCount = 0;
1794  Resource->NumberOfSharedWaiters = 0;
1795  Resource->NumberOfExclusiveWaiters = 0;
1796  return STATUS_SUCCESS;
1797 }
#define STATUS_SUCCESS
Definition: contextmenu.cpp:55
Definition: extypes.h:210
#define MAXLONG
Definition: umtypes.h:104
ACPI_EFI_EVENT Event
Definition: acefiex.h:607
GLenum GLclampf GLint i
Definition: glfuncs.h:14
#define FALSE
Definition: types.h:117
_Acquires_shared_lock_ _Curr_ PERESOURCE Resource
Definition: exfuncs.h:440
UINTN Size
Definition: acefiex.h:550
ULONG TableSize
Definition: extypes.h:218
ULONG OwnerCount
Definition: extypes.h:216
VOID NTAPI KeInitializeSemaphore(IN PKSEMAPHORE Semaphore, IN LONG Count, IN LONG Limit)
Definition: semphobj.c:22
#define KeInitializeEvent(pEvt, foo, foo2)
Definition: env_spec_w32.h:477
_Out_writes_bytes_to_opt_ AbsoluteSecurityDescriptorSize PSECURITY_DESCRIPTOR _Inout_ PULONG _Out_writes_bytes_to_opt_ DaclSize PACL _Inout_ PULONG _Out_writes_bytes_to_opt_ SaclSize PACL _Inout_ PULONG _Out_writes_bytes_to_opt_ OwnerSize PSID Owner
Definition: rtlfuncs.h:1557
ERESOURCE_THREAD OwnerThread
Definition: extypes.h:211
unsigned int ULONG
Definition: retypes.h:1
VOID FASTCALL ExReleaseResourceAndLeaveCriticalRegion ( IN PERESOURCE  Resource)

Definition at line 2278 of file resource.c.

Referenced by _Releases_lock_(), and NtAdjustPrivilegesToken().

2279 {
2280  /* Release the resource */
2282 
2283  /* Leave critical region */
2285 }
_Acquires_shared_lock_ _Curr_ PERESOURCE Resource
Definition: exfuncs.h:440
VOID FASTCALL ExReleaseResourceLite(IN PERESOURCE Resource)
Definition: resource.c:1817
#define KeLeaveCriticalRegion()
Definition: ke_x.h:114
VOID NTAPI ExReleaseResourceForThreadLite ( IN PERESOURCE  Resource,
IN ERESOURCE_THREAD  Thread 
)

Definition at line 1844 of file resource.c.

Referenced by __RxReleaseFcbForThread(), CcUnpinDataForThread(), CdMultiAsyncCompletionRoutine(), CdSingleAsyncCompletionRoutine(), ExReleaseResourceLite(), FFSAddEntry(), FFSAddMcbEntry(), FFSAllocateCcb(), FFSAllocateIrpContext(), FFSAllocateMcb(), FFSCheckDismount(), FFSCleanup(), FFSClose(), FFSCreateFile(), FFSDismountVolume(), FFSFastIoQueryBasicInfo(), FFSFastIoQueryNetworkOpenInfo(), FFSFastIoQueryStandardInfo(), FFSFlush(), FFSFlushFiles(), FFSFreeCcb(), FFSFreeFcb(), FFSFreeIrpContext(), FFSFreeMcb(), FFSInvalidateVolumes(), FFSLockVolume(), FFSLookupMcbEntry(), FFSMediaEjectControl(), FFSMountVolume(), FFSNotifyChangeDirectory(), FFSPurgeVolume(), FFSQueryDirectory(), FFSQueryInformation(), FFSQueryVolumeInformation(), FFSReadFile(), FFSReadVolume(), FFSRemoveEntry(), FFSRemoveMcbEntry(), FFSSetInformation(), FFSSetParentEntry(), FFSShutDown(), FFSUnlockVolume(), FFSv1AllocateFcb(), FFSv2AllocateFcb(), FFSVerifyVolume(), FFSWriteFile(), FFSWriteVolume(), RfsdAllocateCcb(), RfsdAllocateFcb(), RfsdAllocateIrpContext(), RfsdAllocateMcb(), RfsdCheckDismount(), RfsdCleanup(), RfsdClose(), RfsdCreateFile(), RfsdDismountVolume(), RfsdFastIoQueryBasicInfo(), RfsdFastIoQueryStandardInfo(), RfsdFlush(), RfsdFlushFiles(), RfsdFreeCcb(), RfsdFreeFcb(), RfsdFreeIrpContext(), RfsdFreeMcb(), RfsdInvalidateVolumes(), RfsdLockVolume(), RfsdMediaEjectControl(), RfsdMountVolume(), RfsdNotifyChangeDirectory(), RfsdPurgeVolume(), RfsdQueryDirectory(), RfsdQueryInformation(), RfsdQueryVolumeInformation(), RfsdReadFile(), RfsdReadVolume(), RfsdSetInformation(), RfsdShutDown(), RfsdUnlockVolume(), RfsdVerifyVolume(), RfsdWriteFile(), RfsdWriteVolume(), TestOwnerRes(), TestResourceWithOwner(), WCacheDiscardBlocks__(), WCacheEODirect__(), WCacheFlushAll__(), WCacheFlushBlocks__(), WCachePurgeAll__(), WCacheReadBlocks__(), WCacheRelease__(), and WCacheWriteBlocks__().

1846 {
1847  ULONG i;
1848  ULONG Count;
1851  ASSERT(Thread != 0);
1852 
1853  /* Get the thread and lock the resource */
1854  ExAcquireResourceLock(Resource, &LockHandle);
1855 
1856  /* Sanity checks */
1859 
1860  /* Check if it's exclusively owned */
1862  {
1863  /* Decrement owner count and check if we're done */
1864  ASSERT(Resource->OwnerEntry.OwnerThread == Thread);
1865  if (--Resource->OwnerEntry.OwnerCount)
1866  {
1867  /* Done, release lock! */
1868  ExReleaseResourceLock(Resource, &LockHandle);
1869  return;
1870  }
1871 
1872  /* Clear the owner */
1873  Resource->OwnerEntry.OwnerThread = 0;
1874 
1875  /* Decrement the number of active entries */
1876  ASSERT(Resource->ActiveEntries == 1);
1877  Resource->ActiveEntries--;
1878 
1879  /* Check if there are shared waiters */
1881  {
1882  /* Remove the exclusive flag */
1883  Resource->Flag &= ~ResourceOwnedExclusive;
1884 
1885  /* Give ownage to another thread */
1886  Count = Resource->NumberOfSharedWaiters;
1887  Resource->ActiveEntries = Count;
1888  Resource->NumberOfSharedWaiters = 0;
1889 
1890  /* Release lock and let someone else have it */
1891  ASSERT(Resource->ActiveCount == 1);
1892  ExReleaseResourceLock(Resource, &LockHandle);
1893  KeReleaseSemaphore(Resource->SharedWaiters, 0, Count, FALSE);
1894  return;
1895  }
1896  else if (IsExclusiveWaiting(Resource))
1897  {
1898  /* Give exclusive access */
1899  Resource->OwnerEntry.OwnerThread = 1;
1900  Resource->OwnerEntry.OwnerCount = 1;
1901  Resource->ActiveEntries = 1;
1902  Resource->NumberOfExclusiveWaiters--;
1903 
1904  /* Release the lock and give it away */
1905  ASSERT(Resource->ActiveCount == 1);
1906  ExReleaseResourceLock(Resource, &LockHandle);
1907  KeSetEventBoostPriority(Resource->ExclusiveWaiters,
1908  (PKTHREAD*)&Resource->OwnerEntry.OwnerThread);
1909  return;
1910  }
1911 
1912  /* Remove the exclusive flag */
1913  Resource->Flag &= ~ResourceOwnedExclusive;
1914  Resource->ActiveCount = 0;
1915  }
1916  else
1917  {
1918  /* Check if we are in the thread list */
1919  if (Resource->OwnerEntry.OwnerThread == Thread)
1920  {
1921  /* Found it, get owner */
1922  Owner = &Resource->OwnerEntry;
1923  }
1924  else
1925  {
1926  /* Assume no valid index */
1927  i = 1;
1928 
1929  /* If we got a valid pointer, try to get the resource index */
1930  if (!((ULONG)Thread & 3)) i = ((PKTHREAD)Thread)->ResourceIndex;
1931 
1932  /* Do a table lookup */
1933  Owner = Resource->OwnerTable;
1934  ASSERT(Owner != NULL);
1935 
1936  /* Check if we're out of the size and don't match */
1937  if ((i >= Owner->TableSize) || (Owner[i].OwnerThread != Thread))
1938  {
1939  /* Get the last entry */
1940  Limit = &Owner[Owner->TableSize];
1941  for (;;)
1942  {
1943  /* Move to the next entry */
1944  Owner++;
1945 
1946  /* Make sure we're not out of bounds */
1947  if (Owner >= Limit)
1948  {
1949  /* Bugcheck, nobody owns us */
1950  KeBugCheckEx(RESOURCE_NOT_OWNED,
1952  (ULONG_PTR)Thread,
1953  (ULONG_PTR)Resource->OwnerTable,
1954  (ULONG_PTR)3);
1955  }
1956 
1957  /* Check for a match */
1958  if (Owner->OwnerThread == Thread) break;
1959  }
1960  }
1961  else
1962  {
1963  /* Get the entry directly */
1964  Owner = &Owner[i];
1965  }
1966  }
1967 
1968  /* Sanity checks */
1969  ASSERT(Owner->OwnerThread == Thread);
1970  ASSERT(Owner->OwnerCount > 0);
1971 
1972  /* Check if we are the last owner */
1973  if (--Owner->OwnerCount)
1974  {
1975  /* There are other owners, release lock */
1976  ExReleaseResourceLock(Resource, &LockHandle);
1977  return;
1978  }
1979 
1980  /* Clear owner */
1981  Owner->OwnerThread = 0;
1982 
1983  /* See if the resource isn't being owned anymore */
1984  ASSERT(Resource->ActiveEntries > 0);
1985  if (!(--Resource->ActiveEntries))
1986  {
1987  /* Check if there's an exclusive waiter */
1989  {
1990  /* Give exclusive access */
1992  Resource->OwnerEntry.OwnerThread = 1;
1993  Resource->OwnerEntry.OwnerCount = 1;
1994  Resource->ActiveEntries = 1;
1995  Resource->NumberOfExclusiveWaiters--;
1996 
1997  /* Release the lock and give it away */
1998  ASSERT(Resource->ActiveCount == 1);
1999  ExReleaseResourceLock(Resource, &LockHandle);
2000  KeSetEventBoostPriority(Resource->ExclusiveWaiters,
2001  (PKTHREAD*)&Resource->OwnerEntry.OwnerThread);
2002  return;
2003  }
2004 
2005  /* Clear the active count */
2006  Resource->ActiveCount = 0;
2007  }
2008  }
2009 
2010  /* Release lock */
2011  ExReleaseResourceLock(Resource, &LockHandle);
2012 }
Definition: extypes.h:210
#define IsExclusiveWaiting(r)
Definition: resource.c:16
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel)?(CompletionRoutine!=NULL):TRUE)
_Inout_ __drv_aliasesMem PSLIST_ENTRY _Inout_ PSLIST_ENTRY _In_ ULONG Count
Definition: exfuncs.h:1015
uint32_t ULONG_PTR
Definition: typedefs.h:64
struct _KTHREAD * PKTHREAD
Definition: nt_native.h:28
GLenum GLclampf GLint i
Definition: glfuncs.h:14
#define FALSE
Definition: types.h:117
#define ExpVerifyResource(r)
Definition: resource.c:150
_Acquires_shared_lock_ _Curr_ PERESOURCE Resource
Definition: exfuncs.h:440
smooth NULL
Definition: ftsmooth.c:513
ULONG TableSize
Definition: extypes.h:218
ULONG OwnerCount
Definition: extypes.h:216
FORCEINLINE VOID ExReleaseResourceLock(IN PERESOURCE Resource, IN PKLOCK_QUEUE_HANDLE LockHandle)
Definition: resource.c:37
_In_opt_ PFILE_OBJECT _In_opt_ PETHREAD Thread
Definition: fltkernel.h:2653
#define IsOwnedExclusive(r)
Definition: resource.c:18
#define ExpCheckForApcsDisabled(b, r, t)
Definition: resource.c:151
FORCEINLINE VOID ExAcquireResourceLock(IN PERESOURCE Resource, IN PKLOCK_QUEUE_HANDLE LockHandle)
Definition: resource.c:25
LONG NTAPI KeReleaseSemaphore(IN PKSEMAPHORE Semaphore, IN KPRIORITY Increment, IN LONG Adjustment, IN BOOLEAN Wait)
Definition: semphobj.c:54
_Out_writes_bytes_to_opt_ AbsoluteSecurityDescriptorSize PSECURITY_DESCRIPTOR _Inout_ PULONG _Out_writes_bytes_to_opt_ DaclSize PACL _Inout_ PULONG _Out_writes_bytes_to_opt_ SaclSize PACL _Inout_ PULONG _Out_writes_bytes_to_opt_ OwnerSize PSID Owner
Definition: rtlfuncs.h:1557
ERESOURCE_THREAD OwnerThread
Definition: extypes.h:211
unsigned int ULONG
Definition: retypes.h:1
VOID NTAPI KeSetEventBoostPriority(IN PKEVENT Event, IN PKTHREAD *WaitingThread OPTIONAL)
Definition: eventobj.c:229
*LockHandle LockHandle _Out_ PKLOCK_QUEUE_HANDLE LockHandle
Definition: kefuncs.h:742
#define IsSharedWaiting(r)
Definition: resource.c:17
#define KeGetCurrentThread
Definition: hal.h:44
_In_ LONG _In_ LONG Limit
Definition: kefuncs.h:328
#define ResourceOwnedExclusive
Definition: dldetect.h:32
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
VOID FASTCALL ExReleaseResourceLite ( IN PERESOURCE  Resource)

Definition at line 1817 of file resource.c.

Referenced by __brelse(), __RxAcquireFcb(), __RxReleaseFcb(), _do_load_tree(), _free_fileref(), _free_tree(), acquire_for_lazy_write(), add_balance_item(), add_calc_job(), add_children_to_move_list(), add_data_reloc(), add_device(), add_dir_child(), add_metadata_reloc(), add_metadata_reloc_extent_item(), AddUnregisteredProvider(), alloc_chunk(), allocate_cache(), ApphelpCacheReleaseLock(), balance_data_chunk(), balance_metadata_chunk(), balance_thread(), BroadcastOpen(), BuildAndSubmitIrp(), calc_thread(), CcUnpinRepinnedBcb(), CdFastQueryBasicInfo(), CdFastQueryNetworkInfo(), CdFastQueryStdInfo(), CdfsCleanup(), CdfsCreate(), CdfsReleaseFromLazyWrite(), CdfsShortNameCacheGet(), CdfsVerifyVolume(), CdReleaseForCreateSection(), CdReleaseFromCache(), clean_space_cache(), close_file(), CmpUnlockHiveFlusher(), CmpUnlockRegistry(), CountAvailableClusters(), create_snapshot(), create_subvol(), CreateRedirectedFile(), delete_fileref(), delete_reparse_point(), disk_arrival(), disk_removal(), dismount_volume(), do_calc(), do_flush(), do_read_job(), do_rollback(), do_write(), DoQuery(), drv_cleanup(), drv_create(), drv_flush_buffers(), drv_query_information(), drv_query_volume_information(), drv_read(), DxEngUnlockHdev(), Ex2ProcessUserPerfStat(), ExReleaseResourceAndLeaveCriticalRegion(), ExReleaseTimeRefreshLock(), Ext2AddEntry(), Ext2bhReaperThread(), Ext2CheckDismount(), Ext2Cleanup(), Ext2CleanupAllMcbs(), Ext2Close(), Ext2Create(), Ext2CreateFile(), Ext2DeleteFile(), Ext2DeleteReparsePoint(), Ext2DismountVolume(), Ext2DropBH(), Ext2ExceptionHandler(), Ext2FastIoQueryBasicInfo(), Ext2FastIoQueryNetworkOpenInfo(), Ext2FastIoQueryStandardInfo(), Ext2FastIoWrite(), Ext2FcbReaperThread(), Ext2FirstUnusedMcb(), Ext2FloppyFlush(), Ext2Flush(), Ext2FlushFiles(), Ext2FlushVolume(), Ext2FreeBlock(), Ext2FreeInode(), Ext2GetRetrievalPointerBase(), Ext2GetRetrievalPointers(), Ext2InsertMcb(), Ext2InvalidateVolumes(), Ext2LinkHeadMcb(), Ext2LinkTailMcb(), Ext2LoadGroup(), Ext2LockControl(), Ext2LockVolume(), Ext2LookupFile(), Ext2McbReaperThread(), Ext2MediaEjectControl(), Ext2MountVolume(), Ext2NewBlock(), Ext2NewInode(), Ext2NotifyChangeDirectory(), Ext2OplockRequest(), Ext2ProcessGlobalProperty(), Ext2ProcessVolumeProperty(), Ext2PurgeFile(), Ext2PurgeVolume(), Ext2QueryDirectory(), Ext2QueryFileInformation(), Ext2QueryRetrievalPointers(), Ext2QueryUnusedBH(), Ext2QueryUnusedFcb(), Ext2QueryVolumeInformation(), Ext2ReadFile(), Ext2ReadVolume(), Ext2ReleaseFcb(), Ext2ReleaseFileForCcFlush(), Ext2ReleaseFileForModWrite(), Ext2ReleaseForCreateSection(), Ext2ReleaseFromLazyWrite(), Ext2ReleaseFromReadAhead(), Ext2RemoveEntry(), Ext2RemoveMcb(), Ext2SearchMcb(), Ext2SetFileInformation(), Ext2SetFileType(), Ext2SetLinkInfo(), Ext2SetParentEntry(), Ext2SetRenameInfo(), Ext2SetReparsePoint(), Ext2SetVolumeInformation(), Ext2ShutDown(), Ext2UnlinkFcb(), Ext2UnlinkMcb(), Ext2UnlockVolume(), Ext2UpdateGroupDirStat(), Ext2VerifyVolume(), Ext2WriteFile(), Ext2WriteVolume(), fast_query_basic_info(), fast_query_standard_info(), FatiCleanup(), FatiCleanVcbs(), FatiOpenExistingFcb(), FatiOverwriteFile(), FatPendingClose(), FatReleaseFcb(), FatReleaseVcb(), FatSetEndOfFileInfo(), fcb_get_last_dir_index(), FFSCleanup(), FFSCreateVolume(), FFSFloppyFlush(), FFSFlushVolume(), FFSInvalidateVolumes(), FFSPurgeFile(), FFSPurgeVolume(), FFSReleaseFromLazyWrite(), FFSReleaseFromReadAhead(), FFSv1WriteInode(), FFSWriteFile(), FFSWriteVolume(), file_create2(), fill_in_hard_link_information(), find_device_from_uuid(), find_file_in_dir(), finish_removing_device(), FltUnregisterFilter(), FsRtlAcquireFileForModWriteEx(), FsRtlCopyRead(), FsRtlCopyWrite(), FsRtlMdlReadDev(), FsRtlPrepareMdlWriteDev(), FsRtlReleaseFile(), FsRtlReleaseFileForCcFlush(), FsRtlReleaseFileForModWrite(), get_block_bh_mdl(), get_block_bh_pin(), get_chunk_from_address(), get_devices(), get_inode_info(), get_object_id(), get_reparse_point(), get_reparse_tag(), get_tree_new_address(), get_usage(), GetNextCluster(), GetNextClusterExtend(), insert_cache_extent(), insert_extent(), insert_extent_chunk(), insert_fileref_child(), insert_prealloc_extent(), insert_tree_extent(), insert_tree_extent_skinny(), invalidate_volumes(), IoEnumerateRegisteredFiltersList(), IopActionInitChildServices(), IopAttachFilterDriversCallback(), IopBootLog(), IopGetSetSecurityObject(), IopLoadUnloadDriver(), IopMountVolume(), IopParseDevice(), IopSaveBootLogToFile(), IopSetDeviceSecurityDescriptor(), IoRegisterFileSystem(), IoRegisterFsRegistrationChange(), IoUnregisterFileSystem(), IoUnregisterFsRegistrationChange(), is_device_part_of_mounted_btrfs_raid(), is_volume_mounted(), KspSynchronizedEventRoutine(), KsReleaseDeviceSecurityLock(), load_chunk_root(), lock_volume(), lzo_write_compressed_bit(), MiLoadUserSymbols(), MiProcessLoaderEntry(), MmGetSystemRoutineAddress(), mount_vol(), move_across_subvols(), MupCheckForUnregisteredProvider(), MupCleanup(), MupCleanupFcb(), MupCleanupVcb(), MupCloseFcb(), MupCloseUncProvider(), MupCloseVcb(), MupDecodeFileObject(), MupDereferenceCcb(), MupDereferenceMasterQueryContext(), MupForwardIoRequest(), MupInvalidatePrefixTable(), notify_change_directory(), NpCommonFlushBuffers(), NpCommonRead(), NpCommonWrite(), NpDisconnect(), NpListen(), NpQueryClientProcess(), NpReleaseVcb(), NpSetClientProcess(), NpTransceive(), NtfsCleanup(), NtfsCleanupFile(), NtfsClose(), NtfsCreate(), NtfsQueryDirectory(), NtfsQueryInformation(), NtfsQueryVolumeInformation(), ObpLeaveObjectTypeMutex(), ObpReleaseObjectLock(), open_file(), OpenMupFileSystem(), pnp_cancel_remove_device(), pnp_query_remove_device(), QSI_DEF(), query_directory(), query_filesystems(), query_ranges(), QueryPathCompletionRoutine(), raid_generations_okay(), ReadVolumeLabel(), reduce_tree_extent(), refresh_mountmgr(), RegisterUncProvider(), release_from_lazy_write(), remove_balance_item(), remove_device(), RfsdCleanup(), RfsdCreateVolume(), RfsdFastIoQueryNetworkOpenInfo(), RfsdFloppyFlush(), RfsdFlushVolume(), RfsdInvalidateVolumes(), RfsdPurgeFile(), RfsdPurgeVolume(), RfsdReleaseFromLazyWrite(), RfsdReleaseFromReadAhead(), RfsdWriteFile(), RfsdWriteVolume(), RtlLeaveHeapLock(), RxFindOrCreateFcb(), RxpReleasePrefixTableLock(), RxStartMinirdr(), send_disks_pnp_message(), send_notification_fcb(), set_basic_information(), set_disposition_information(), set_end_of_file_information(), set_file_security(), set_inode_info(), set_label(), set_link_information(), set_rename_information(), set_reparse_point(), set_sparse(), set_zero_data(), test_vol(), TestResourceExclusiveAccess(), TestResourceSharedAccess(), try_extend_data(), uninit(), update_changed_extent_ref(), update_chunk_caches(), update_chunk_usage(), update_chunks(), update_volumes(), UserLeave(), verify_vcb(), verify_volume(), VfatCleanup(), VfatCleanupFile(), VfatClose(), VfatCreate(), VfatCreateFile(), VfatDismountVolume(), VfatFastIoQueryBasicInfo(), VfatFastIoQueryStandardInfo(), VfatFlush(), VfatFlushVolume(), VfatGetRetrievalPointers(), VfatMount(), VfatQueryInformation(), VfatQueryVolumeInformation(), VfatRead(), VfatReleaseForCcFlush(), VfatReleaseFromLazyWrite(), VfatReleaseFromReadAhead(), VfatSetInformation(), VfatSetVolumeInformation(), VfatShutdown(), VfatWrite(), write_file2(), write_metadata_items(), WriteCluster(), and zlib_write_compressed_bit().

1818 {
1819  /* Just call the For-Thread function */
1821 }
#define ExGetCurrentResourceThread()
Definition: env_spec_w32.h:633
_Acquires_shared_lock_ _Curr_ PERESOURCE Resource
Definition: exfuncs.h:440
VOID NTAPI ExReleaseResourceForThreadLite(IN PERESOURCE Resource, IN ERESOURCE_THREAD Thread)
Definition: resource.c:1844
VOID NTAPI ExSetResourceOwnerPointer ( IN PERESOURCE  Resource,
IN PVOID  OwnerPointer 
)

Definition at line 2045 of file resource.c.

Referenced by CcSetBcbOwnerPointer(), RxCommonRead(), and TestResourceWithOwner().

2047 {
2050  POWNER_ENTRY Owner, ThisOwner;
2051 
2052  /* Sanity check */
2053  ASSERT((OwnerPointer != 0) && (((ULONG_PTR)OwnerPointer & 3) == 3));
2054 
2055  /* Get the thread */
2056  Thread = ExGetCurrentResourceThread();
2057 
2058  /* Sanity check */
2060 
2061  /* Lock the resource */
2062  ExAcquireResourceLock(Resource, &LockHandle);
2063 
2064  /* Check if it's exclusive */
2066  {
2067  /* If it's exclusive, set the first entry no matter what */
2068  ASSERT(Resource->OwnerEntry.OwnerThread == Thread);
2069  ASSERT(Resource->OwnerEntry.OwnerCount > 0);
2070  Resource->OwnerEntry.OwnerThread = (ULONG_PTR)OwnerPointer;
2071  }
2072  else
2073  {
2074  /* Set the thread in both entries */
2075  ThisOwner = ExpFindEntryForThread(Resource,
2077  0,
2078  FALSE);
2079  Owner = ExpFindEntryForThread(Resource, Thread, 0, FALSE);
2080  if (!Owner)
2081  {
2082  /* Nobody owns it, crash */
2083  KeBugCheckEx(RESOURCE_NOT_OWNED,
2085  Thread,
2086  (ULONG_PTR)Resource->OwnerTable,
2087  3);
2088  }
2089 
2090  /* Set if we are the owner */
2091  if (ThisOwner)
2092  {
2093  /* Update data */
2094  ThisOwner->OwnerCount += Owner->OwnerCount;
2095  Owner->OwnerCount = 0;
2096  Owner->OwnerThread = 0;
2097  ASSERT(Resource->ActiveEntries >= 2);
2098  Resource->ActiveEntries--;
2099  }
2100  else
2101  {
2102  /* Update the owner entry instead */
2104  }
2105  }
2106 
2107  /* Release the resource */
2108  ExReleaseResourceLock(Resource, &LockHandle);
2109 }
#define ExGetCurrentResourceThread()
Definition: env_spec_w32.h:633
Definition: extypes.h:210
ULONG_PTR ERESOURCE_THREAD
Definition: extypes.h:208
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel)?(CompletionRoutine!=NULL):TRUE)
_In_ PVOID OwnerPointer
Definition: exfuncs.h:862
POWNER_ENTRY FASTCALL ExpFindEntryForThread(IN PERESOURCE Resource, IN ERESOURCE_THREAD Thread, IN PKLOCK_QUEUE_HANDLE LockHandle, IN BOOLEAN FirstEntryInelligible)
Definition: resource.c:479
uint32_t ULONG_PTR
Definition: typedefs.h:64
#define FALSE
Definition: types.h:117
#define ExpVerifyResource(r)
Definition: resource.c:150
_Acquires_shared_lock_ _Curr_ PERESOURCE Resource
Definition: exfuncs.h:440
ULONG OwnerCount
Definition: extypes.h:216
FORCEINLINE VOID ExReleaseResourceLock(IN PERESOURCE Resource, IN PKLOCK_QUEUE_HANDLE LockHandle)
Definition: resource.c:37
_In_opt_ PFILE_OBJECT _In_opt_ PETHREAD Thread
Definition: fltkernel.h:2653
#define IsOwnedExclusive(r)
Definition: resource.c:18
FORCEINLINE VOID ExAcquireResourceLock(IN PERESOURCE Resource, IN PKLOCK_QUEUE_HANDLE LockHandle)
Definition: resource.c:25
_Out_writes_bytes_to_opt_ AbsoluteSecurityDescriptorSize PSECURITY_DESCRIPTOR _Inout_ PULONG _Out_writes_bytes_to_opt_ DaclSize PACL _Inout_ PULONG _Out_writes_bytes_to_opt_ SaclSize PACL _Inout_ PULONG _Out_writes_bytes_to_opt_ OwnerSize PSID Owner
Definition: rtlfuncs.h:1557
ERESOURCE_THREAD OwnerThread
Definition: extypes.h:211
#define ULONG_PTR
Definition: config.h:101
*LockHandle LockHandle _Out_ PKLOCK_QUEUE_HANDLE LockHandle
Definition: kefuncs.h:742
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
BOOLEAN NTAPI ExTryToAcquireResourceExclusiveLite ( IN PERESOURCE  Resource)

Definition at line 2129 of file resource.c.

Referenced by ApphelpCacheTryAcquireLock().

2130 {
2133  BOOLEAN Acquired = FALSE;
2134 
2135  /* Sanity check */
2136  ASSERT((Resource->Flag & ResourceNeverExclusive) == 0);
2137 
2138  /* Get the thread */
2139  Thread = ExGetCurrentResourceThread();
2140 
2141  /* Sanity check and validation */
2144 
2145  /* Acquire the lock */
2146  ExAcquireResourceLock(Resource, &LockHandle);
2147 
2148  /* Check if there is an owner */
2149  if (!Resource->ActiveCount)
2150  {
2151  /* No owner, give exclusive access */
2153  Resource->OwnerEntry.OwnerThread = Thread;
2154  Resource->OwnerEntry.OwnerCount = 1;
2155  Resource->ActiveCount = 1;
2156  Resource->ActiveEntries = 1;
2157  Acquired = TRUE;
2158  }
2159  else if ((IsOwnedExclusive(Resource)) &&
2160  (Resource->OwnerEntry.OwnerThread == Thread))
2161  {
2162  /* Do a recursive acquire */
2163  Resource->OwnerEntry.OwnerCount++;
2164  Acquired = TRUE;
2165  }
2166 
2167  /* Release the resource */
2168  ExReleaseResourceLock(Resource, &LockHandle);
2169  return Acquired;
2170 }
#define ExGetCurrentResourceThread()
Definition: env_spec_w32.h:633
ULONG_PTR ERESOURCE_THREAD
Definition: extypes.h:208
#define TRUE
Definition: types.h:120
#define ResourceNeverExclusive
Definition: extypes.h:245
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel)?(CompletionRoutine!=NULL):TRUE)
BOOLEAN NTAPI KeIsExecutingDpc(VOID)
Definition: dpc.c:946
#define FALSE
Definition: types.h:117
#define ExpVerifyResource(r)
Definition: resource.c:150
_Acquires_shared_lock_ _Curr_ PERESOURCE Resource
Definition: exfuncs.h:440
unsigned char BOOLEAN
FORCEINLINE VOID ExReleaseResourceLock(IN PERESOURCE Resource, IN PKLOCK_QUEUE_HANDLE LockHandle)
Definition: resource.c:37
_In_opt_ PFILE_OBJECT _In_opt_ PETHREAD Thread
Definition: fltkernel.h:2653
#define IsOwnedExclusive(r)
Definition: resource.c:18
FORCEINLINE VOID ExAcquireResourceLock(IN PERESOURCE Resource, IN PKLOCK_QUEUE_HANDLE LockHandle)
Definition: resource.c:25
*LockHandle LockHandle _Out_ PKLOCK_QUEUE_HANDLE LockHandle
Definition: kefuncs.h:742
#define ResourceOwnedExclusive
Definition: dldetect.h:32

Variable Documentation

KSPIN_LOCK ExpResourceSpinLock
ULONG ExpResourceTimeoutCount = 90 * 3600 / 2

Definition at line 74 of file resource.c.

Referenced by ExpWaitForResource().

LIST_ENTRY ExpSystemResourcesList

Definition at line 76 of file resource.c.

LARGE_INTEGER ExpTimeout

Definition at line 73 of file resource.c.

Referenced by ExpWaitForResource().

BOOLEAN ExResourceStrict = TRUE

Definition at line 77 of file resource.c.

LARGE_INTEGER ExShortTime = {{-100000, -1}}

Definition at line 72 of file resource.c.