120 likes | 132 Views
The NT Resource module is a general-purpose shared/exclusive access control package used in file systems, cache manager, and device drivers. It allows multiple readers (shared access) and a single writer (exclusive access) with special code to prevent starvation.
E N D
NT Executive Resources April 24, 2000 Instructor: Gary Kimura
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
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
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
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
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
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]
Acquire Resource Exclusive Logic Acquire resource spinlock if the resource is available (i.e., Active count == 0) then Set current thread as the exclusive resource owner Set ActiveCount = 1 Release resource spinlock else if the resource is held exclusive by the current thread then Increment Thread Ownership count Release resource spinlock else 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 owner return
Acquire Resource Shared Logic If Resource is available Set current thread in OwnerTable; Set ActiveCount = 1; return; If Resource is held exclusive If current thread already has the resource Increment Thread Ownership count; return; If current thread does not have the resource Add current thread to OwnerTable; Wait on SharedWaiter semaphore; return;
Acquire Resource Shared Logic (Continued) Resource is already shared If current thread already has the resource Increment Thread Ownership count; return; If current thread does not have the resource If there are exclusive waiters Add current thread to the OwnerTable; Wait on the SharedWaiters semaphore; return; If there are no exclusive waiters Add current thread to the OwnerTable; return;
Release Resource Logic If resource is held exclusive Decrement Thread Ownership count; If thread ownership count is now zero If there are threads waiting for shared access Set resource for shared access; Signal SharedWaiters by number of shared waiters; return; If there are threads waiting for exclusive access Signal ExclusiveWaiters; return;
Release Resource Logic (continued) If resource is held shared Locate 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 access Set resource to look like it is held exclusive; Signal ExclusiveWaiters; return;