3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/ex/resource.c
6 * PURPOSE: Resource synchronization construct
14 * Usage of ERESOURCE members is not documented.
15 * From names of members and functionnalities, we can assume :
17 * OwnerTable = list of threads who have shared access(if more than one)
18 * ActiveCount = number of threads who have access to the resource
19 * Flag = bits : ResourceOwnedExclusive=0x80
20 * ResourceNeverExclusive=0x10
21 * ResourceReleaseByOtherThread=0x20
22 * ResourceDisableBoost=0x08
23 * SharedWaiters = semaphore, used to manage wait list of shared waiters.
24 * ExclusiveWaiters = event, used to manage wait list of exclusive waiters.
25 * OwnerThreads[0]= thread who have exclusive access
26 * OwnerThreads[1]= if only one thread own the resource
27 * thread who have shared access
30 * and TableSize = number of entries in the owner table
31 * NumberOfExclusiveWaiters = number of threads waiting for exclusive access.
32 * NumberOfSharedWaiters = number of threads waiting for exclusive access.
36 #define ResourceOwnedExclusive 0x80
37 #define ResourceDisableBoost 0x08
39 /* INCLUDES *****************************************************************/
41 #include <ddk/ntddk.h>
42 #include <internal/ke.h>
43 #include <internal/pool.h>
46 #include <internal/debug.h>
48 /* GLOBALS *******************************************************************/
50 #define TAG_OWNER_TABLE TAG('R', 'O', 'W', 'N')
51 #define TAG_EXCLUSIVE_LOCK TAG('E', 'R', 'E', 'L')
52 #define TAG_SHARED_SEM TAG('E', 'R', 'S', 'S')
54 /* FUNCTIONS *****************************************************************/
60 ExTryToAcquireResourceExclusiveLite (
64 * FUNCTION: Attempts to require the resource for exclusive access
66 * Resource = Points to the resource of be acquired
67 * RETURNS: TRUE if the resource was acquired for the caller
68 * NOTES: Must be acquired at IRQL < DISPATCH_LEVEL
71 return(ExAcquireResourceExclusiveLite(Resource,FALSE));
76 ExAcquireResourceExclusive (
81 return(ExAcquireResourceExclusiveLite(Resource,Wait));
84 #endif /* LIBCAPTIVE */
88 ExAcquireResourceExclusiveLite (
93 * FUNCTION: Acquires a resource exclusively for the calling thread
95 * Resource = Points to the resource to acquire
96 * Wait = Is set to TRUE if the caller should wait to acquire the
97 * resource if it can't be acquired immediately
98 * RETURNS: TRUE if the resource was acquired,
100 * NOTES: Must be called at IRQL < DISPATCH_LEVEL
105 DPRINT("ExAcquireResourceExclusiveLite(Resource %x, Wait %d)\n",
108 KeAcquireSpinLock(&Resource->SpinLock, &oldIrql);
110 /* resource already locked */
111 if((Resource->Flag & ResourceOwnedExclusive)
112 && Resource->OwnerThreads[0].OwnerThread == ExGetCurrentResourceThread())
114 /* it's ok : same lock for same thread */
115 Resource->OwnerThreads[0].a.OwnerCount++;
116 KeReleaseSpinLock(&Resource->SpinLock, oldIrql);
117 DPRINT("ExAcquireResourceExclusiveLite() = TRUE\n");
121 if (Resource->ActiveCount && !Wait)
123 KeReleaseSpinLock(&Resource->SpinLock, oldIrql);
124 DPRINT("ExAcquireResourceExclusiveLite() = FALSE\n");
129 * This is slightly better than it looks because other exclusive
130 * threads who are waiting won't be woken up but there is a race
131 * with new threads trying to grab the resource so we must have
132 * the spinlock, still normally this loop will only be executed
134 * NOTE: We might want to set a timeout to detect deadlock
137 while (Resource->ActiveCount)
139 Resource->NumberOfExclusiveWaiters++;
140 KeReleaseSpinLock(&Resource->SpinLock, oldIrql);
141 KeWaitForSingleObject(Resource->ExclusiveWaiters,
146 KeAcquireSpinLock(&Resource->SpinLock, &oldIrql);
147 Resource->NumberOfExclusiveWaiters--;
149 Resource->Flag |= ResourceOwnedExclusive;
150 Resource->ActiveCount = 1;
151 Resource->OwnerThreads[0].OwnerThread = ExGetCurrentResourceThread();
152 Resource->OwnerThreads[0].a.OwnerCount = 1;
153 KeReleaseSpinLock(&Resource->SpinLock, oldIrql);
154 DPRINT("ExAcquireResourceExclusiveLite() = TRUE\n");
158 static BOOLEAN EiRemoveSharedOwner(PERESOURCE Resource,
159 ERESOURCE_THREAD ResourceThreadId)
161 * FUNCTION: Removes the current thread from the shared owners of the resource
163 * Resource = Pointer to the resource for which the thread is to be
165 * NOTE: Must be called with the resource spinlock held
170 if (Resource->OwnerThreads[1].OwnerThread == ResourceThreadId)
172 Resource->OwnerThreads[1].a.OwnerCount--;
173 if (Resource->OwnerThreads[1].a.OwnerCount == 0)
175 Resource->ActiveCount--;
176 Resource->OwnerThreads[1].OwnerThread = 0;
181 if (Resource->OwnerThreads[1].OwnerThread)
183 /* Oh dear, the caller didn't own the resource after all */
187 for (i=0; i<Resource->OwnerThreads[1].a.TableSize; i++)
189 if (Resource->OwnerTable[i].OwnerThread == ResourceThreadId)
191 Resource->OwnerTable[i].a.OwnerCount--;
192 if (Resource->OwnerTable[i].a.OwnerCount == 0)
194 Resource->ActiveCount--;
195 Resource->OwnerTable[i].OwnerThread = 0;
204 static BOOLEAN EiAddSharedOwner(PERESOURCE Resource)
206 * FUNCTION: Adds the current thread to the shared owners of the resource
208 * Resource = Pointer to the resource for which the thread is to be
210 * NOTE: Must be called with the resource spinlock held
213 ERESOURCE_THREAD CurrentThread = ExGetCurrentResourceThread();
214 POWNER_ENTRY freeEntry;
217 DPRINT("EiAddSharedOwner(Resource %x)\n", Resource);
219 if (Resource->ActiveCount == 0)
221 /* no owner, it's easy */
222 Resource->OwnerThreads[1].OwnerThread = ExGetCurrentResourceThread();
223 Resource->OwnerThreads[1].a.OwnerCount = 1;
224 if (Resource->OwnerTable != NULL)
226 ExFreePool(Resource->OwnerTable);
228 Resource->OwnerTable = NULL;
229 Resource->ActiveCount = 1;
230 DPRINT("EiAddSharedOwner() = TRUE\n");
235 * now, we must search if this thread has already acquired this resource
236 * then increase ownercount if found, else create new entry or reuse free
239 if (Resource->OwnerTable == NULL)
241 DPRINT("Creating owner table\n");
243 /* allocate ownertable,memset to 0, initialize first entry */
244 Resource->OwnerTable =
245 ExAllocatePoolWithTag(NonPagedPool, sizeof(OWNER_ENTRY)*3,
247 if (Resource->OwnerTable == NULL)
252 memset(Resource->OwnerTable,0,sizeof(OWNER_ENTRY)*3);
253 memcpy(&Resource->OwnerTable[0], &Resource->OwnerThreads[1],
254 sizeof(OWNER_ENTRY));
256 Resource->OwnerThreads[1].OwnerThread = 0;
257 Resource->OwnerThreads[1].a.TableSize = 3;
259 Resource->OwnerTable[1].OwnerThread = CurrentThread;
260 Resource->OwnerTable[1].a.OwnerCount = 1;
261 Resource->ActiveCount++;
266 DPRINT("Search free entries\n");
268 DPRINT("Number of entries %d\n",
269 Resource->OwnerThreads[1].a.TableSize);
272 for (i=0; i<Resource->OwnerThreads[1].a.TableSize; i++)
274 if (Resource->OwnerTable[i].OwnerThread == CurrentThread)
276 DPRINT("Thread already owns resource\n");
277 Resource->OwnerTable[i].a.OwnerCount++;
280 if (Resource->OwnerTable[i].OwnerThread == 0)
282 freeEntry = &Resource->OwnerTable[i];
287 DPRINT("Found free entry %x\n", freeEntry);
291 DPRINT("Allocating new entry\n");
293 /* reallocate ownertable with one more entry */
295 ExAllocatePoolWithTag(NonPagedPool,
297 (Resource->OwnerThreads[1].a.TableSize+1),
299 if (freeEntry == NULL)
304 memcpy(freeEntry,Resource->OwnerTable,
305 sizeof(OWNER_ENTRY)*(Resource->OwnerThreads[1].a.TableSize));
306 ExFreePool(Resource->OwnerTable);
307 Resource->OwnerTable=freeEntry;
308 freeEntry=&Resource->OwnerTable[Resource->OwnerThreads[1].a.TableSize];
309 Resource->OwnerThreads[1].a.TableSize++;
311 DPRINT("Creating entry\n");
312 freeEntry->OwnerThread=ExGetCurrentResourceThread();
313 freeEntry->a.OwnerCount=1;
314 Resource->ActiveCount++;
320 ExAcquireResourceSharedLite (
325 * FUNCTION: Acquires the given resource for shared access by the calling
328 * Resource = Points to the resource to acquire
329 * Wait = Is set to TRUE if the caller should be put into wait state
330 * until the resource can be acquired if it cannot be acquired
332 * RETURNS: TRUE, if the resource is acquire
338 DPRINT("ExAcquireResourceSharedLite(Resource %x, Wait %d)\n",
341 KeAcquireSpinLock(&Resource->SpinLock, &oldIrql);
343 /* first, resolve trivial cases */
344 if (Resource->ActiveCount == 0)
346 EiAddSharedOwner(Resource);
347 KeReleaseSpinLock(&Resource->SpinLock, oldIrql);
348 DPRINT("ExAcquireResourceSharedLite() = TRUE\n");
352 if ((Resource->Flag & ResourceOwnedExclusive)
353 && Resource->OwnerThreads[0].OwnerThread==ExGetCurrentResourceThread())
355 /* exclusive, but by same thread : it's ok */
357 * NOTE: Is this correct? Seems the same as ExConvertExclusiveToShared
359 Resource->OwnerThreads[0].a.OwnerCount++;
360 KeReleaseSpinLock(&Resource->SpinLock, oldIrql);
361 DPRINT("ExAcquireResourceSharedLite() = TRUE\n");
365 if ((Resource->Flag & ResourceOwnedExclusive)
366 || Resource->NumberOfExclusiveWaiters)
368 /* exclusive by another thread , or thread waiting for exclusive */
371 KeReleaseSpinLock(&Resource->SpinLock, oldIrql);
372 DPRINT("ExAcquireResourceSharedLite() = FALSE\n");
377 Resource->NumberOfSharedWaiters++;
380 /* wait for the semaphore */
381 KeReleaseSpinLock(&Resource->SpinLock, oldIrql);
382 KeWaitForSingleObject(Resource->SharedWaiters,0, KernelMode, FALSE, NULL);
383 KeAcquireSpinLock(&Resource->SpinLock, &oldIrql);
384 /* the spin lock was released we must check again */
386 while ((Resource->Flag & ResourceOwnedExclusive)
387 || Resource->NumberOfExclusiveWaiters);
388 Resource->NumberOfSharedWaiters--;
392 EiAddSharedOwner(Resource);
393 KeReleaseSpinLock(&Resource->SpinLock, oldIrql);
394 DPRINT("ExAcquireResourceSharedLite() = TRUE\n");
400 ExConvertExclusiveToSharedLite (
404 * FUNCTION: Converts a given resource from acquired for exclusive access
405 * to acquire for shared access
407 * Resource = Points to the resource for which the access should be
409 * NOTES: Caller must be running at IRQL < DISPATCH_LEVEL
415 DPRINT("ExConvertExclusiveToSharedLite(Resource %x)\n", Resource);
417 KeAcquireSpinLock(&Resource->SpinLock, &oldIrql);
419 oldWaiters = Resource->NumberOfSharedWaiters;
421 if (!(Resource->Flag & ResourceOwnedExclusive))
423 /* Might not be what the caller expects, better bug check */
425 KeReleaseSpinLock(&Resource->SpinLock, oldIrql);
429 //transfer infos from entry 0 to entry 1 and erase entry 0
430 Resource->OwnerThreads[1].OwnerThread=Resource->OwnerThreads[0].OwnerThread;
431 Resource->OwnerThreads[1].a.OwnerCount=Resource->OwnerThreads[0].a.OwnerCount;
432 Resource->OwnerThreads[0].OwnerThread=0;
433 Resource->OwnerThreads[0].a.OwnerCount=0;
434 /* erase exclusive flag */
435 Resource->Flag &= (~ResourceOwnedExclusive);
436 /* if no shared waiters, that's all */
439 KeReleaseSpinLock(&Resource->SpinLock, oldIrql);
443 /* else, awake the waiters */
444 KeReleaseSemaphore(Resource->SharedWaiters,0,oldWaiters,0);
445 KeReleaseSpinLock(&Resource->SpinLock, oldIrql);
446 DPRINT("ExConvertExclusiveToSharedLite() finished\n");
447 #else /* !LIBCAPTIVE */
448 /* no waiters allower for libcaptive */
450 #endif /* LIBCAPTIVE */
457 ExDisableResourceBoostLite (
461 Resource->Flag |= ResourceDisableBoost;
464 #endif /* LIBCAPTIVE */
468 ExGetExclusiveWaiterCount (
472 return(Resource->NumberOfExclusiveWaiters);
478 ExAcquireSharedStarveExclusive (
483 * FUNCTION: Acquires a given resource for shared access without waiting
484 * for any pending attempts to acquire exclusive access to the
487 * Resource = Points to the resource to be acquired for shared access
488 * Wait = Is set to TRUE if the caller will wait until the resource
489 * becomes available when access can't be granted immediately
490 * RETURNS: TRUE if the requested access is granted. The routine returns
491 * FALSE if the input Wait is FALSE and shared access can't be
492 * granted immediately
497 DPRINT("ExAcquireSharedStarveExclusive(Resource %x, Wait %d)\n",
500 KeAcquireSpinLock(&Resource->SpinLock, &oldIrql);
502 /* no owner, it's easy */
503 if (Resource->ActiveCount == 0)
505 Resource->OwnerThreads[1].OwnerThread=ExGetCurrentResourceThread();
506 Resource->OwnerThreads[1].a.OwnerCount=1;
507 Resource->ActiveCount=1;
508 KeReleaseSpinLock(&Resource->SpinLock, oldIrql);
509 DPRINT("ExAcquireSharedStarveExclusive() = TRUE\n");
513 if ((Resource->Flag & ResourceOwnedExclusive)
514 && Resource->OwnerThreads[0].OwnerThread==ExGetCurrentResourceThread())
516 /* exclusive, but by same thread : it's ok */
517 Resource->OwnerThreads[0].a.OwnerCount++;
518 KeReleaseSpinLock(&Resource->SpinLock, oldIrql);
519 DPRINT("ExAcquireSharedStarveExclusive() = TRUE\n");
523 if (Resource->Flag & ResourceOwnedExclusive)
525 /* exclusive by another thread */
528 KeReleaseSpinLock(&Resource->SpinLock, oldIrql);
529 DPRINT("ExAcquireSharedStarveExclusive() = FALSE\n");
534 Resource->NumberOfSharedWaiters++;
535 /* wait for the semaphore */
536 KeReleaseSpinLock(&Resource->SpinLock, oldIrql);
537 KeWaitForSingleObject(Resource->SharedWaiters,0,0,0,0);
538 KeAcquireSpinLock(&Resource->SpinLock, &oldIrql);
539 Resource->NumberOfSharedWaiters--;
542 EiAddSharedOwner(Resource);
543 KeReleaseSpinLock(&Resource->SpinLock, oldIrql);
544 DPRINT("ExAcquireSharedStarveExclusive() = TRUE\n");
551 ExAcquireSharedWaitForExclusive (
556 return(ExAcquireResourceSharedLite(Resource,Wait));
567 return(ExDeleteResourceLite(Resource));
570 #endif /* LIBCAPTIVE */
574 ExDeleteResourceLite (
578 DPRINT("ExDeleteResourceLite(Resource %x)\n", Resource);
579 if (Resource->OwnerTable) ExFreePool(Resource->OwnerTable);
580 if (Resource->SharedWaiters) ExFreePool(Resource->SharedWaiters);
581 if (Resource->ExclusiveWaiters) ExFreePool(Resource->ExclusiveWaiters);
582 return(STATUS_SUCCESS);;
588 ExGetSharedWaiterCount (
592 return(Resource->NumberOfSharedWaiters);
599 ExInitializeResource (
603 return(ExInitializeResourceLite(Resource));
606 #endif /* LIBCAPTIVE */
609 ExInitializeResourceLite (PERESOURCE Resource)
611 DPRINT("ExInitializeResourceLite(Resource %x)\n", Resource);
612 memset(Resource,0,sizeof(ERESOURCE));
613 Resource->NumberOfSharedWaiters = 0;
614 Resource->NumberOfExclusiveWaiters = 0;
615 KeInitializeSpinLock(&Resource->SpinLock);
617 Resource->ExclusiveWaiters =
618 ExAllocatePoolWithTag(NonPagedPool, sizeof(KEVENT), TAG_EXCLUSIVE_LOCK);
619 KeInitializeEvent(Resource->ExclusiveWaiters,
620 SynchronizationEvent,
622 Resource->SharedWaiters =
623 ExAllocatePoolWithTag(NonPagedPool ,sizeof(KSEMAPHORE), TAG_SHARED_SEM);
624 KeInitializeSemaphore(Resource->SharedWaiters,0,0x7fffffff);
625 Resource->ActiveCount = 0;
631 ExIsResourceAcquiredExclusiveLite (
635 * FUNCTION: Returns whether the current thread has exclusive access to
638 * Resource = Points to the resource to be queried
639 * RETURNS: TRUE if the caller has exclusive access to the resource,
643 return((Resource->Flag & ResourceOwnedExclusive)
644 && Resource->OwnerThreads[0].OwnerThread==ExGetCurrentResourceThread());
649 ExIsResourceAcquiredSharedLite (
653 * FUNCTION: Returns whether the current thread has shared access to a given
656 * Resource = Points to the resource to be queried
657 * RETURNS: The number of times the caller has acquired shared access to the
662 if (Resource->OwnerThreads[0].OwnerThread == ExGetCurrentResourceThread())
664 return(Resource->OwnerThreads[0].a.OwnerCount);
666 if (Resource->OwnerThreads[1].OwnerThread == ExGetCurrentResourceThread())
668 return(Resource->OwnerThreads[1].a.OwnerCount);
670 if (!Resource->OwnerThreads[1].a.TableSize)
674 for (i=0; i<Resource->OwnerThreads[1].a.TableSize; i++)
676 if (Resource->OwnerTable[i].OwnerThread==ExGetCurrentResourceThread())
678 return Resource->OwnerTable[i].a.OwnerCount;
687 ExReinitializeResourceLite (
691 Resource->NumberOfSharedWaiters = 0;
692 Resource->NumberOfExclusiveWaiters = 0;
693 KeInitializeSpinLock(&Resource->SpinLock);
695 KeInitializeEvent(Resource->ExclusiveWaiters,SynchronizationEvent,
697 KeInitializeSemaphore(Resource->SharedWaiters,0,0x7fffffff);
698 Resource->ActiveCount = 0;
699 if (Resource->OwnerTable)
701 ExFreePool(Resource->OwnerTable);
703 Resource->OwnerThreads[0].OwnerThread=0;
704 Resource->OwnerThreads[0].a.OwnerCount=0;
705 Resource->OwnerThreads[1].OwnerThread=0;
706 Resource->OwnerThreads[1].a.OwnerCount=0;
712 ExReleaseResourceLite (
716 return(ExReleaseResourceForThreadLite(Resource,
717 ExGetCurrentResourceThread()));
724 ExReleaseResourceForThread (
726 ERESOURCE_THREAD ResourceThreadId
729 return(ExReleaseResourceForThreadLite(Resource,ResourceThreadId));
732 #endif /* LIBCAPTIVE */
736 ExReleaseResourceForThreadLite (
738 ERESOURCE_THREAD ResourceThreadId
741 * FUNCTION: Releases a resource for the given thread
743 * Resource = Points to the release to release
744 * ResourceThreadId = Identifies the thread that originally acquired
746 * NOTES: Must be running at IRQL < DISPATCH_LEVEL
747 * BUG: We don't support starving exclusive waiters
752 DPRINT("ExReleaseResourceForThreadLite(Resource %x, ResourceThreadId %x)\n",
753 Resource, ResourceThreadId);
755 KeAcquireSpinLock(&Resource->SpinLock, &oldIrql);
757 if (Resource->Flag & ResourceOwnedExclusive)
759 DPRINT("Releasing from exclusive access\n");
761 Resource->OwnerThreads[0].a.OwnerCount--;
762 if (Resource->OwnerThreads[0].a.OwnerCount > 0)
764 KeReleaseSpinLock(&Resource->SpinLock, oldIrql);
765 DPRINT("ExReleaseResourceForThreadLite() finished\n");
769 Resource->OwnerThreads[0].OwnerThread = 0;
770 Resource->ActiveCount--;
771 Resource->Flag &=(~ResourceOwnedExclusive);
772 assert(Resource->ActiveCount == 0);
773 DPRINT("Resource->NumberOfExclusiveWaiters %d\n",
774 Resource->NumberOfExclusiveWaiters);
775 if (Resource->NumberOfExclusiveWaiters)
777 /* get resource to first exclusive waiter */
778 KeSetEvent(Resource->ExclusiveWaiters,
781 KeReleaseSpinLock(&Resource->SpinLock, oldIrql);
782 DPRINT("ExReleaseResourceForThreadLite() finished\n");
785 DPRINT("Resource->NumberOfSharedWaiters %d\n",
786 Resource->NumberOfSharedWaiters);
787 if (Resource->NumberOfSharedWaiters)
790 DPRINT("Releasing semaphore\n");
791 KeReleaseSemaphore(Resource->SharedWaiters,
793 Resource->NumberOfSharedWaiters,
795 #else /* !LIBCAPTIVE */
797 #endif /* !LIBCAPTIVE */
799 KeReleaseSpinLock(&Resource->SpinLock, oldIrql);
800 DPRINT("ExReleaseResourceForThreadLite() finished\n");
804 EiRemoveSharedOwner(Resource, ResourceThreadId);
806 if (Resource->ActiveCount == 0)
808 if (Resource->NumberOfExclusiveWaiters)
810 /* get resource to first exclusive waiter */
811 KeSetEvent(Resource->ExclusiveWaiters,
817 KeReleaseSpinLock(&Resource->SpinLock, oldIrql);
818 DPRINT("ExReleaseResourceForThreadLite() finished\n");
825 ExSetResourceOwnerPointer (
826 IN PERESOURCE Resource,
827 IN PVOID OwnerPointer
833 #endif /* LIBCAPTIVE */