migrate substitution keywords to SVN
[reactos.git] / reactos / ntoskrnl / mm / mpw.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 1998, 1999, 2000, 2001 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 /* $Id$
20 *
21 * PROJECT: ReactOS kernel
22 * FILE: ntoskrnl/mm/mpw.c
23 * PURPOSE: Writes data that has been modified in memory but not on
24 * the disk
25 * PROGRAMMER: David Welch (welch@cwcom.net)
26 * UPDATE HISTORY:
27 * 27/05/98: Created
28 */
29
30 /* INCLUDES ****************************************************************/
31
32 #include <ntoskrnl.h>
33 #define NDEBUG
34 #include <internal/debug.h>
35
36 /* GLOBALS *******************************************************************/
37
38 static HANDLE MpwThreadHandle;
39 static CLIENT_ID MpwThreadId;
40 static KEVENT MpwThreadEvent;
41 static volatile BOOLEAN MpwThreadShouldTerminate;
42
43 /* FUNCTIONS *****************************************************************/
44
45 NTSTATUS STDCALL
46 MmWriteDirtyPages(ULONG Target, PULONG Actual)
47 {
48 PFN_TYPE Page;
49 PFN_TYPE NextPage;
50 NTSTATUS Status;
51
52 Page = MmGetLRUFirstUserPage();
53 while (Page != 0 && Target > 0)
54 {
55 /*
56 * FIXME: While the current page is write back it is possible
57 * that the next page is freed and not longer a user page.
58 */
59 NextPage = MmGetLRUNextUserPage(Page);
60 if (MmIsDirtyPageRmap(Page))
61 {
62 Status = MmWritePagePhysicalAddress(Page);
63 if (NT_SUCCESS(Status))
64 {
65 Target--;
66 }
67 }
68 Page = NextPage;
69 }
70 *Actual = Target;
71 return(STATUS_SUCCESS);
72 }
73
74 NTSTATUS STDCALL
75 MmMpwThreadMain(PVOID Ignored)
76 {
77 NTSTATUS Status;
78 ULONG PagesWritten;
79 LARGE_INTEGER Timeout;
80
81 Timeout.QuadPart = -50000000;
82
83 for(;;)
84 {
85 Status = KeWaitForSingleObject(&MpwThreadEvent,
86 0,
87 KernelMode,
88 FALSE,
89 &Timeout);
90 if (!NT_SUCCESS(Status))
91 {
92 DbgPrint("MpwThread: Wait failed\n");
93 KEBUGCHECK(0);
94 return(STATUS_UNSUCCESSFUL);
95 }
96 if (MpwThreadShouldTerminate)
97 {
98 DbgPrint("MpwThread: Terminating\n");
99 return(STATUS_SUCCESS);
100 }
101
102 PagesWritten = 0;
103 #if 0
104 /*
105 * FIXME: MmWriteDirtyPages doesn't work correctly.
106 */
107 MmWriteDirtyPages(128, &PagesWritten);
108 #endif
109
110 CcRosFlushDirtyPages(128, &PagesWritten);
111 }
112 }
113
114 NTSTATUS MmInitMpwThread(VOID)
115 {
116 KPRIORITY Priority;
117 NTSTATUS Status;
118
119 MpwThreadShouldTerminate = FALSE;
120 KeInitializeEvent(&MpwThreadEvent, SynchronizationEvent, FALSE);
121
122 Status = PsCreateSystemThread(&MpwThreadHandle,
123 THREAD_ALL_ACCESS,
124 NULL,
125 NULL,
126 &MpwThreadId,
127 (PKSTART_ROUTINE) MmMpwThreadMain,
128 NULL);
129 if (!NT_SUCCESS(Status))
130 {
131 return(Status);
132 }
133
134 Priority = 1;
135 NtSetInformationThread(MpwThreadHandle,
136 ThreadPriority,
137 &Priority,
138 sizeof(Priority));
139
140 return(STATUS_SUCCESS);
141 }