2004-08-15 Casper S. Hornstrup <chorns@users.sourceforge.net>
[reactos.git] / reactos / ntoskrnl / mm / pager.c
1 /* $Id: pager.c,v 1.18 2004/08/15 16:39:08 chorns Exp $
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/mm/pager.c
6 * PURPOSE: Moves infrequently used data out of memory
7 * PROGRAMMER: David Welch (welch@cwcom.net)
8 * UPDATE HISTORY:
9 * 27/05/98: Created
10 */
11
12 /* INCLUDES ****************************************************************/
13
14 #include <ntoskrnl.h>
15 #define NDEBUG
16 #include <internal/debug.h>
17
18 /* GLOBALS *******************************************************************/
19
20 #if 0
21 static HANDLE PagerThreadHandle;
22 static CLIENT_ID PagerThreadId;
23 static KEVENT PagerThreadEvent;
24 static BOOLEAN PagerThreadShouldTerminate;
25 static ULONG PagerThreadWorkCount;
26 #endif
27
28 /* FUNCTIONS *****************************************************************/
29
30 #if 0
31 BOOLEAN
32 MiIsPagerThread(VOID)
33 {
34 return(PsGetCurrentThreadId() == PagerThreadId.UniqueThread);
35 }
36
37 VOID
38 MiStartPagerThread(VOID)
39 {
40 ULONG WasWorking;
41
42 WasWorking = InterlockedIncrement(&PagerThreadWorkCount);
43 if (WasWorking == 1)
44 {
45 KeSetEvent(&PagerThreadEvent, IO_NO_INCREMENT, FALSE);
46 }
47 }
48
49 VOID
50 MiStopPagerThread(VOID)
51 {
52 (VOID)InterlockedDecrement(&PagerThreadWorkCount);
53 }
54
55 static NTSTATUS STDCALL
56 MmPagerThreadMain(PVOID Ignored)
57 {
58 NTSTATUS Status;
59
60 for(;;)
61 {
62 /* Wake for a low memory situation or a terminate request. */
63 Status = KeWaitForSingleObject(&PagerThreadEvent,
64 0,
65 KernelMode,
66 FALSE,
67 NULL);
68 if (!NT_SUCCESS(Status))
69 {
70 DbgPrint("PagerThread: Wait failed\n");
71 KEBUGCHECK(0);
72 }
73 if (PagerThreadShouldTerminate)
74 {
75 DbgPrint("PagerThread: Terminating\n");
76 return(STATUS_SUCCESS);
77 }
78 do
79 {
80 /* Try and make some memory available to the system. */
81 MmRebalanceMemoryConsumers();
82 }
83 while(PagerThreadWorkCount > 0);
84 }
85 }
86
87 NTSTATUS MmInitPagerThread(VOID)
88 {
89 NTSTATUS Status;
90
91 PagerThreadShouldTerminate = FALSE;
92 PagerThreadWorkCount = 0;
93 KeInitializeEvent(&PagerThreadEvent,
94 SynchronizationEvent,
95 FALSE);
96
97 Status = PsCreateSystemThread(&PagerThreadHandle,
98 THREAD_ALL_ACCESS,
99 NULL,
100 NULL,
101 &PagerThreadId,
102 (PKSTART_ROUTINE) MmPagerThreadMain,
103 NULL);
104 if (!NT_SUCCESS(Status))
105 {
106 return(Status);
107 }
108
109 return(STATUS_SUCCESS);
110 }
111 #endif