- split logoff and shutdown resources
[reactos.git] / reactos / ntoskrnl / ps / idle.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: ntoskrnl/ps/idle.c
5 * PURPOSE: Using idle time
6 *
7 * PROGRAMMERS: Alex Ionescu (alex@relsoft.net)
8 * David Welch (welch@cwcom.net)
9 */
10
11 /* INCLUDES *****************************************************************/
12
13 #include <ntoskrnl.h>
14 #define NDEBUG
15 #include <internal/debug.h>
16
17 #if defined (ALLOC_PRAGMA)
18 #pragma alloc_text(INIT, PsInitIdleThread)
19 #endif
20
21 /* GLOBALS *******************************************************************/
22
23 extern PEPROCESS PsIdleProcess;
24
25 /* FUNCTIONS *****************************************************************/
26
27 /** System idle thread procedure
28 *
29 */
30 VOID STDCALL
31 PsIdleThreadMain(PVOID Context)
32 {
33 KIRQL oldlvl;
34
35 PKPRCB Prcb = KeGetCurrentPrcb();
36
37 for(;;)
38 {
39 if (Prcb->DpcData[0].DpcQueueDepth > 0)
40 {
41 KeRaiseIrql(DISPATCH_LEVEL,&oldlvl);
42 KiDispatchInterrupt();
43 KeLowerIrql(oldlvl);
44 }
45
46 NtYieldExecution();
47
48 Ke386HaltProcessor();
49 }
50 }
51
52 /*
53 * HACK-O-RAMA
54 * Antique vestigial code left alive for the sole purpose of First/Idle Thread
55 * creation until I can merge my fix for properly creating them.
56 */
57 NTSTATUS
58 NTAPI
59 PsInitializeIdleOrFirstThread(PEPROCESS Process,
60 PETHREAD* ThreadPtr,
61 PKSTART_ROUTINE StartRoutine,
62 KPROCESSOR_MODE AccessMode,
63 BOOLEAN First)
64 {
65 PETHREAD Thread;
66 PVOID KernelStack;
67 extern unsigned int init_stack;
68
69 Thread = ExAllocatePool(NonPagedPool, sizeof(ETHREAD));
70 RtlZeroMemory(Thread, sizeof(ETHREAD));
71 Thread->ThreadsProcess = Process;
72 if (First)
73 {
74 KernelStack = (PVOID)init_stack;
75 }
76 else
77 {
78 KernelStack = MmCreateKernelStack(FALSE);
79 }
80 KeInitializeThread(&Process->Pcb,
81 &Thread->Tcb,
82 PspSystemThreadStartup,
83 StartRoutine,
84 NULL,
85 NULL,
86 NULL,
87 KernelStack);
88 Thread->Tcb.ApcQueueable = TRUE;
89 InitializeListHead(&Thread->IrpList);
90 *ThreadPtr = Thread;
91 return STATUS_SUCCESS;
92 }
93
94 /*
95 * HACK-O-RAMA
96 * Antique vestigial code left alive for the sole purpose of First/Idle Thread
97 * creation until I can merge my fix for properly creating them.
98 */
99 VOID
100 INIT_FUNCTION
101 NTAPI
102 PsInitIdleThread(VOID)
103 {
104 PETHREAD IdleThread;
105 KIRQL oldIrql;
106
107 PsInitializeIdleOrFirstThread(PsIdleProcess,
108 &IdleThread,
109 PsIdleThreadMain,
110 KernelMode,
111 FALSE);
112
113 oldIrql = KeAcquireDispatcherDatabaseLock ();
114 KiUnblockThread(&IdleThread->Tcb, NULL, 0);
115 KeReleaseDispatcherDatabaseLock(oldIrql);
116
117 KeGetCurrentPrcb()->IdleThread = &IdleThread->Tcb;
118 KeSetPriorityThread(&IdleThread->Tcb, LOW_PRIORITY);
119 KeSetAffinityThread(&IdleThread->Tcb, 1 << 0);
120 }