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");
552 ExAcquireSharedWaitForExclusive (
557 return(ExAcquireResourceSharedLite(Resource,Wait));
566 return(ExDeleteResourceLite(Resource));
569 #endif /* LIBCAPTIVE */
573 ExDeleteResourceLite (
577 DPRINT("ExDeleteResourceLite(Resource %x)\n", Resource);
578 if (Resource->OwnerTable) ExFreePool(Resource->OwnerTable);
579 if (Resource->SharedWaiters) ExFreePool(Resource->SharedWaiters);
580 if (Resource->ExclusiveWaiters) ExFreePool(Resource->ExclusiveWaiters);
581 return(STATUS_SUCCESS);;
587 ExGetSharedWaiterCount (
591 return(Resource->NumberOfSharedWaiters);
598 ExInitializeResource (
602 return(ExInitializeResourceLite(Resource));
605 #endif /* LIBCAPTIVE */
608 ExInitializeResourceLite (PERESOURCE Resource)
610 DPRINT("ExInitializeResourceLite(Resource %x)\n", Resource);
611 memset(Resource,0,sizeof(ERESOURCE));
612 Resource->NumberOfSharedWaiters = 0;
613 Resource->NumberOfExclusiveWaiters = 0;
614 KeInitializeSpinLock(&Resource->SpinLock);
616 Resource->ExclusiveWaiters =
617 ExAllocatePoolWithTag(NonPagedPool, sizeof(KEVENT), TAG_EXCLUSIVE_LOCK);
618 KeInitializeEvent(Resource->ExclusiveWaiters,
619 SynchronizationEvent,
621 Resource->SharedWaiters =
622 ExAllocatePoolWithTag(NonPagedPool ,sizeof(KSEMAPHORE), TAG_SHARED_SEM);
623 KeInitializeSemaphore(Resource->SharedWaiters,0,0x7fffffff);
624 Resource->ActiveCount = 0;
630 ExIsResourceAcquiredExclusiveLite (
634 * FUNCTION: Returns whether the current thread has exclusive access to
637 * Resource = Points to the resource to be queried
638 * RETURNS: TRUE if the caller has exclusive access to the resource,
642 return((Resource->Flag & ResourceOwnedExclusive)
643 && Resource->OwnerThreads[0].OwnerThread==ExGetCurrentResourceThread());
648 ExIsResourceAcquiredSharedLite (
652 * FUNCTION: Returns whether the current thread has shared access to a given
655 * Resource = Points to the resource to be queried
656 * RETURNS: The number of times the caller has acquired shared access to the
661 if (Resource->OwnerThreads[0].OwnerThread == ExGetCurrentResourceThread())
663 return(Resource->OwnerThreads[0].a.OwnerCount);
665 if (Resource->OwnerThreads[1].OwnerThread == ExGetCurrentResourceThread())
667 return(Resource->OwnerThreads[1].a.OwnerCount);
669 if (!Resource->OwnerThreads[1].a.TableSize)
673 for (i=0; i<Resource->OwnerThreads[1].a.TableSize; i++)
675 if (Resource->OwnerTable[i].OwnerThread==ExGetCurrentResourceThread())
677 return Resource->OwnerTable[i].a.OwnerCount;
686 ExReinitializeResourceLite (
690 Resource->NumberOfSharedWaiters = 0;
691 Resource->NumberOfExclusiveWaiters = 0;
692 KeInitializeSpinLock(&Resource->SpinLock);
694 KeInitializeEvent(Resource->ExclusiveWaiters,SynchronizationEvent,
696 KeInitializeSemaphore(Resource->SharedWaiters,0,0x7fffffff);
697 Resource->ActiveCount = 0;
698 if (Resource->OwnerTable)
700 ExFreePool(Resource->OwnerTable);
702 Resource->OwnerThreads[0].OwnerThread=0;
703 Resource->OwnerThreads[0].a.OwnerCount=0;
704 Resource->OwnerThreads[1].OwnerThread=0;
705 Resource->OwnerThreads[1].a.OwnerCount=0;
711 ExReleaseResourceLite (
715 return(ExReleaseResourceForThreadLite(Resource,
716 ExGetCurrentResourceThread()));
723 ExReleaseResourceForThread (
725 ERESOURCE_THREAD ResourceThreadId
728 return(ExReleaseResourceForThreadLite(Resource,ResourceThreadId));
731 #endif /* LIBCAPTIVE */
735 ExReleaseResourceForThreadLite (
737 ERESOURCE_THREAD ResourceThreadId
740 * FUNCTION: Releases a resource for the given thread
742 * Resource = Points to the release to release
743 * ResourceThreadId = Identifies the thread that originally acquired
745 * NOTES: Must be running at IRQL < DISPATCH_LEVEL
746 * BUG: We don't support starving exclusive waiters
751 DPRINT("ExReleaseResourceForThreadLite(Resource %x, ResourceThreadId %x)\n",
752 Resource, ResourceThreadId);
754 KeAcquireSpinLock(&Resource->SpinLock, &oldIrql);
756 if (Resource->Flag & ResourceOwnedExclusive)
758 DPRINT("Releasing from exclusive access\n");
760 Resource->OwnerThreads[0].a.OwnerCount--;
761 if (Resource->OwnerThreads[0].a.OwnerCount > 0)
763 KeReleaseSpinLock(&Resource->SpinLock, oldIrql);
764 DPRINT("ExReleaseResourceForThreadLite() finished\n");
768 Resource->OwnerThreads[0].OwnerThread = 0;
769 Resource->ActiveCount--;
770 Resource->Flag &=(~ResourceOwnedExclusive);
771 assert(Resource->ActiveCount == 0);
772 DPRINT("Resource->NumberOfExclusiveWaiters %d\n",
773 Resource->NumberOfExclusiveWaiters);
774 if (Resource->NumberOfExclusiveWaiters)
776 /* get resource to first exclusive waiter */
777 KeSetEvent(Resource->ExclusiveWaiters,
780 KeReleaseSpinLock(&Resource->SpinLock, oldIrql);
781 DPRINT("ExReleaseResourceForThreadLite() finished\n");
784 DPRINT("Resource->NumberOfSharedWaiters %d\n",
785 Resource->NumberOfSharedWaiters);
786 if (Resource->NumberOfSharedWaiters)
789 DPRINT("Releasing semaphore\n");
790 KeReleaseSemaphore(Resource->SharedWaiters,
792 Resource->NumberOfSharedWaiters,
794 #else /* !LIBCAPTIVE */
796 #endif /* !LIBCAPTIVE */
798 KeReleaseSpinLock(&Resource->SpinLock, oldIrql);
799 DPRINT("ExReleaseResourceForThreadLite() finished\n");
803 EiRemoveSharedOwner(Resource, ResourceThreadId);
805 if (Resource->ActiveCount == 0)
807 if (Resource->NumberOfExclusiveWaiters)
809 /* get resource to first exclusive waiter */
810 KeSetEvent(Resource->ExclusiveWaiters,
816 KeReleaseSpinLock(&Resource->SpinLock, oldIrql);
817 DPRINT("ExReleaseResourceForThreadLite() finished\n");
824 ExSetResourceOwnerPointer (
825 IN PERESOURCE Resource,
826 IN PVOID OwnerPointer
832 #endif /* LIBCAPTIVE */