2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS Client/Server Runtime SubSystem
4 * FILE: subsystems/win32/csrsrv/thredsup.c
5 * PURPOSE: CSR Server DLL Thread Management
6 * PROGRAMMERS: ReactOS Portable Systems Group
7 * Alex Ionescu (alex@relsoft.net)
10 /* INCLUDES *******************************************************************/
17 #define CsrHashThread(t) (HandleToUlong(t) % NUMBER_THREAD_HASH_BUCKETS)
19 /* GLOBALS ********************************************************************/
21 LIST_ENTRY CsrThreadHashTable
[NUMBER_THREAD_HASH_BUCKETS
];
24 /* PRIVATE FUNCTIONS **********************************************************/
30 * The ProtectHandle routine protects an object handle against closure.
32 * @return TRUE or FALSE.
39 ProtectHandle(IN HANDLE ObjectHandle
)
42 OBJECT_HANDLE_ATTRIBUTE_INFORMATION HandleInfo
;
44 /* Query current state */
45 Status
= NtQueryObject(ObjectHandle
,
46 ObjectHandleFlagInformation
,
50 if (NT_SUCCESS(Status
))
52 /* Enable protect from close */
53 HandleInfo
.ProtectFromClose
= TRUE
;
54 Status
= NtSetInformationObject(ObjectHandle
,
55 ObjectHandleFlagInformation
,
58 if (NT_SUCCESS(Status
)) return TRUE
;
61 /* We failed to or set the state */
66 * @name UnProtectHandle
69 * The UnProtectHandle routine unprotects an object handle against closure.
71 * @return TRUE or FALSE.
78 UnProtectHandle(IN HANDLE ObjectHandle
)
81 OBJECT_HANDLE_ATTRIBUTE_INFORMATION HandleInfo
;
83 /* Query current state */
84 Status
= NtQueryObject(ObjectHandle
,
85 ObjectHandleFlagInformation
,
89 if (NT_SUCCESS(Status
))
91 /* Disable protect from close */
92 HandleInfo
.ProtectFromClose
= FALSE
;
93 Status
= NtSetInformationObject(ObjectHandle
,
94 ObjectHandleFlagInformation
,
97 if (NT_SUCCESS(Status
)) return TRUE
;
100 /* We failed to or set the state */
105 * @name CsrAllocateThread
107 * The CsrAllocateThread routine allocates a new CSR Thread object.
110 * Pointer to the CSR Process which will contain this CSR Thread.
112 * @return Pointer to the newly allocated CSR Thread.
119 CsrAllocateThread(IN PCSR_PROCESS CsrProcess
)
121 PCSR_THREAD CsrThread
;
123 /* Allocate the structure */
124 CsrThread
= RtlAllocateHeap(CsrHeap
, HEAP_ZERO_MEMORY
, sizeof(CSR_THREAD
));
125 if (!CsrThread
) return NULL
;
127 /* Reference the Thread and Process */
128 CsrLockedReferenceThread(CsrThread
);
129 CsrLockedReferenceProcess(CsrProcess
);
131 /* Set the Parent Process */
132 CsrThread
->Process
= CsrProcess
;
139 * @name CsrLockedReferenceThread
141 * The CsrLockedReferenceThread references a CSR Thread while the
142 * Process Lock is already being held.
145 * Pointer to the CSR Thread to be referenced.
149 * @remarks This routine will return with the Process Lock held.
154 CsrLockedReferenceThread(IN PCSR_THREAD CsrThread
)
156 /* Increment the reference count */
157 ++CsrThread
->ReferenceCount
;
161 * @name CsrLocateThreadByClientId
163 * The CsrLocateThreadByClientId routine locates the CSR Thread and,
164 * optionally, its parent CSR Process, corresponding to a Client ID.
167 * Optional pointer to a CSR Process pointer which will contain
168 * the CSR Thread's parent.
171 * Pointer to a Client ID structure containing the Unique Thread ID
174 * @return Pointer to the CSR Thread corresponding to this CID, or NULL if
182 CsrLocateThreadByClientId(OUT PCSR_PROCESS
*Process OPTIONAL
,
183 IN PCLIENT_ID ClientId
)
186 PLIST_ENTRY ListHead
, NextEntry
;
187 PCSR_THREAD FoundThread
;
188 // ASSERT(ProcessStructureListLocked());
190 if (Process
) *Process
= NULL
;
192 /* Hash the Thread */
193 i
= CsrHashThread(ClientId
->UniqueThread
);
195 /* Set the list pointers */
196 ListHead
= &CsrThreadHashTable
[i
];
197 NextEntry
= ListHead
->Flink
;
200 while (NextEntry
!= ListHead
)
203 FoundThread
= CONTAINING_RECORD(NextEntry
, CSR_THREAD
, HashLinks
);
205 /* Move to the next entry */
206 NextEntry
= NextEntry
->Flink
;
208 /* Compare the CID */
209 // if (*(PULONGLONG)&FoundThread->ClientId == *(PULONGLONG)ClientId)
210 if ( FoundThread
->ClientId
.UniqueProcess
== ClientId
->UniqueProcess
&&
211 FoundThread
->ClientId
.UniqueThread
== ClientId
->UniqueThread
)
213 /* Match found, return the process */
214 if (Process
) *Process
= FoundThread
->Process
;
216 /* Return thread too */
226 * @name CsrLocateThreadInProcess
228 * The CsrLocateThreadInProcess routine locates the CSR Thread
229 * corresponding to a Client ID inside a specific CSR Process.
232 * Optional pointer to the CSR Process which contains the CSR Thread
233 * that will be looked up.
236 * Pointer to a Client ID structure containing the Unique Thread ID
239 * @return Pointer to the CSR Thread corresponding to this CID, or NULL if
242 * @remarks If the CsrProcess argument is NULL, the lookup will be done inside
248 CsrLocateThreadInProcess(IN PCSR_PROCESS CsrProcess OPTIONAL
,
251 PLIST_ENTRY ListHead
, NextEntry
;
252 PCSR_THREAD FoundThread
= NULL
;
254 /* Use the Root Process if none was specified */
255 if (!CsrProcess
) CsrProcess
= CsrRootProcess
;
257 /* Save the List pointers */
258 ListHead
= &CsrProcess
->ThreadList
;
259 NextEntry
= ListHead
->Flink
;
262 while (NextEntry
!= ListHead
)
264 /* Get Thread Entry */
265 FoundThread
= CONTAINING_RECORD(NextEntry
, CSR_THREAD
, Link
);
267 /* Check for TID Match */
268 if (FoundThread
->ClientId
.UniqueThread
== Cid
->UniqueThread
) break;
270 /* Move to the next entry */
271 NextEntry
= NextEntry
->Flink
;
274 /* Return what we found */
279 * @name CsrInsertThread
281 * The CsrInsertThread routine inserts a CSR Thread into its parent's
282 * Thread List and into the Thread Hash Table.
285 * Pointer to the CSR Process containing this CSR Thread.
288 * Pointer to the CSR Thread to be inserted.
297 CsrInsertThread(IN PCSR_PROCESS Process
,
298 IN PCSR_THREAD Thread
)
303 // ASSERT(ProcessStructureListLocked());
305 /* Make sure the thread isn't already dead by the time we got this */
306 Status
= NtQueryInformationThread(Thread
->ThreadHandle
,
311 if (!NT_SUCCESS(Status
)) return Status
;
312 if (ThreadInfo
) return STATUS_THREAD_IS_TERMINATING
;
314 /* Insert it into the Regular List */
315 InsertTailList(&Process
->ThreadList
, &Thread
->Link
);
317 /* Increase Thread Count */
318 Process
->ThreadCount
++;
320 /* Hash the Thread */
321 i
= CsrHashThread(Thread
->ClientId
.UniqueThread
);
323 /* Insert it there too */
324 InsertHeadList(&CsrThreadHashTable
[i
], &Thread
->HashLinks
);
325 return STATUS_SUCCESS
;
329 * @name CsrDeallocateThread
331 * The CsrDeallocateThread frees the memory associated with a CSR Thread.
334 * Pointer to the CSR Thread to be freed.
338 * @remarks Do not call this routine. It is reserved for the internal
339 * thread management routines when a CSR Thread has been cleanly
340 * dereferenced and killed.
345 CsrDeallocateThread(IN PCSR_THREAD CsrThread
)
347 /* Free the process object from the heap */
348 // ASSERT(CsrThread->WaitBlock == NULL);
349 RtlFreeHeap(CsrHeap
, 0, CsrThread
);
353 * @name CsrRemoveThread
355 * The CsrRemoveThread function undoes a CsrInsertThread operation and
356 * removes the CSR Thread from the the Hash Table and Thread List.
359 * Pointer to the CSR Thread to remove.
363 * @remarks If this CSR Thread is the last one inside a CSR Process, the
364 * parent will be dereferenced and the CsrProcessLastThreadTerminated
367 * After executing this routine, the CSR Thread will have the
368 * CsrThreadInTermination flag set.
373 CsrRemoveThread(IN PCSR_THREAD CsrThread
)
375 ASSERT(ProcessStructureListLocked());
377 /* Remove it from the List */
378 RemoveEntryList(&CsrThread
->Link
);
380 /* Decreate the thread count of the process */
381 CsrThread
->Process
->ThreadCount
--;
383 /* Remove it from the Hash List as well */
384 if (CsrThread
->HashLinks
.Flink
) RemoveEntryList(&CsrThread
->HashLinks
);
386 /* Check if this is the last Thread */
387 if (CsrThread
->Process
->ThreadCount
== 0)
389 /* Check if it's not already been marked for deletion */
390 if ((CsrThread
->Process
->Flags
& CsrProcessLastThreadTerminated
) == 0)
392 /* Let everyone know this process is about to lose the thread */
393 CsrThread
->Process
->Flags
|= CsrProcessLastThreadTerminated
;
395 /* Reference the Process */
396 CsrLockedDereferenceProcess(CsrThread
->Process
);
400 /* Mark the thread for deletion */
401 CsrThread
->Flags
|= CsrThreadInTermination
;
405 * @name CsrThreadRefcountZero
407 * The CsrThreadRefcountZero routine is executed when a CSR Thread has lost
408 * all its active references. It removes and de-allocates the CSR Thread.
411 * Pointer to the CSR Thread that is to be deleted.
415 * @remarks Do not call this routine. It is reserved for the internal
416 * thread management routines when a CSR Thread has lost all
419 * This routine is called with the Process Lock held.
424 CsrThreadRefcountZero(IN PCSR_THREAD CsrThread
)
426 PCSR_PROCESS CsrProcess
= CsrThread
->Process
;
428 ASSERT(ProcessStructureListLocked());
430 /* Remove this thread */
431 CsrRemoveThread(CsrThread
);
433 /* Release the Process Lock */
434 CsrReleaseProcessLock();
436 /* Close the NT Thread Handle */
437 if (CsrThread
->ThreadHandle
)
439 UnProtectHandle(CsrThread
->ThreadHandle
);
440 Status
= NtClose(CsrThread
->ThreadHandle
);
441 ASSERT(NT_SUCCESS(Status
));
444 /* De-allocate the CSR Thread Object */
445 CsrDeallocateThread(CsrThread
);
447 /* Remove a reference from the process */
448 CsrDereferenceProcess(CsrProcess
);
452 * @name CsrLockedDereferenceThread
454 * The CsrLockedDereferenceThread dereferences a CSR Thread while the
455 * Process Lock is already being held.
458 * Pointer to the CSR Thread to be dereferenced.
462 * @remarks This routine will return with the Process Lock held.
467 CsrLockedDereferenceThread(IN PCSR_THREAD CsrThread
)
471 /* Decrease reference count */
472 LockCount
= --CsrThread
->ReferenceCount
;
473 ASSERT(LockCount
>= 0);
476 /* Call the generic cleanup code */
477 CsrAcquireProcessLock();
478 CsrThreadRefcountZero(CsrThread
);
483 /* PUBLIC FUNCTIONS ***********************************************************/
486 * @name CsrAddStaticServerThread
489 * The CsrAddStaticServerThread routine adds a new CSR Thread to the
490 * CSR Server Process (CsrRootProcess).
493 * Handle to an existing NT Thread to which to associate this
497 * Pointer to the Client ID structure of the NT Thread to associate
498 * with this CSR Thread.
501 * Initial CSR Thread Flags to associate to this CSR Thread. Usually
502 * CsrThreadIsServerThread.
504 * @return Pointer to the newly allocated CSR Thread.
511 CsrAddStaticServerThread(IN HANDLE hThread
,
512 IN PCLIENT_ID ClientId
,
513 IN ULONG ThreadFlags
)
515 PCSR_THREAD CsrThread
;
518 CsrAcquireProcessLock();
520 /* Allocate the Server Thread */
521 CsrThread
= CsrAllocateThread(CsrRootProcess
);
524 /* Setup the Object */
525 CsrThread
->ThreadHandle
= hThread
;
526 ProtectHandle(hThread
);
527 CsrThread
->ClientId
= *ClientId
;
528 CsrThread
->Flags
= ThreadFlags
;
530 /* Insert it into the Thread List */
531 InsertTailList(&CsrRootProcess
->ThreadList
, &CsrThread
->Link
);
533 /* Increment the thread count */
534 CsrRootProcess
->ThreadCount
++;
538 DPRINT1("CsrAddStaticServerThread: alloc failed for thread 0x%x\n", hThread
);
541 /* Release the Process Lock and return */
542 CsrReleaseProcessLock();
547 * @name CsrCreateRemoteThread
550 * The CsrCreateRemoteThread routine creates a CSR Thread object for
551 * an NT Thread which is not part of the current NT Process.
554 * Handle to an existing NT Thread to which to associate this
558 * Pointer to the Client ID structure of the NT Thread to associate
559 * with this CSR Thread.
561 * @return STATUS_SUCCESS in case of success, STATUS_UNSUCCESSFUL otherwise.
568 CsrCreateRemoteThread(IN HANDLE hThread
,
569 IN PCLIENT_ID ClientId
)
573 PCSR_THREAD CsrThread
;
574 PCSR_PROCESS CsrProcess
;
575 KERNEL_USER_TIMES KernelTimes
;
577 /* Get the Thread Create Time */
578 Status
= NtQueryInformationThread(hThread
,
583 if (!NT_SUCCESS(Status
))
585 DPRINT1("Failed to query thread times: %lx\n", Status
);
589 /* Lock the Owner Process */
590 Status
= CsrLockProcessByClientId(ClientId
->UniqueProcess
, &CsrProcess
);
591 if (!NT_SUCCESS(Status
))
593 DPRINT1("No known process for %lx\n", ClientId
->UniqueProcess
);
597 /* Make sure the thread didn't terminate */
598 if (KernelTimes
.ExitTime
.QuadPart
)
600 /* Unlock the process and return */
601 CsrUnlockProcess(CsrProcess
);
602 DPRINT1("Dead thread: %I64x\n", KernelTimes
.ExitTime
.QuadPart
);
603 return STATUS_THREAD_IS_TERMINATING
;
606 /* Allocate a CSR Thread Structure */
607 CsrThread
= CsrAllocateThread(CsrProcess
);
610 DPRINT1("CSRSRV: %s: out of memory!\n", __FUNCTION__
);
611 CsrUnlockProcess(CsrProcess
);
612 return STATUS_NO_MEMORY
;
615 /* Duplicate the Thread Handle */
616 Status
= NtDuplicateObject(NtCurrentProcess(),
622 DUPLICATE_SAME_ACCESS
);
624 if (!NT_SUCCESS(Status
))
626 DPRINT1("Thread duplication failed: %lx\n", Status
);
627 ThreadHandle
= hThread
;
630 /* Save the data we have */
631 CsrThread
->CreateTime
= KernelTimes
.CreateTime
;
632 CsrThread
->ClientId
= *ClientId
;
633 CsrThread
->ThreadHandle
= ThreadHandle
;
634 ProtectHandle(ThreadHandle
);
635 CsrThread
->Flags
= 0;
637 /* Insert the Thread into the Process */
638 Status
= CsrInsertThread(CsrProcess
, CsrThread
);
639 if (!NT_SUCCESS(Status
))
642 if (CsrThread
->ThreadHandle
!= hThread
) NtClose(CsrThread
->ThreadHandle
);
643 CsrUnlockProcess(CsrProcess
);
644 CsrDeallocateThread(CsrThread
);
648 /* Release the lock and return */
649 CsrUnlockProcess(CsrProcess
);
650 return STATUS_SUCCESS
;
654 * @name CsrCreateThread
657 * The CsrCreateThread routine creates a CSR Thread object for an NT Thread.
660 * Pointer to the CSR Process which will contain the CSR Thread.
663 * Handle to an existing NT Thread to which to associate this
667 * Pointer to the Client ID structure of the NT Thread to associate
668 * with this CSR Thread.
670 * @return STATUS_SUCCESS in case of success, STATUS_UNSUCCESSFUL otherwise.
677 CsrCreateThread(IN PCSR_PROCESS CsrProcess
,
679 IN PCLIENT_ID ClientId
,
680 IN BOOLEAN HaveClient
)
683 PCSR_THREAD CsrThread
, CurrentThread
;
684 PCSR_PROCESS CurrentProcess
;
685 CLIENT_ID CurrentCid
;
686 KERNEL_USER_TIMES KernelTimes
;
690 /* Get the current thread and CID */
691 CurrentThread
= CsrGetClientThread();
692 CurrentCid
= CurrentThread
->ClientId
;
694 /* Acquire the Process Lock */
695 CsrAcquireProcessLock();
697 /* Get the current Process and make sure the Thread is valid with this CID */
698 CurrentThread
= CsrLocateThreadByClientId(&CurrentProcess
, &CurrentCid
);
700 /* Something is wrong if we get an empty thread back */
703 DPRINT1("CSRSRV: %s: invalid thread!\n", __FUNCTION__
);
704 CsrReleaseProcessLock();
705 return STATUS_THREAD_IS_TERMINATING
;
710 /* Acquire the Process Lock */
711 CsrAcquireProcessLock();
714 /* Get the Thread Create Time */
715 Status
= NtQueryInformationThread(hThread
,
720 if (!NT_SUCCESS(Status
))
722 CsrReleaseProcessLock();
726 /* Allocate a CSR Thread Structure */
727 CsrThread
= CsrAllocateThread(CsrProcess
);
730 DPRINT1("CSRSRV: %s: out of memory!\n", __FUNCTION__
);
731 CsrReleaseProcessLock();
732 return STATUS_NO_MEMORY
;
735 /* Save the data we have */
736 CsrThread
->CreateTime
= KernelTimes
.CreateTime
;
737 CsrThread
->ClientId
= *ClientId
;
738 CsrThread
->ThreadHandle
= hThread
;
739 ProtectHandle(hThread
);
740 CsrThread
->Flags
= 0;
742 /* Insert the Thread into the Process */
743 Status
= CsrInsertThread(CsrProcess
, CsrThread
);
744 if (!NT_SUCCESS(Status
))
747 CsrUnlockProcess(CsrProcess
);
748 CsrDeallocateThread(CsrThread
);
752 /* Release the lock and return */
753 CsrReleaseProcessLock();
755 return STATUS_SUCCESS
;
759 * @name CsrDereferenceThread
762 * The CsrDereferenceThread routine removes a reference from a CSR Thread.
765 * Pointer to the CSR Thread to dereference.
769 * @remarks If the reference count has reached zero (ie: the CSR Thread has
770 * no more active references), it will be deleted.
775 CsrDereferenceThread(IN PCSR_THREAD CsrThread
)
777 /* Acquire process lock */
778 CsrAcquireProcessLock();
780 /* Decrease reference count */
781 ASSERT(CsrThread
->ReferenceCount
> 0);
782 if ((--CsrThread
->ReferenceCount
) == 0)
784 /* Call the generic cleanup code */
785 CsrThreadRefcountZero(CsrThread
);
789 /* Just release the lock */
790 CsrReleaseProcessLock();
795 * @name CsrDestroyThread
798 * The CsrDestroyThread routine destroys the CSR Thread corresponding to
802 * Pointer to the Client ID Structure corresponding to the CSR
803 * Thread which is about to be destroyed.
805 * @return STATUS_SUCCESS in case of success, STATUS_THREAD_IS_TERMINATING
806 * if the CSR Thread is already terminating.
813 CsrDestroyThread(IN PCLIENT_ID Cid
)
815 CLIENT_ID ClientId
= *Cid
;
816 PCSR_THREAD CsrThread
;
817 PCSR_PROCESS CsrProcess
;
820 CsrAcquireProcessLock();
822 /* Find the thread */
823 CsrThread
= CsrLocateThreadByClientId(&CsrProcess
,
826 /* Make sure we got one back, and that it's not already gone */
827 if (!CsrThread
|| (CsrThread
->Flags
& CsrThreadTerminated
))
829 /* Release the lock and return failure */
830 CsrReleaseProcessLock();
831 return STATUS_THREAD_IS_TERMINATING
;
834 /* Set the terminated flag */
835 CsrThread
->Flags
|= CsrThreadTerminated
;
837 /* Acquire the Wait Lock */
838 CsrAcquireWaitLock();
840 /* Do we have an active wait block? */
841 if (CsrThread
->WaitBlock
)
843 /* Notify waiters of termination */
844 CsrNotifyWaitBlock(CsrThread
->WaitBlock
,
848 CsrProcessTerminating
,
852 /* Release the Wait Lock */
853 CsrReleaseWaitLock();
855 /* Dereference the thread */
856 CsrLockedDereferenceThread(CsrThread
);
858 /* Release the Process Lock and return success */
859 CsrReleaseProcessLock();
860 return STATUS_SUCCESS
;
864 * @name CsrExecServerThread
867 * The CsrExecServerThread routine creates an NT Thread and then
868 * initializes a CSR Thread for it.
870 * @param ThreadHandler
871 * Pointer to the thread's startup routine.
874 * Initial CSR Thread Flags to set to the CSR Thread.
876 * @return STATUS_SUCCESS in case of success, STATUS_UNSUCCESSFUL otherwise.
878 * @remarks This routine is similar to CsrAddStaticServerThread, but it
879 * also creates an NT Thread instead of expecting one to already
885 CsrExecServerThread(IN PVOID ThreadHandler
,
888 PCSR_THREAD CsrThread
;
893 /* Acquire process lock */
894 CsrAcquireProcessLock();
896 /* Allocate a CSR Thread in the Root Process */
897 ASSERT(CsrRootProcess
!= NULL
);
898 CsrThread
= CsrAllocateThread(CsrRootProcess
);
902 CsrReleaseProcessLock();
903 return STATUS_NO_MEMORY
;
906 /* Create the Thread */
907 Status
= RtlCreateUserThread(NtCurrentProcess(),
917 if (!NT_SUCCESS(Status
))
920 CsrDeallocateThread(CsrThread
);
921 CsrReleaseProcessLock();
925 /* Setup the Thread Object */
926 CsrThread
->ThreadHandle
= hThread
;
927 ProtectHandle(hThread
);
928 CsrThread
->ClientId
= ClientId
;
929 CsrThread
->Flags
= Flags
;
931 /* Insert it into the Thread List */
932 InsertHeadList(&CsrRootProcess
->ThreadList
, &CsrThread
->Link
);
934 /* Increase the thread count */
935 CsrRootProcess
->ThreadCount
++;
938 CsrReleaseProcessLock();
943 * @name CsrLockThreadByClientId
946 * The CsrLockThreadByClientId routine locks the CSR Thread corresponding
947 * to the given Thread ID and optionally returns it.
950 * Thread ID corresponding to the CSR Thread which will be locked.
953 * Optional pointer to a CSR Thread pointer which will hold the
954 * CSR Thread corresponding to the given Thread ID.
956 * @return STATUS_SUCCESS in case of success, STATUS_UNSUCCESSFUL otherwise.
958 * @remarks Locking a CSR Thread is defined as acquiring an extra
959 * reference to it and returning with the Process Lock held.
964 CsrLockThreadByClientId(IN HANDLE Tid
,
965 OUT PCSR_THREAD
*CsrThread
)
967 PLIST_ENTRY NextEntry
;
968 PCSR_THREAD CurrentThread
= NULL
;
969 NTSTATUS Status
= STATUS_UNSUCCESSFUL
;
972 /* Acquire the lock */
973 CsrAcquireProcessLock();
976 ASSERT(CsrThread
!= NULL
);
979 /* Convert to Hash */
980 i
= CsrHashThread(Tid
);
982 /* Setup the List Pointers */
983 NextEntry
= CsrThreadHashTable
[i
].Flink
;
986 while (NextEntry
!= &CsrThreadHashTable
[i
])
989 CurrentThread
= CONTAINING_RECORD(NextEntry
, CSR_THREAD
, HashLinks
);
991 /* Check for TID Match */
992 if ((CurrentThread
->ClientId
.UniqueThread
== Tid
) &&
993 (CurrentThread
->Flags
& CsrThreadTerminated
) == 0)
995 /* Get out of here */
999 /* Move to the next entry */
1000 NextEntry
= NextEntry
->Flink
;
1003 /* Nothing found if we got back to the list */
1004 if (NextEntry
== &CsrThreadHashTable
[i
]) CurrentThread
= NULL
;
1006 /* Did the loop find something? */
1009 /* Reference the found thread */
1010 Status
= STATUS_SUCCESS
;
1011 CsrLockedReferenceThread(CurrentThread
);
1012 *CsrThread
= CurrentThread
;
1016 /* Nothing found, release the lock */
1017 Status
= STATUS_UNSUCCESSFUL
;
1018 CsrReleaseProcessLock();
1021 /* Return the status */
1026 * @name CsrReferenceThread
1029 * The CsrReferenceThread routine increases the active reference count of
1033 * Pointer to the CSR Thread whose reference count will be increased.
1037 * @remarks Do not use this routine if the Process Lock is already held.
1042 CsrReferenceThread(IN PCSR_THREAD CsrThread
)
1044 /* Acquire process lock */
1045 CsrAcquireProcessLock();
1048 ASSERT((CsrThread
->Flags
& CsrThreadTerminated
) == 0);
1049 ASSERT(CsrThread
->ReferenceCount
!= 0);
1051 /* Increment reference count */
1052 CsrThread
->ReferenceCount
++;
1054 /* Release the lock */
1055 CsrReleaseProcessLock();
1059 * @name CsrUnlockThread
1062 * The CsrUnlockThread undoes a previous CsrLockThreadByClientId operation.
1065 * Pointer to a previously locked CSR Thread.
1067 * @return STATUS_SUCCESS.
1069 * @remarks This routine must be called with the Process Lock held.
1074 CsrUnlockThread(IN PCSR_THREAD CsrThread
)
1076 /* Dereference the Thread */
1077 ASSERT(ProcessStructureListLocked());
1078 CsrLockedDereferenceThread(CsrThread
);
1080 /* Release the lock and return */
1081 CsrReleaseProcessLock();
1082 return STATUS_SUCCESS
;