[CSRSRV][CONSRV][USERSRV]: Display complementary redundant information to try to...
[reactos.git] / reactos / subsystems / win32 / csrsrv / thredsup.c
1 /*
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)
8 */
9
10 /* INCLUDES *******************************************************************/
11
12 #include <srv.h>
13
14 #define NDEBUG
15 #include <debug.h>
16
17 #define CsrHashThread(t) (HandleToUlong(t) % NUMBER_THREAD_HASH_BUCKETS)
18
19 /* GLOBALS ********************************************************************/
20
21 LIST_ENTRY CsrThreadHashTable[NUMBER_THREAD_HASH_BUCKETS];
22
23
24 /* PRIVATE FUNCTIONS **********************************************************/
25
26 /*++
27 * @name ProtectHandle
28 * @implemented NT5.2
29 *
30 * The ProtectHandle routine protects an object handle against closure.
31 *
32 * @return TRUE or FALSE.
33 *
34 * @remarks None.
35 *
36 *--*/
37 BOOLEAN
38 NTAPI
39 ProtectHandle(IN HANDLE ObjectHandle)
40 {
41 NTSTATUS Status;
42 OBJECT_HANDLE_ATTRIBUTE_INFORMATION HandleInfo;
43
44 /* Query current state */
45 Status = NtQueryObject(ObjectHandle,
46 ObjectHandleFlagInformation,
47 &HandleInfo,
48 sizeof(HandleInfo),
49 NULL);
50 if (NT_SUCCESS(Status))
51 {
52 /* Enable protect from close */
53 HandleInfo.ProtectFromClose = TRUE;
54 Status = NtSetInformationObject(ObjectHandle,
55 ObjectHandleFlagInformation,
56 &HandleInfo,
57 sizeof(HandleInfo));
58 if (NT_SUCCESS(Status)) return TRUE;
59 }
60
61 /* We failed to or set the state */
62 return FALSE;
63 }
64
65 /*++
66 * @name UnProtectHandle
67 * @implemented NT5.2
68 *
69 * The UnProtectHandle routine unprotects an object handle against closure.
70 *
71 * @return TRUE or FALSE.
72 *
73 * @remarks None.
74 *
75 *--*/
76 BOOLEAN
77 NTAPI
78 UnProtectHandle(IN HANDLE ObjectHandle)
79 {
80 NTSTATUS Status;
81 OBJECT_HANDLE_ATTRIBUTE_INFORMATION HandleInfo;
82
83 /* Query current state */
84 Status = NtQueryObject(ObjectHandle,
85 ObjectHandleFlagInformation,
86 &HandleInfo,
87 sizeof(HandleInfo),
88 NULL);
89 if (NT_SUCCESS(Status))
90 {
91 /* Disable protect from close */
92 HandleInfo.ProtectFromClose = FALSE;
93 Status = NtSetInformationObject(ObjectHandle,
94 ObjectHandleFlagInformation,
95 &HandleInfo,
96 sizeof(HandleInfo));
97 if (NT_SUCCESS(Status)) return TRUE;
98 }
99
100 /* We failed to or set the state */
101 return FALSE;
102 }
103
104 /*++
105 * @name CsrAllocateThread
106 *
107 * The CsrAllocateThread routine allocates a new CSR Thread object.
108 *
109 * @param CsrProcess
110 * Pointer to the CSR Process which will contain this CSR Thread.
111 *
112 * @return Pointer to the newly allocated CSR Thread.
113 *
114 * @remarks None.
115 *
116 *--*/
117 PCSR_THREAD
118 NTAPI
119 CsrAllocateThread(IN PCSR_PROCESS CsrProcess)
120 {
121 PCSR_THREAD CsrThread;
122
123 /* Allocate the structure */
124 CsrThread = RtlAllocateHeap(CsrHeap, HEAP_ZERO_MEMORY, sizeof(CSR_THREAD));
125 if (!CsrThread) return NULL;
126
127 /* Reference the Thread and Process */
128 CsrLockedReferenceThread(CsrThread);
129 CsrLockedReferenceProcess(CsrProcess);
130
131 /* Set the Parent Process */
132 CsrThread->Process = CsrProcess;
133
134 /* Return Thread */
135 return CsrThread;
136 }
137
138 /*++
139 * @name CsrLockedReferenceThread
140 *
141 * The CsrLockedReferenceThread references a CSR Thread while the
142 * Process Lock is already being held.
143 *
144 * @param CsrThread
145 * Pointer to the CSR Thread to be referenced.
146 *
147 * @return None.
148 *
149 * @remarks This routine will return with the Process Lock held.
150 *
151 *--*/
152 VOID
153 NTAPI
154 CsrLockedReferenceThread(IN PCSR_THREAD CsrThread)
155 {
156 /* Increment the reference count */
157 ++CsrThread->ReferenceCount;
158 }
159
160 /*++
161 * @name CsrLocateThreadByClientId
162 *
163 * The CsrLocateThreadByClientId routine locates the CSR Thread and,
164 * optionally, its parent CSR Process, corresponding to a Client ID.
165 *
166 * @param Process
167 * Optional pointer to a CSR Process pointer which will contain
168 * the CSR Thread's parent.
169 *
170 * @param ClientId
171 * Pointer to a Client ID structure containing the Unique Thread ID
172 * to look up.
173 *
174 * @return Pointer to the CSR Thread corresponding to this CID, or NULL if
175 * none was found.
176 *
177 * @remarks None.
178 *
179 *--*/
180 PCSR_THREAD
181 NTAPI
182 CsrLocateThreadByClientId(OUT PCSR_PROCESS *Process OPTIONAL,
183 IN PCLIENT_ID ClientId)
184 {
185 ULONG i;
186 PLIST_ENTRY ListHead, NextEntry;
187 PCSR_THREAD FoundThread;
188 // ASSERT(ProcessStructureListLocked());
189
190 if (Process) *Process = NULL;
191
192 /* Hash the Thread */
193 i = CsrHashThread(ClientId->UniqueThread);
194
195 /* Set the list pointers */
196 ListHead = &CsrThreadHashTable[i];
197 NextEntry = ListHead->Flink;
198
199 /* Star the loop */
200 while (NextEntry != ListHead)
201 {
202 /* Get the thread */
203 FoundThread = CONTAINING_RECORD(NextEntry, CSR_THREAD, HashLinks);
204
205 /* Move to the next entry */
206 NextEntry = NextEntry->Flink;
207
208 /* Compare the CID */
209 // if (*(PULONGLONG)&FoundThread->ClientId == *(PULONGLONG)ClientId)
210 if ( FoundThread->ClientId.UniqueProcess == ClientId->UniqueProcess &&
211 FoundThread->ClientId.UniqueThread == ClientId->UniqueThread )
212 {
213 /* Match found, return the process */
214 if (Process) *Process = FoundThread->Process;
215
216 /* Return thread too */
217 return FoundThread;
218 }
219 }
220
221 /* Nothing found */
222 return NULL;
223 }
224
225 /*++
226 * @name CsrLocateThreadInProcess
227 *
228 * The CsrLocateThreadInProcess routine locates the CSR Thread
229 * corresponding to a Client ID inside a specific CSR Process.
230 *
231 * @param Process
232 * Optional pointer to the CSR Process which contains the CSR Thread
233 * that will be looked up.
234 *
235 * @param ClientId
236 * Pointer to a Client ID structure containing the Unique Thread ID
237 * to look up.
238 *
239 * @return Pointer to the CSR Thread corresponding to this CID, or NULL if
240 * none was found.
241 *
242 * @remarks If the CsrProcess argument is NULL, the lookup will be done inside
243 * CsrRootProcess.
244 *
245 *--*/
246 PCSR_THREAD
247 NTAPI
248 CsrLocateThreadInProcess(IN PCSR_PROCESS CsrProcess OPTIONAL,
249 IN PCLIENT_ID Cid)
250 {
251 PLIST_ENTRY ListHead, NextEntry;
252 PCSR_THREAD FoundThread = NULL;
253
254 /* Use the Root Process if none was specified */
255 if (!CsrProcess) CsrProcess = CsrRootProcess;
256
257 /* Save the List pointers */
258 ListHead = &CsrProcess->ThreadList;
259 NextEntry = ListHead->Flink;
260
261 /* Start the Loop */
262 while (NextEntry != ListHead)
263 {
264 /* Get Thread Entry */
265 FoundThread = CONTAINING_RECORD(NextEntry, CSR_THREAD, Link);
266
267 /* Check for TID Match */
268 if (FoundThread->ClientId.UniqueThread == Cid->UniqueThread) break;
269
270 /* Move to the next entry */
271 NextEntry = NextEntry->Flink;
272 }
273
274 /* Return what we found */
275 return FoundThread;
276 }
277
278 /*++
279 * @name CsrInsertThread
280 *
281 * The CsrInsertThread routine inserts a CSR Thread into its parent's
282 * Thread List and into the Thread Hash Table.
283 *
284 * @param Process
285 * Pointer to the CSR Process containing this CSR Thread.
286 *
287 * @param Thread
288 * Pointer to the CSR Thread to be inserted.
289 *
290 * @return None.
291 *
292 * @remarks None.
293 *
294 *--*/
295 NTSTATUS
296 NTAPI
297 CsrInsertThread(IN PCSR_PROCESS Process,
298 IN PCSR_THREAD Thread)
299 {
300 ULONG i;
301 NTSTATUS Status;
302 ULONG ThreadInfo;
303 // ASSERT(ProcessStructureListLocked());
304
305 /* Make sure the thread isn't already dead by the time we got this */
306 Status = NtQueryInformationThread(Thread->ThreadHandle,
307 ThreadIsTerminated,
308 &ThreadInfo,
309 sizeof(ThreadInfo),
310 NULL);
311 if (!NT_SUCCESS(Status)) return Status;
312 if (ThreadInfo) return STATUS_THREAD_IS_TERMINATING;
313
314 /* Insert it into the Regular List */
315 InsertTailList(&Process->ThreadList, &Thread->Link);
316
317 /* Increase Thread Count */
318 Process->ThreadCount++;
319
320 /* Hash the Thread */
321 i = CsrHashThread(Thread->ClientId.UniqueThread);
322
323 /* Insert it there too */
324 InsertHeadList(&CsrThreadHashTable[i], &Thread->HashLinks);
325 return STATUS_SUCCESS;
326 }
327
328 /*++
329 * @name CsrDeallocateThread
330 *
331 * The CsrDeallocateThread frees the memory associated with a CSR Thread.
332 *
333 * @param CsrThread
334 * Pointer to the CSR Thread to be freed.
335 *
336 * @return None.
337 *
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.
341 *
342 *--*/
343 VOID
344 NTAPI
345 CsrDeallocateThread(IN PCSR_THREAD CsrThread)
346 {
347 /* Free the process object from the heap */
348 // ASSERT(CsrThread->WaitBlock == NULL);
349 RtlFreeHeap(CsrHeap, 0, CsrThread);
350 }
351
352 /*++
353 * @name CsrRemoveThread
354 *
355 * The CsrRemoveThread function undoes a CsrInsertThread operation and
356 * removes the CSR Thread from the the Hash Table and Thread List.
357 *
358 * @param CsrThread
359 * Pointer to the CSR Thread to remove.
360 *
361 * @return None.
362 *
363 * @remarks If this CSR Thread is the last one inside a CSR Process, the
364 * parent will be dereferenced and the CsrProcessLastThreadTerminated
365 * flag will be set.
366 *
367 * After executing this routine, the CSR Thread will have the
368 * CsrThreadInTermination flag set.
369 *
370 *--*/
371 VOID
372 NTAPI
373 CsrRemoveThread(IN PCSR_THREAD CsrThread)
374 {
375 ASSERT(ProcessStructureListLocked());
376
377 /* Remove it from the List */
378 RemoveEntryList(&CsrThread->Link);
379
380 /* Decreate the thread count of the process */
381 CsrThread->Process->ThreadCount--;
382
383 /* Remove it from the Hash List as well */
384 if (CsrThread->HashLinks.Flink) RemoveEntryList(&CsrThread->HashLinks);
385
386 /* Check if this is the last Thread */
387 if (CsrThread->Process->ThreadCount == 0)
388 {
389 /* Check if it's not already been marked for deletion */
390 if ((CsrThread->Process->Flags & CsrProcessLastThreadTerminated) == 0)
391 {
392 /* Let everyone know this process is about to lose the thread */
393 CsrThread->Process->Flags |= CsrProcessLastThreadTerminated;
394
395 /* Reference the Process */
396 CsrLockedDereferenceProcess(CsrThread->Process);
397 }
398 }
399
400 /* Mark the thread for deletion */
401 CsrThread->Flags |= CsrThreadInTermination;
402 }
403
404 /*++
405 * @name CsrThreadRefcountZero
406 *
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.
409 *
410 * @param CsrThread
411 * Pointer to the CSR Thread that is to be deleted.
412 *
413 * @return None.
414 *
415 * @remarks Do not call this routine. It is reserved for the internal
416 * thread management routines when a CSR Thread has lost all
417 * its references.
418 *
419 * This routine is called with the Process Lock held.
420 *
421 *--*/
422 VOID
423 NTAPI
424 CsrThreadRefcountZero(IN PCSR_THREAD CsrThread)
425 {
426 PCSR_PROCESS CsrProcess = CsrThread->Process;
427 NTSTATUS Status;
428 ASSERT(ProcessStructureListLocked());
429
430 /* Remove this thread */
431 CsrRemoveThread(CsrThread);
432
433 /* Release the Process Lock */
434 CsrReleaseProcessLock();
435
436 /* Close the NT Thread Handle */
437 if (CsrThread->ThreadHandle)
438 {
439 UnProtectHandle(CsrThread->ThreadHandle);
440 Status = NtClose(CsrThread->ThreadHandle);
441
442 if (!NT_SUCCESS(Status))
443 DPRINT1("CSR: NtClose failed, we are going to ASSERT, Status = 0x%08lx; P:[0x%x, 0x%x] T:[0x%x, 0x%x] Process: 0x%p; Thread: 0x%p; ThreadHandle: 0x%p\n",
444 Status, CsrProcess->ClientId.UniqueProcess, CsrProcess->ClientId.UniqueThread,
445 CsrThread->ClientId.UniqueProcess, CsrThread->ClientId.UniqueThread,
446 CsrProcess, CsrThread, CsrThread->ThreadHandle);
447
448 ASSERT(NT_SUCCESS(Status));
449 }
450
451 /* De-allocate the CSR Thread Object */
452 CsrDeallocateThread(CsrThread);
453
454 /* Remove a reference from the process */
455 CsrDereferenceProcess(CsrProcess);
456 }
457
458 /*++
459 * @name CsrLockedDereferenceThread
460 *
461 * The CsrLockedDereferenceThread dereferences a CSR Thread while the
462 * Process Lock is already being held.
463 *
464 * @param CsrThread
465 * Pointer to the CSR Thread to be dereferenced.
466 *
467 * @return None.
468 *
469 * @remarks This routine will return with the Process Lock held.
470 *
471 *--*/
472 VOID
473 NTAPI
474 CsrLockedDereferenceThread(IN PCSR_THREAD CsrThread)
475 {
476 LONG LockCount;
477
478 /* Decrease reference count */
479 LockCount = --CsrThread->ReferenceCount;
480 ASSERT(LockCount >= 0);
481 if (LockCount == 0)
482 {
483 /* Call the generic cleanup code */
484 CsrAcquireProcessLock();
485 CsrThreadRefcountZero(CsrThread);
486 }
487 }
488
489
490 /* PUBLIC FUNCTIONS ***********************************************************/
491
492 /*++
493 * @name CsrAddStaticServerThread
494 * @implemented NT4
495 *
496 * The CsrAddStaticServerThread routine adds a new CSR Thread to the
497 * CSR Server Process (CsrRootProcess).
498 *
499 * @param hThread
500 * Handle to an existing NT Thread to which to associate this
501 * CSR Thread.
502 *
503 * @param ClientId
504 * Pointer to the Client ID structure of the NT Thread to associate
505 * with this CSR Thread.
506 *
507 * @param ThreadFlags
508 * Initial CSR Thread Flags to associate to this CSR Thread. Usually
509 * CsrThreadIsServerThread.
510 *
511 * @return Pointer to the newly allocated CSR Thread.
512 *
513 * @remarks None.
514 *
515 *--*/
516 PCSR_THREAD
517 NTAPI
518 CsrAddStaticServerThread(IN HANDLE hThread,
519 IN PCLIENT_ID ClientId,
520 IN ULONG ThreadFlags)
521 {
522 PCSR_THREAD CsrThread;
523
524 /* Get the Lock */
525 CsrAcquireProcessLock();
526
527 /* Allocate the Server Thread */
528 CsrThread = CsrAllocateThread(CsrRootProcess);
529 if (CsrThread)
530 {
531 /* Setup the Object */
532 CsrThread->ThreadHandle = hThread;
533 ProtectHandle(hThread);
534 CsrThread->ClientId = *ClientId;
535 CsrThread->Flags = ThreadFlags;
536
537 /* Insert it into the Thread List */
538 InsertTailList(&CsrRootProcess->ThreadList, &CsrThread->Link);
539
540 /* Increment the thread count */
541 CsrRootProcess->ThreadCount++;
542 }
543 else
544 {
545 DPRINT1("CsrAddStaticServerThread: alloc failed for thread 0x%x\n", hThread);
546 }
547
548 /* Release the Process Lock and return */
549 CsrReleaseProcessLock();
550 return CsrThread;
551 }
552
553 /*++
554 * @name CsrCreateRemoteThread
555 * @implemented NT4
556 *
557 * The CsrCreateRemoteThread routine creates a CSR Thread object for
558 * an NT Thread which is not part of the current NT Process.
559 *
560 * @param hThread
561 * Handle to an existing NT Thread to which to associate this
562 * CSR Thread.
563 *
564 * @param ClientId
565 * Pointer to the Client ID structure of the NT Thread to associate
566 * with this CSR Thread.
567 *
568 * @return STATUS_SUCCESS in case of success, STATUS_UNSUCCESSFUL otherwise.
569 *
570 * @remarks None.
571 *
572 *--*/
573 NTSTATUS
574 NTAPI
575 CsrCreateRemoteThread(IN HANDLE hThread,
576 IN PCLIENT_ID ClientId)
577 {
578 NTSTATUS Status;
579 HANDLE ThreadHandle;
580 PCSR_THREAD CsrThread;
581 PCSR_PROCESS CsrProcess;
582 KERNEL_USER_TIMES KernelTimes;
583
584 /* Get the Thread Create Time */
585 Status = NtQueryInformationThread(hThread,
586 ThreadTimes,
587 &KernelTimes,
588 sizeof(KernelTimes),
589 NULL);
590 if (!NT_SUCCESS(Status))
591 {
592 DPRINT1("Failed to query thread times: %lx\n", Status);
593 return Status;
594 }
595
596 /* Lock the Owner Process */
597 Status = CsrLockProcessByClientId(ClientId->UniqueProcess, &CsrProcess);
598 if (!NT_SUCCESS(Status))
599 {
600 DPRINT1("No known process for %lx\n", ClientId->UniqueProcess);
601 return Status;
602 }
603
604 /* Make sure the thread didn't terminate */
605 if (KernelTimes.ExitTime.QuadPart)
606 {
607 /* Unlock the process and return */
608 CsrUnlockProcess(CsrProcess);
609 DPRINT1("Dead thread: %I64x\n", KernelTimes.ExitTime.QuadPart);
610 return STATUS_THREAD_IS_TERMINATING;
611 }
612
613 /* Allocate a CSR Thread Structure */
614 CsrThread = CsrAllocateThread(CsrProcess);
615 if (!CsrThread)
616 {
617 DPRINT1("CSRSRV: %s: out of memory!\n", __FUNCTION__);
618 CsrUnlockProcess(CsrProcess);
619 return STATUS_NO_MEMORY;
620 }
621
622 /* Duplicate the Thread Handle */
623 Status = NtDuplicateObject(NtCurrentProcess(),
624 hThread,
625 NtCurrentProcess(),
626 &ThreadHandle,
627 0,
628 0,
629 DUPLICATE_SAME_ACCESS);
630 /* Allow failure */
631 if (!NT_SUCCESS(Status))
632 {
633 DPRINT1("Thread duplication failed: %lx\n", Status);
634 ThreadHandle = hThread;
635 }
636
637 /* Save the data we have */
638 CsrThread->CreateTime = KernelTimes.CreateTime;
639 CsrThread->ClientId = *ClientId;
640 CsrThread->ThreadHandle = ThreadHandle;
641 ProtectHandle(ThreadHandle);
642 CsrThread->Flags = 0;
643
644 /* Insert the Thread into the Process */
645 Status = CsrInsertThread(CsrProcess, CsrThread);
646 if (!NT_SUCCESS(Status))
647 {
648 /* Bail out */
649 if (CsrThread->ThreadHandle != hThread) NtClose(CsrThread->ThreadHandle);
650 CsrUnlockProcess(CsrProcess);
651 CsrDeallocateThread(CsrThread);
652 return Status;
653 }
654
655 /* Release the lock and return */
656 CsrUnlockProcess(CsrProcess);
657 return STATUS_SUCCESS;
658 }
659
660 /*++
661 * @name CsrCreateThread
662 * @implemented NT4
663 *
664 * The CsrCreateThread routine creates a CSR Thread object for an NT Thread.
665 *
666 * @param CsrProcess
667 * Pointer to the CSR Process which will contain the CSR Thread.
668 *
669 * @param hThread
670 * Handle to an existing NT Thread to which to associate this
671 * CSR Thread.
672 *
673 * @param ClientId
674 * Pointer to the Client ID structure of the NT Thread to associate
675 * with this CSR Thread.
676 *
677 * @return STATUS_SUCCESS in case of success, STATUS_UNSUCCESSFUL otherwise.
678 *
679 * @remarks None.
680 *
681 *--*/
682 NTSTATUS
683 NTAPI
684 CsrCreateThread(IN PCSR_PROCESS CsrProcess,
685 IN HANDLE hThread,
686 IN PCLIENT_ID ClientId,
687 IN BOOLEAN HaveClient)
688 {
689 NTSTATUS Status;
690 PCSR_THREAD CsrThread, CurrentThread;
691 PCSR_PROCESS CurrentProcess;
692 CLIENT_ID CurrentCid;
693 KERNEL_USER_TIMES KernelTimes;
694
695 if (HaveClient)
696 {
697 /* Get the current thread and CID */
698 CurrentThread = CsrGetClientThread();
699 CurrentCid = CurrentThread->ClientId;
700
701 /* Acquire the Process Lock */
702 CsrAcquireProcessLock();
703
704 /* Get the current Process and make sure the Thread is valid with this CID */
705 CurrentThread = CsrLocateThreadByClientId(&CurrentProcess, &CurrentCid);
706
707 /* Something is wrong if we get an empty thread back */
708 if (!CurrentThread)
709 {
710 DPRINT1("CSRSRV: %s: invalid thread!\n", __FUNCTION__);
711 CsrReleaseProcessLock();
712 return STATUS_THREAD_IS_TERMINATING;
713 }
714 }
715 else
716 {
717 /* Acquire the Process Lock */
718 CsrAcquireProcessLock();
719 }
720
721 /* Get the Thread Create Time */
722 Status = NtQueryInformationThread(hThread,
723 ThreadTimes,
724 &KernelTimes,
725 sizeof(KernelTimes),
726 NULL);
727 if (!NT_SUCCESS(Status))
728 {
729 CsrReleaseProcessLock();
730 return Status;
731 }
732
733 /* Allocate a CSR Thread Structure */
734 CsrThread = CsrAllocateThread(CsrProcess);
735 if (!CsrThread)
736 {
737 DPRINT1("CSRSRV: %s: out of memory!\n", __FUNCTION__);
738 CsrReleaseProcessLock();
739 return STATUS_NO_MEMORY;
740 }
741
742 /* Save the data we have */
743 CsrThread->CreateTime = KernelTimes.CreateTime;
744 CsrThread->ClientId = *ClientId;
745 CsrThread->ThreadHandle = hThread;
746 ProtectHandle(hThread);
747 CsrThread->Flags = 0;
748
749 /* Insert the Thread into the Process */
750 Status = CsrInsertThread(CsrProcess, CsrThread);
751 if (!NT_SUCCESS(Status))
752 {
753 /* Bail out */
754 CsrUnlockProcess(CsrProcess);
755 CsrDeallocateThread(CsrThread);
756 return Status;
757 }
758
759 /* Release the lock and return */
760 CsrReleaseProcessLock();
761
762 return STATUS_SUCCESS;
763 }
764
765 /*++
766 * @name CsrDereferenceThread
767 * @implemented NT4
768 *
769 * The CsrDereferenceThread routine removes a reference from a CSR Thread.
770 *
771 * @param CsrThread
772 * Pointer to the CSR Thread to dereference.
773 *
774 * @return None.
775 *
776 * @remarks If the reference count has reached zero (ie: the CSR Thread has
777 * no more active references), it will be deleted.
778 *
779 *--*/
780 VOID
781 NTAPI
782 CsrDereferenceThread(IN PCSR_THREAD CsrThread)
783 {
784 /* Acquire process lock */
785 CsrAcquireProcessLock();
786
787 /* Decrease reference count */
788 ASSERT(CsrThread->ReferenceCount > 0);
789 if ((--CsrThread->ReferenceCount) == 0)
790 {
791 /* Call the generic cleanup code */
792 CsrThreadRefcountZero(CsrThread);
793 }
794 else
795 {
796 /* Just release the lock */
797 CsrReleaseProcessLock();
798 }
799 }
800
801 /*++
802 * @name CsrDestroyThread
803 * @implemented NT4
804 *
805 * The CsrDestroyThread routine destroys the CSR Thread corresponding to
806 * a given Thread ID.
807 *
808 * @param Cid
809 * Pointer to the Client ID Structure corresponding to the CSR
810 * Thread which is about to be destroyed.
811 *
812 * @return STATUS_SUCCESS in case of success, STATUS_THREAD_IS_TERMINATING
813 * if the CSR Thread is already terminating.
814 *
815 * @remarks None.
816 *
817 *--*/
818 NTSTATUS
819 NTAPI
820 CsrDestroyThread(IN PCLIENT_ID Cid)
821 {
822 CLIENT_ID ClientId = *Cid;
823 PCSR_THREAD CsrThread;
824 PCSR_PROCESS CsrProcess;
825
826 /* Acquire lock */
827 CsrAcquireProcessLock();
828
829 /* Find the thread */
830 CsrThread = CsrLocateThreadByClientId(&CsrProcess,
831 &ClientId);
832
833 /* Make sure we got one back, and that it's not already gone */
834 if (!CsrThread || (CsrThread->Flags & CsrThreadTerminated))
835 {
836 /* Release the lock and return failure */
837 CsrReleaseProcessLock();
838 return STATUS_THREAD_IS_TERMINATING;
839 }
840
841 /* Set the terminated flag */
842 CsrThread->Flags |= CsrThreadTerminated;
843
844 /* Acquire the Wait Lock */
845 CsrAcquireWaitLock();
846
847 /* Do we have an active wait block? */
848 if (CsrThread->WaitBlock)
849 {
850 /* Notify waiters of termination */
851 CsrNotifyWaitBlock(CsrThread->WaitBlock,
852 NULL,
853 NULL,
854 NULL,
855 CsrProcessTerminating,
856 TRUE);
857 }
858
859 /* Release the Wait Lock */
860 CsrReleaseWaitLock();
861
862 /* Dereference the thread */
863 CsrLockedDereferenceThread(CsrThread);
864
865 /* Release the Process Lock and return success */
866 CsrReleaseProcessLock();
867 return STATUS_SUCCESS;
868 }
869
870 /*++
871 * @name CsrExecServerThread
872 * @implemented NT4
873 *
874 * The CsrExecServerThread routine creates an NT Thread and then
875 * initializes a CSR Thread for it.
876 *
877 * @param ThreadHandler
878 * Pointer to the thread's startup routine.
879 *
880 * @param Flags
881 * Initial CSR Thread Flags to set to the CSR Thread.
882 *
883 * @return STATUS_SUCCESS in case of success, STATUS_UNSUCCESSFUL otherwise.
884 *
885 * @remarks This routine is similar to CsrAddStaticServerThread, but it
886 * also creates an NT Thread instead of expecting one to already
887 * exist.
888 *
889 *--*/
890 NTSTATUS
891 NTAPI
892 CsrExecServerThread(IN PVOID ThreadHandler,
893 IN ULONG Flags)
894 {
895 PCSR_THREAD CsrThread;
896 HANDLE hThread;
897 CLIENT_ID ClientId;
898 NTSTATUS Status;
899
900 /* Acquire process lock */
901 CsrAcquireProcessLock();
902
903 /* Allocate a CSR Thread in the Root Process */
904 ASSERT(CsrRootProcess != NULL);
905 CsrThread = CsrAllocateThread(CsrRootProcess);
906 if (!CsrThread)
907 {
908 /* Fail */
909 CsrReleaseProcessLock();
910 return STATUS_NO_MEMORY;
911 }
912
913 /* Create the Thread */
914 Status = RtlCreateUserThread(NtCurrentProcess(),
915 NULL,
916 FALSE,
917 0,
918 0,
919 0,
920 ThreadHandler,
921 NULL,
922 &hThread,
923 &ClientId);
924 if (!NT_SUCCESS(Status))
925 {
926 /* Fail */
927 CsrDeallocateThread(CsrThread);
928 CsrReleaseProcessLock();
929 return Status;
930 }
931
932 /* Setup the Thread Object */
933 CsrThread->ThreadHandle = hThread;
934 ProtectHandle(hThread);
935 CsrThread->ClientId = ClientId;
936 CsrThread->Flags = Flags;
937
938 /* Insert it into the Thread List */
939 InsertHeadList(&CsrRootProcess->ThreadList, &CsrThread->Link);
940
941 /* Increase the thread count */
942 CsrRootProcess->ThreadCount++;
943
944 /* Return */
945 CsrReleaseProcessLock();
946 return Status;
947 }
948
949 /*++
950 * @name CsrLockThreadByClientId
951 * @implemented NT4
952 *
953 * The CsrLockThreadByClientId routine locks the CSR Thread corresponding
954 * to the given Thread ID and optionally returns it.
955 *
956 * @param Tid
957 * Thread ID corresponding to the CSR Thread which will be locked.
958 *
959 * @param CsrThread
960 * Optional pointer to a CSR Thread pointer which will hold the
961 * CSR Thread corresponding to the given Thread ID.
962 *
963 * @return STATUS_SUCCESS in case of success, STATUS_UNSUCCESSFUL otherwise.
964 *
965 * @remarks Locking a CSR Thread is defined as acquiring an extra
966 * reference to it and returning with the Process Lock held.
967 *
968 *--*/
969 NTSTATUS
970 NTAPI
971 CsrLockThreadByClientId(IN HANDLE Tid,
972 OUT PCSR_THREAD *CsrThread)
973 {
974 PLIST_ENTRY NextEntry;
975 PCSR_THREAD CurrentThread = NULL;
976 NTSTATUS Status = STATUS_UNSUCCESSFUL;
977 ULONG i;
978
979 /* Acquire the lock */
980 CsrAcquireProcessLock();
981
982 /* Assume failure */
983 ASSERT(CsrThread != NULL);
984 *CsrThread = NULL;
985
986 /* Convert to Hash */
987 i = CsrHashThread(Tid);
988
989 /* Setup the List Pointers */
990 NextEntry = CsrThreadHashTable[i].Flink;
991
992 /* Start Loop */
993 while (NextEntry != &CsrThreadHashTable[i])
994 {
995 /* Get the Thread */
996 CurrentThread = CONTAINING_RECORD(NextEntry, CSR_THREAD, HashLinks);
997
998 /* Check for TID Match */
999 if ((CurrentThread->ClientId.UniqueThread == Tid) &&
1000 (CurrentThread->Flags & CsrThreadTerminated) == 0)
1001 {
1002 /* Get out of here */
1003 break;
1004 }
1005
1006 /* Move to the next entry */
1007 NextEntry = NextEntry->Flink;
1008 }
1009
1010 /* Nothing found if we got back to the list */
1011 if (NextEntry == &CsrThreadHashTable[i]) CurrentThread = NULL;
1012
1013 /* Did the loop find something? */
1014 if (CurrentThread)
1015 {
1016 /* Reference the found thread */
1017 Status = STATUS_SUCCESS;
1018 CsrLockedReferenceThread(CurrentThread);
1019 *CsrThread = CurrentThread;
1020 }
1021 else
1022 {
1023 /* Nothing found, release the lock */
1024 Status = STATUS_UNSUCCESSFUL;
1025 CsrReleaseProcessLock();
1026 }
1027
1028 /* Return the status */
1029 return Status;
1030 }
1031
1032 /*++
1033 * @name CsrReferenceThread
1034 * @implemented NT4
1035 *
1036 * The CsrReferenceThread routine increases the active reference count of
1037 * a CSR Thread.
1038 *
1039 * @param CsrThread
1040 * Pointer to the CSR Thread whose reference count will be increased.
1041 *
1042 * @return None.
1043 *
1044 * @remarks Do not use this routine if the Process Lock is already held.
1045 *
1046 *--*/
1047 VOID
1048 NTAPI
1049 CsrReferenceThread(IN PCSR_THREAD CsrThread)
1050 {
1051 /* Acquire process lock */
1052 CsrAcquireProcessLock();
1053
1054 /* Sanity checks */
1055 ASSERT((CsrThread->Flags & CsrThreadTerminated) == 0);
1056 ASSERT(CsrThread->ReferenceCount != 0);
1057
1058 /* Increment reference count */
1059 CsrThread->ReferenceCount++;
1060
1061 /* Release the lock */
1062 CsrReleaseProcessLock();
1063 }
1064
1065 /*++
1066 * @name CsrUnlockThread
1067 * @implemented NT4
1068 *
1069 * The CsrUnlockThread undoes a previous CsrLockThreadByClientId operation.
1070 *
1071 * @param CsrThread
1072 * Pointer to a previously locked CSR Thread.
1073 *
1074 * @return STATUS_SUCCESS.
1075 *
1076 * @remarks This routine must be called with the Process Lock held.
1077 *
1078 *--*/
1079 NTSTATUS
1080 NTAPI
1081 CsrUnlockThread(IN PCSR_THREAD CsrThread)
1082 {
1083 /* Dereference the Thread */
1084 ASSERT(ProcessStructureListLocked());
1085 CsrLockedDereferenceThread(CsrThread);
1086
1087 /* Release the lock and return */
1088 CsrReleaseProcessLock();
1089 return STATUS_SUCCESS;
1090 }
1091
1092 /* EOF */