Merge 13159:13510 from trunk
[reactos.git] / reactos / ntoskrnl / ps / idle.c
1 /* $Id$
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/ps/idle.c
6 * PURPOSE: Using idle time
7 *
8 * PROGRAMMERS: David Welch (welch@cwcom.net)
9 */
10
11 /* INCLUDES *****************************************************************/
12
13 #include <ntoskrnl.h>
14 #define NDEBUG
15 #include <internal/debug.h>
16
17 /* GLOBALS *******************************************************************/
18
19 /* FUNCTIONS *****************************************************************/
20
21 /** System idle thread procedure
22 *
23 */
24 VOID STDCALL
25 PsIdleThreadMain(PVOID Context)
26 {
27 KIRQL oldlvl;
28
29 PKPCR Pcr = KeGetCurrentKPCR();
30
31 for(;;)
32 {
33 if (Pcr->PrcbData.DpcData[0].DpcQueueDepth > 0)
34 {
35 KeRaiseIrql(DISPATCH_LEVEL,&oldlvl);
36 KiDispatchInterrupt();
37 KeLowerIrql(oldlvl);
38 }
39
40 NtYieldExecution();
41
42 Ke386HaltProcessor();
43 }
44 }
45
46
47 /** Initialization of system idle thread
48 *
49 */
50 VOID INIT_FUNCTION
51 PsInitIdleThread(VOID)
52 {
53 NTSTATUS Status;
54 PETHREAD IdleThread;
55 HANDLE IdleThreadHandle;
56
57 Status = PsCreateSystemThread(&IdleThreadHandle,
58 THREAD_ALL_ACCESS,
59 NULL,
60 NULL,
61 NULL,
62 PsIdleThreadMain,
63 NULL);
64 if(!NT_SUCCESS(Status))
65 {
66 DPRINT("Couldn't create Idle System Thread!");
67 KEBUGCHECK(0);
68 return;
69 }
70 Status = ObReferenceObjectByHandle(IdleThreadHandle,
71 THREAD_ALL_ACCESS,
72 PsThreadType,
73 KernelMode,
74 (PVOID*)&IdleThread,
75 NULL);
76 if(!NT_SUCCESS(Status))
77 {
78 DPRINT("Couldn't get pointer to Idle System Thread!");
79 KEBUGCHECK(0);
80 return;
81 }
82 NtClose(IdleThreadHandle);
83 KeGetCurrentKPCR()->PrcbData.IdleThread = &IdleThread->Tcb;
84 KeSetPriorityThread(&IdleThread->Tcb, LOW_PRIORITY);
85 KeSetAffinityThread(&IdleThread->Tcb, 1 << 0);
86
87 }