Of course, I forgot to commit the new file in revision 22049...
[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 ULONG_PTR KernelStack;
67 extern unsigned int init_stack_top;
68
69 Thread = ExAllocatePool(NonPagedPool, sizeof(ETHREAD));
70 RtlZeroMemory(Thread, sizeof(ETHREAD));
71 Thread->ThreadsProcess = Process;
72 if (First)
73 {
74 KernelStack = init_stack_top;
75 }
76 else
77 {
78 KernelStack = (ULONG_PTR)MmCreateKernelStack(FALSE) + KERNEL_STACK_SIZE;
79 }
80 KeInitializeThread(&Process->Pcb,
81 &Thread->Tcb,
82 PspSystemThreadStartup,
83 StartRoutine,
84 NULL,
85 NULL,
86 NULL,
87 (PVOID)KernelStack);
88 InitializeListHead(&Thread->IrpList);
89 *ThreadPtr = Thread;
90 return STATUS_SUCCESS;
91 }
92
93 /*
94 * HACK-O-RAMA
95 * Antique vestigial code left alive for the sole purpose of First/Idle Thread
96 * creation until I can merge my fix for properly creating them.
97 */
98 VOID
99 INIT_FUNCTION
100 NTAPI
101 PsInitIdleThread(VOID)
102 {
103 PETHREAD IdleThread;
104 KIRQL oldIrql;
105
106 PsInitializeIdleOrFirstThread(PsIdleProcess,
107 &IdleThread,
108 PsIdleThreadMain,
109 KernelMode,
110 FALSE);
111
112 oldIrql = KeAcquireDispatcherDatabaseLock ();
113 KiUnblockThread(&IdleThread->Tcb, NULL, 0);
114 KeReleaseDispatcherDatabaseLock(oldIrql);
115
116 KeGetCurrentPrcb()->IdleThread = &IdleThread->Tcb;
117 KeSetPriorityThread(&IdleThread->Tcb, LOW_PRIORITY);
118 KeSetAffinityThread(&IdleThread->Tcb, 1 << 0);
119 }