Big merge: thanks alex and greatlord. Not a complete merge but most
[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 /* FUNCTIONS *****************************************************************/
22
23 /** System idle thread procedure
24 *
25 */
26 VOID STDCALL
27 PsIdleThreadMain(PVOID Context)
28 {
29 KIRQL oldlvl;
30
31 PKPRCB Prcb = KeGetCurrentPrcb();
32
33 for(;;)
34 {
35 if (Prcb->DpcData[0].DpcQueueDepth > 0)
36 {
37 KeRaiseIrql(DISPATCH_LEVEL,&oldlvl);
38 KiDispatchInterrupt();
39 KeLowerIrql(oldlvl);
40 }
41
42 NtYieldExecution();
43
44 #ifdef _M_IX86
45 Ke386HaltProcessor();
46 #elif defined(_M_PPC)
47 for(;;);
48 #endif
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
68 Thread = ExAllocatePool(NonPagedPool, sizeof(ETHREAD));
69 RtlZeroMemory(Thread, sizeof(ETHREAD));
70 Thread->ThreadsProcess = Process;
71 if (First)
72 {
73 KernelStack = P0BootStack;
74 }
75 else
76 {
77 KernelStack = (PVOID)((ULONG_PTR)MmCreateKernelStack(FALSE) +
78 KERNEL_STACK_SIZE);
79 }
80 KeInitializeThread(&Process->Pcb,
81 &Thread->Tcb,
82 PspSystemThreadStartup,
83 StartRoutine,
84 NULL,
85 NULL,
86 NULL,
87 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 KiReadyThread(&IdleThread->Tcb);
114 KeReleaseDispatcherDatabaseLock(oldIrql);
115
116 KeGetCurrentPrcb()->IdleThread = &IdleThread->Tcb;
117 KeSetPriorityThread(&IdleThread->Tcb, LOW_PRIORITY);
118 KeSetAffinityThread(&IdleThread->Tcb, 1 << 0);
119 }