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
);
442 if (!NT_SUCCESS(Status
))
443 DPRINT1("CSR: NtClose failed, we are going to ASSERT, Status = 0x%08lx; [%02x,%02x] Process: 0x%p; ThreadHandle: 0x%p\n",
444 Status
, CsrThread
->ClientId
.UniqueProcess
, CsrThread
->ClientId
.UniqueThread
, CsrProcess
, CsrThread
->ThreadHandle
);
446 ASSERT(NT_SUCCESS(Status
));
449 /* De-allocate the CSR Thread Object */
450 CsrDeallocateThread(CsrThread
);
452 /* Remove a reference from the process */
453 CsrDereferenceProcess(CsrProcess
);
457 * @name CsrLockedDereferenceThread
459 * The CsrLockedDereferenceThread dereferences a CSR Thread while the
460 * Process Lock is already being held.
463 * Pointer to the CSR Thread to be dereferenced.
467 * @remarks This routine will return with the Process Lock held.
472 CsrLockedDereferenceThread(IN PCSR_THREAD CsrThread
)
476 /* Decrease reference count */
477 LockCount
= --CsrThread
->ReferenceCount
;
478 ASSERT(LockCount
>= 0);
481 /* Call the generic cleanup code */
482 CsrAcquireProcessLock();
483 CsrThreadRefcountZero(CsrThread
);
488 /* PUBLIC FUNCTIONS ***********************************************************/
491 * @name CsrAddStaticServerThread
494 * The CsrAddStaticServerThread routine adds a new CSR Thread to the
495 * CSR Server Process (CsrRootProcess).
498 * Handle to an existing NT Thread to which to associate this
502 * Pointer to the Client ID structure of the NT Thread to associate
503 * with this CSR Thread.
506 * Initial CSR Thread Flags to associate to this CSR Thread. Usually
507 * CsrThreadIsServerThread.
509 * @return Pointer to the newly allocated CSR Thread.
516 CsrAddStaticServerThread(IN HANDLE hThread
,
517 IN PCLIENT_ID ClientId
,
518 IN ULONG ThreadFlags
)
520 PCSR_THREAD CsrThread
;
523 CsrAcquireProcessLock();
525 /* Allocate the Server Thread */
526 CsrThread
= CsrAllocateThread(CsrRootProcess
);
529 /* Setup the Object */
530 CsrThread
->ThreadHandle
= hThread
;
531 ProtectHandle(hThread
);
532 CsrThread
->ClientId
= *ClientId
;
533 CsrThread
->Flags
= ThreadFlags
;
535 /* Insert it into the Thread List */
536 InsertTailList(&CsrRootProcess
->ThreadList
, &CsrThread
->Link
);
538 /* Increment the thread count */
539 CsrRootProcess
->ThreadCount
++;
543 DPRINT1("CsrAddStaticServerThread: alloc failed for thread 0x%x\n", hThread
);
546 /* Release the Process Lock and return */
547 CsrReleaseProcessLock();
552 * @name CsrCreateRemoteThread
555 * The CsrCreateRemoteThread routine creates a CSR Thread object for
556 * an NT Thread which is not part of the current NT Process.
559 * Handle to an existing NT Thread to which to associate this
563 * Pointer to the Client ID structure of the NT Thread to associate
564 * with this CSR Thread.
566 * @return STATUS_SUCCESS in case of success, STATUS_UNSUCCESSFUL otherwise.
573 CsrCreateRemoteThread(IN HANDLE hThread
,
574 IN PCLIENT_ID ClientId
)
578 PCSR_THREAD CsrThread
;
579 PCSR_PROCESS CsrProcess
;
580 KERNEL_USER_TIMES KernelTimes
;
582 /* Get the Thread Create Time */
583 Status
= NtQueryInformationThread(hThread
,
588 if (!NT_SUCCESS(Status
))
590 DPRINT1("Failed to query thread times: %lx\n", Status
);
594 /* Lock the Owner Process */
595 Status
= CsrLockProcessByClientId(ClientId
->UniqueProcess
, &CsrProcess
);
596 if (!NT_SUCCESS(Status
))
598 DPRINT1("No known process for %lx\n", ClientId
->UniqueProcess
);
602 /* Make sure the thread didn't terminate */
603 if (KernelTimes
.ExitTime
.QuadPart
)
605 /* Unlock the process and return */
606 CsrUnlockProcess(CsrProcess
);
607 DPRINT1("Dead thread: %I64x\n", KernelTimes
.ExitTime
.QuadPart
);
608 return STATUS_THREAD_IS_TERMINATING
;
611 /* Allocate a CSR Thread Structure */
612 CsrThread
= CsrAllocateThread(CsrProcess
);
615 DPRINT1("CSRSRV: %s: out of memory!\n", __FUNCTION__
);
616 CsrUnlockProcess(CsrProcess
);
617 return STATUS_NO_MEMORY
;
620 /* Duplicate the Thread Handle */
621 Status
= NtDuplicateObject(NtCurrentProcess(),
627 DUPLICATE_SAME_ACCESS
);
629 if (!NT_SUCCESS(Status
))
631 DPRINT1("Thread duplication failed: %lx\n", Status
);
632 ThreadHandle
= hThread
;
635 /* Save the data we have */
636 CsrThread
->CreateTime
= KernelTimes
.CreateTime
;
637 CsrThread
->ClientId
= *ClientId
;
638 CsrThread
->ThreadHandle
= ThreadHandle
;
639 ProtectHandle(ThreadHandle
);
640 CsrThread
->Flags
= 0;
642 /* Insert the Thread into the Process */
643 Status
= CsrInsertThread(CsrProcess
, CsrThread
);
644 if (!NT_SUCCESS(Status
))
647 if (CsrThread
->ThreadHandle
!= hThread
) NtClose(CsrThread
->ThreadHandle
);
648 CsrUnlockProcess(CsrProcess
);
649 CsrDeallocateThread(CsrThread
);
653 /* Release the lock and return */
654 CsrUnlockProcess(CsrProcess
);
655 return STATUS_SUCCESS
;
659 * @name CsrCreateThread
662 * The CsrCreateThread routine creates a CSR Thread object for an NT Thread.
665 * Pointer to the CSR Process which will contain the CSR Thread.
668 * Handle to an existing NT Thread to which to associate this
672 * Pointer to the Client ID structure of the NT Thread to associate
673 * with this CSR Thread.
675 * @return STATUS_SUCCESS in case of success, STATUS_UNSUCCESSFUL otherwise.
682 CsrCreateThread(IN PCSR_PROCESS CsrProcess
,
684 IN PCLIENT_ID ClientId
,
685 IN BOOLEAN HaveClient
)
688 PCSR_THREAD CsrThread
, CurrentThread
;
689 PCSR_PROCESS CurrentProcess
;
690 CLIENT_ID CurrentCid
;
691 KERNEL_USER_TIMES KernelTimes
;
695 /* Get the current thread and CID */
696 CurrentThread
= CsrGetClientThread();
697 CurrentCid
= CurrentThread
->ClientId
;
699 /* Acquire the Process Lock */
700 CsrAcquireProcessLock();
702 /* Get the current Process and make sure the Thread is valid with this CID */
703 CurrentThread
= CsrLocateThreadByClientId(&CurrentProcess
, &CurrentCid
);
705 /* Something is wrong if we get an empty thread back */
708 DPRINT1("CSRSRV: %s: invalid thread!\n", __FUNCTION__
);
709 CsrReleaseProcessLock();
710 return STATUS_THREAD_IS_TERMINATING
;
715 /* Acquire the Process Lock */
716 CsrAcquireProcessLock();
719 /* Get the Thread Create Time */
720 Status
= NtQueryInformationThread(hThread
,
725 if (!NT_SUCCESS(Status
))
727 CsrReleaseProcessLock();
731 /* Allocate a CSR Thread Structure */
732 CsrThread
= CsrAllocateThread(CsrProcess
);
735 DPRINT1("CSRSRV: %s: out of memory!\n", __FUNCTION__
);
736 CsrReleaseProcessLock();
737 return STATUS_NO_MEMORY
;
740 /* Save the data we have */
741 CsrThread
->CreateTime
= KernelTimes
.CreateTime
;
742 CsrThread
->ClientId
= *ClientId
;
743 CsrThread
->ThreadHandle
= hThread
;
744 ProtectHandle(hThread
);
745 CsrThread
->Flags
= 0;
747 /* Insert the Thread into the Process */
748 Status
= CsrInsertThread(CsrProcess
, CsrThread
);
749 if (!NT_SUCCESS(Status
))
752 CsrUnlockProcess(CsrProcess
);
753 CsrDeallocateThread(CsrThread
);
757 /* Release the lock and return */
758 CsrReleaseProcessLock();
760 return STATUS_SUCCESS
;
764 * @name CsrDereferenceThread
767 * The CsrDereferenceThread routine removes a reference from a CSR Thread.
770 * Pointer to the CSR Thread to dereference.
774 * @remarks If the reference count has reached zero (ie: the CSR Thread has
775 * no more active references), it will be deleted.
780 CsrDereferenceThread(IN PCSR_THREAD CsrThread
)
782 /* Acquire process lock */
783 CsrAcquireProcessLock();
785 /* Decrease reference count */
786 ASSERT(CsrThread
->ReferenceCount
> 0);
787 if ((--CsrThread
->ReferenceCount
) == 0)
789 /* Call the generic cleanup code */
790 CsrThreadRefcountZero(CsrThread
);
794 /* Just release the lock */
795 CsrReleaseProcessLock();
800 * @name CsrDestroyThread
803 * The CsrDestroyThread routine destroys the CSR Thread corresponding to
807 * Pointer to the Client ID Structure corresponding to the CSR
808 * Thread which is about to be destroyed.
810 * @return STATUS_SUCCESS in case of success, STATUS_THREAD_IS_TERMINATING
811 * if the CSR Thread is already terminating.
818 CsrDestroyThread(IN PCLIENT_ID Cid
)
820 CLIENT_ID ClientId
= *Cid
;
821 PCSR_THREAD CsrThread
;
822 PCSR_PROCESS CsrProcess
;
825 CsrAcquireProcessLock();
827 /* Find the thread */
828 CsrThread
= CsrLocateThreadByClientId(&CsrProcess
,
831 /* Make sure we got one back, and that it's not already gone */
832 if (!CsrThread
|| (CsrThread
->Flags
& CsrThreadTerminated
))
834 /* Release the lock and return failure */
835 CsrReleaseProcessLock();
836 return STATUS_THREAD_IS_TERMINATING
;
839 /* Set the terminated flag */
840 CsrThread
->Flags
|= CsrThreadTerminated
;
842 /* Acquire the Wait Lock */
843 CsrAcquireWaitLock();
845 /* Do we have an active wait block? */
846 if (CsrThread
->WaitBlock
)
848 /* Notify waiters of termination */
849 CsrNotifyWaitBlock(CsrThread
->WaitBlock
,
853 CsrProcessTerminating
,
857 /* Release the Wait Lock */
858 CsrReleaseWaitLock();
860 /* Dereference the thread */
861 CsrLockedDereferenceThread(CsrThread
);
863 /* Release the Process Lock and return success */
864 CsrReleaseProcessLock();
865 return STATUS_SUCCESS
;
869 * @name CsrExecServerThread
872 * The CsrExecServerThread routine creates an NT Thread and then
873 * initializes a CSR Thread for it.
875 * @param ThreadHandler
876 * Pointer to the thread's startup routine.
879 * Initial CSR Thread Flags to set to the CSR Thread.
881 * @return STATUS_SUCCESS in case of success, STATUS_UNSUCCESSFUL otherwise.
883 * @remarks This routine is similar to CsrAddStaticServerThread, but it
884 * also creates an NT Thread instead of expecting one to already
890 CsrExecServerThread(IN PVOID ThreadHandler
,
893 PCSR_THREAD CsrThread
;
898 /* Acquire process lock */
899 CsrAcquireProcessLock();
901 /* Allocate a CSR Thread in the Root Process */
902 ASSERT(CsrRootProcess
!= NULL
);
903 CsrThread
= CsrAllocateThread(CsrRootProcess
);
907 CsrReleaseProcessLock();
908 return STATUS_NO_MEMORY
;
911 /* Create the Thread */
912 Status
= RtlCreateUserThread(NtCurrentProcess(),
922 if (!NT_SUCCESS(Status
))
925 CsrDeallocateThread(CsrThread
);
926 CsrReleaseProcessLock();
930 /* Setup the Thread Object */
931 CsrThread
->ThreadHandle
= hThread
;
932 ProtectHandle(hThread
);
933 CsrThread
->ClientId
= ClientId
;
934 CsrThread
->Flags
= Flags
;
936 /* Insert it into the Thread List */
937 InsertHeadList(&CsrRootProcess
->ThreadList
, &CsrThread
->Link
);
939 /* Increase the thread count */
940 CsrRootProcess
->ThreadCount
++;
943 CsrReleaseProcessLock();
948 * @name CsrLockThreadByClientId
951 * The CsrLockThreadByClientId routine locks the CSR Thread corresponding
952 * to the given Thread ID and optionally returns it.
955 * Thread ID corresponding to the CSR Thread which will be locked.
958 * Optional pointer to a CSR Thread pointer which will hold the
959 * CSR Thread corresponding to the given Thread ID.
961 * @return STATUS_SUCCESS in case of success, STATUS_UNSUCCESSFUL otherwise.
963 * @remarks Locking a CSR Thread is defined as acquiring an extra
964 * reference to it and returning with the Process Lock held.
969 CsrLockThreadByClientId(IN HANDLE Tid
,
970 OUT PCSR_THREAD
*CsrThread
)
972 PLIST_ENTRY NextEntry
;
973 PCSR_THREAD CurrentThread
= NULL
;
974 NTSTATUS Status
= STATUS_UNSUCCESSFUL
;
977 /* Acquire the lock */
978 CsrAcquireProcessLock();
981 ASSERT(CsrThread
!= NULL
);
984 /* Convert to Hash */
985 i
= CsrHashThread(Tid
);
987 /* Setup the List Pointers */
988 NextEntry
= CsrThreadHashTable
[i
].Flink
;
991 while (NextEntry
!= &CsrThreadHashTable
[i
])
994 CurrentThread
= CONTAINING_RECORD(NextEntry
, CSR_THREAD
, HashLinks
);
996 /* Check for TID Match */
997 if ((CurrentThread
->ClientId
.UniqueThread
== Tid
) &&
998 (CurrentThread
->Flags
& CsrThreadTerminated
) == 0)
1000 /* Get out of here */
1004 /* Move to the next entry */
1005 NextEntry
= NextEntry
->Flink
;
1008 /* Nothing found if we got back to the list */
1009 if (NextEntry
== &CsrThreadHashTable
[i
]) CurrentThread
= NULL
;
1011 /* Did the loop find something? */
1014 /* Reference the found thread */
1015 Status
= STATUS_SUCCESS
;
1016 CsrLockedReferenceThread(CurrentThread
);
1017 *CsrThread
= CurrentThread
;
1021 /* Nothing found, release the lock */
1022 Status
= STATUS_UNSUCCESSFUL
;
1023 CsrReleaseProcessLock();
1026 /* Return the status */
1031 * @name CsrReferenceThread
1034 * The CsrReferenceThread routine increases the active reference count of
1038 * Pointer to the CSR Thread whose reference count will be increased.
1042 * @remarks Do not use this routine if the Process Lock is already held.
1047 CsrReferenceThread(IN PCSR_THREAD CsrThread
)
1049 /* Acquire process lock */
1050 CsrAcquireProcessLock();
1053 ASSERT((CsrThread
->Flags
& CsrThreadTerminated
) == 0);
1054 ASSERT(CsrThread
->ReferenceCount
!= 0);
1056 /* Increment reference count */
1057 CsrThread
->ReferenceCount
++;
1059 /* Release the lock */
1060 CsrReleaseProcessLock();
1064 * @name CsrUnlockThread
1067 * The CsrUnlockThread undoes a previous CsrLockThreadByClientId operation.
1070 * Pointer to a previously locked CSR Thread.
1072 * @return STATUS_SUCCESS.
1074 * @remarks This routine must be called with the Process Lock held.
1079 CsrUnlockThread(IN PCSR_THREAD CsrThread
)
1081 /* Dereference the Thread */
1082 ASSERT(ProcessStructureListLocked());
1083 CsrLockedDereferenceThread(CsrThread
);
1085 /* Release the lock and return */
1086 CsrReleaseProcessLock();
1087 return STATUS_SUCCESS
;