NT Executive Resources
description
Transcript of NT Executive Resources
![Page 1: NT Executive Resources](https://reader036.fdocuments.us/reader036/viewer/2022062310/56815dc7550346895dcbf4bb/html5/thumbnails/1.jpg)
NT Executive Resources
April 24, 2000Instructor: Gary Kimura
![Page 2: NT Executive Resources](https://reader036.fdocuments.us/reader036/viewer/2022062310/56815dc7550346895dcbf4bb/html5/thumbnails/2.jpg)
204/22/23
NT Resource
• The NT Resource module is a general purpose shared/exclusive access control package.
• It is callable from all kernel mode code• It is used in the file systems to control access to
common data structures and files• It is used in the cache manager to represent shared
and exclusive access to cached sections• It is used in device drivers• A version was even ported to user mode
![Page 3: NT Executive Resources](https://reader036.fdocuments.us/reader036/viewer/2022062310/56815dc7550346895dcbf4bb/html5/thumbnails/3.jpg)
304/22/23
Basic Features
• The package uses Thread based ownership. Meaning a thread is the indivisible entity that can acquire and release access to a Resource
• There can be multiple readers (i.e., shared access)• There can be a single writer (i.e., exclusive access)• Special code is added to prevent or even promote
starvation• Recursive acquisition is allowed• A thread can even acquire and maintain ownership
across kernel calls• The package guards against priority inversion
![Page 4: NT Executive Resources](https://reader036.fdocuments.us/reader036/viewer/2022062310/56815dc7550346895dcbf4bb/html5/thumbnails/4.jpg)
404/22/23
Overall design
• Basically the Resource is an ADT with the following API– ExInitializeResource( Resource )– ExAcquireResourceShared( Resource, Wait )– ExAcquireResourceSharedStarveExclusive( Resource, Wait )– ExAcquireResourceExclusive( Resource, Wait )– ExReleaseResource( Resource )– ExConvertExclusiveToShared( Resource )– ExDeleteResource( Resource )
• Global debug support is built into the package– Global list of all resources in the system
![Page 5: NT Executive Resources](https://reader036.fdocuments.us/reader036/viewer/2022062310/56815dc7550346895dcbf4bb/html5/thumbnails/5.jpg)
504/22/23
Resource Data Fields
• Each Resource contains the following data fields:– SpinLock – Controls access to the data fields– SystemResourcesList – Global list of resources– ActiveCount – Number of threads that own this
resource– Flags – State bits for the resource (owned exclusive,
etc) – OwnerTable – Pointer to an ownership table array– OwnerThreads[2] – An allocation optimization for
with at most two owners
![Page 6: NT Executive Resources](https://reader036.fdocuments.us/reader036/viewer/2022062310/56815dc7550346895dcbf4bb/html5/thumbnails/6.jpg)
604/22/23
Resource Data Fields (continued)
– SharedWaiters – A semaphore for threads waiting for shared access to the resource
– ExclusiveWaiters – A synchronization event for threads waiting for exclusive access to the resource
– NumberOfSharedWaiters – Current number of shared waiters
– NumberOfExclusiveWaiters – Current number of exclusive waiters
– ContentionCount – Number of times a thread has had to wait for this resource
![Page 7: NT Executive Resources](https://reader036.fdocuments.us/reader036/viewer/2022062310/56815dc7550346895dcbf4bb/html5/thumbnails/7.jpg)
704/22/23
Notes
• Global data for resources– Global spinlock – Used for getting access to the global
resource list– Global list head – Used to link all of the resources in the
system into a common list
• Owner table– Each table entry contains a thread id and a thread ownership
count– The table can dynamically grow as resource usage increases– Exclusive ownership is stored in OwnerTable[0]
![Page 8: NT Executive Resources](https://reader036.fdocuments.us/reader036/viewer/2022062310/56815dc7550346895dcbf4bb/html5/thumbnails/8.jpg)
804/22/23
Acquire Resource Exclusive Logic
Acquire resource spinlockif the resource is available (i.e., Active count == 0) then Set current thread as the exclusive resource owner Set ActiveCount = 1 Release resource spinlockelse if the resource is held exclusive by the current thread then Increment Thread Ownership count Release resource spinlockelse if we can wait for the resource then Increment NumberOfExclusiveWaiters Release resource spinlock Wait on the ExclusiveWaiters event Set current thread as the exclusive resource ownerreturn
![Page 9: NT Executive Resources](https://reader036.fdocuments.us/reader036/viewer/2022062310/56815dc7550346895dcbf4bb/html5/thumbnails/9.jpg)
904/22/23
Acquire Resource Shared Logic
If Resource is availableSet current thread in OwnerTable;Set ActiveCount = 1;return;
If Resource is held exclusiveIf current thread already has the resource
Increment Thread Ownership count;return;
If current thread does not have the resourceAdd current thread to OwnerTable;Wait on SharedWaiter semaphore;return;
![Page 10: NT Executive Resources](https://reader036.fdocuments.us/reader036/viewer/2022062310/56815dc7550346895dcbf4bb/html5/thumbnails/10.jpg)
1004/22/23
Acquire Resource Shared Logic (Continued)
Resource is already sharedIf current thread already has the resource
Increment Thread Ownership count;return;
If current thread does not have the resourceIf there are exclusive waiters
Add current thread to the OwnerTable;Wait on the SharedWaiters semaphore;return;
If there are no exclusive waitersAdd current thread to the OwnerTable;return;
![Page 11: NT Executive Resources](https://reader036.fdocuments.us/reader036/viewer/2022062310/56815dc7550346895dcbf4bb/html5/thumbnails/11.jpg)
1104/22/23
Release Resource Logic
If resource is held exclusiveDecrement Thread Ownership count;If thread ownership count is now zero
If there are threads waiting for shared accessSet resource for shared access;Signal SharedWaiters by number of shared waiters;return;
If there are threads waiting for exclusive accessSignal ExclusiveWaiters;return;
![Page 12: NT Executive Resources](https://reader036.fdocuments.us/reader036/viewer/2022062310/56815dc7550346895dcbf4bb/html5/thumbnails/12.jpg)
1204/22/23
Release Resource Logic (continued)
If resource is held sharedLocate and decrement Thread Ownership count;If thread ownership count is now zero
Decrement ActiveCount;If ActiveCount is now zero
If there are threads waiting for exclusive accessSet resource to look like it is held exclusive;Signal ExclusiveWaiters;return;