430d4acd00107e2d066c720a24002c867fdc2a29
[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 extern PEPROCESS PsIdleProcess;
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 Ke386HaltProcessor();
45 }
46 }
47
48
49 /** Initialization of system idle thread
50 *
51 */
52 VOID INIT_FUNCTION
53 PsInitIdleThread(VOID)
54 {
55 NTSTATUS Status;
56 PETHREAD IdleThread;
57 KIRQL oldIrql;
58
59 Status = PsInitializeThread(PsIdleProcess,
60 &IdleThread,
61 NULL,
62 KernelMode,
63 FALSE);
64 if (!NT_SUCCESS(Status))
65 {
66 DPRINT1("Couldn't create idle system thread! Status: 0x%x\n", Status);
67 KEBUGCHECK(0);
68 return;
69 }
70
71 IdleThread->StartAddress = PsIdleThreadMain;
72 Status = KiArchInitThread(&IdleThread->Tcb, PsIdleThreadMain, NULL);
73 if (!NT_SUCCESS(Status))
74 {
75 DPRINT1("Couldn't initialize system idle thread! Status: 0x%x\n", Status);
76 ObDereferenceObject(IdleThread);
77 KEBUGCHECK(0);
78 return;
79 }
80
81 oldIrql = KeAcquireDispatcherDatabaseLock ();
82 PsUnblockThread(IdleThread, NULL, 0);
83 KeReleaseDispatcherDatabaseLock(oldIrql);
84
85 KeGetCurrentPrcb()->IdleThread = &IdleThread->Tcb;
86 KeSetPriorityThread(&IdleThread->Tcb, LOW_PRIORITY);
87 KeSetAffinityThread(&IdleThread->Tcb, 1 << 0);
88
89 }