1 using System;
  2 using System.Collections;
  3 using cmsx.machine;
  4 
  5 namespace cmsx.kernel
  6 {
  7     public class Kernel
  8     {
  9         static Kernel() : instance(new Kernel())
 10         {
 11         }
 12         public static nothrow Kernel& Instance()
 13         {
 14             return *instance;
 15         }
 16         private nothrow Kernel() : machine(GetMachine())sessionTable(GetSessionTable())processTable(GetProcessTable())baseDebugger()debugger(&baseDebugger)consoleDriver(null)
 17             kernelEvent(OsCreateEvent())kernelIdleEvent(OsCreateEvent())booting(false)waiting(false)
 18         {
 19             SetIdle();
 20             SetKernelShutdownFunc(StopKernelThreads);
 21             RetrieveHostName();
 22         }
 23         public ~Kernel()
 24         {
 25             OsCloseEvent(kernelEvent);
 26             OsCloseEvent(kernelIdleEvent);
 27         }
 28         public nothrow void SetUser(const string& user_)
 29         {
 30             user = user_;
 31         }
 32         public inline nothrow const string& User() const
 33         {
 34             return user;
 35         }
 36         public inline nothrow const string& Host() const
 37         {
 38             return host;
 39         }
 40         public void Init()
 41         {
 42             DefaultInitializeMemoryTable(machinememoryTable);
 43             memoryTable.addressSpaceNumber = kernelAddressSpaceNumber;
 44         }
 45         public inline nothrow Machine& GetMachine()
 46         {
 47             return machine;
 48         }
 49         public inline nothrow SessionTable& GetSessionTable()
 50         {
 51             return sessionTable;
 52         }
 53         public inline nothrow ProcessTable& GetProcessTable()
 54         {
 55             return processTable;
 56         }
 57         public inline nothrow MemoryTable& GetMemoryTable()
 58         {
 59             return memoryTable;
 60         }
 61         public nothrow void SetProgramFileName(const string& programFileName_)
 62         {
 63             programFileName = programFileName_;
 64         }
 65         public nothrow void SetProgramArguments(const List<string>& programArguments_)
 66         {
 67             programArguments = programArguments_;
 68         }
 69         public nothrow const string& GetProgramFileName() const
 70         {
 71             return programFileName;
 72         }
 73         public nothrow const List<string>& GetProgramArguments() const
 74         {
 75             return programArguments;
 76         }
 77         public nothrow void SetProgramPID(int pid)
 78         {
 79             programPID = pid;
 80         }
 81         public nothrow int GetProgramPID()
 82         {
 83             return programPID;
 84         }
 85         public inline nothrow Debugger* GetDebugger()
 86         {
 87             return debugger;
 88         }
 89         public inline nothrow void SetDebugger(Debugger* debugger_)
 90         {
 91             debugger = debugger_;
 92         }
 93         public inline nothrow bool HasUserDebugger() const
 94         {
 95             return debugger != &baseDebugger;
 96         }
 97         public nothrow void SetExecutable(cmsx.object.ExecutableFile* executable_)
 98         {
 99             executable.Reset(executable_);
100         }
101         public nothrow cmsx.object.ExecutableFile* Executable() const
102         {
103             return executable.Get();
104         }
105         public nothrow GlobalFileTable& FileTable()
106         {
107             return globalFileTable;
108         }
109         public nothrow void SetCurrentSession(Session* currentSession_)
110         {
111             currentSession = currentSession_;
112         }
113         public nothrow Session* CurrentSession() const
114         {
115             return currentSession;
116         }
117         public nothrow ConsoleDriver* GetConsoleDriver()
118         {
119             return consoleDriver;
120         }
121         public nothrow void SetConsoleDriver(ConsoleDriver* consoleDriver_)
122         {
123             consoleDriver = consoleDriver_;
124         }
125         public nothrow void SetBooting()
126         {
127             booting = true;
128         }
129         public nothrow void ResetBooting()
130         {
131             booting = false;
132         }
133         public inline nothrow bool Booting() const
134         {
135             return booting;
136         }
137         public void SetIdle()
138         {
139             OsSetEvent(kernelIdleEvent);
140         }
141         public void WaitIdle()
142         {
143             OsWaitEvent(kernelIdleEvent);
144         }
145         public void SetKernelEvent()
146         {
147             OsSetEvent(kernelEvent);
148         }
149         public void WaitKernelEvent()
150         {
151             OsWaitEvent(kernelEvent);
152         }
153         public nothrow bool Waiting() const
154         {
155             return waiting;
156         }
157         public nothrow void SetWaiting()
158         {
159             waiting = true;
160         }
161         public nothrow void ResetWaiting()
162         {
163             waiting = false;
164         }
165         private nothrow void RetrieveHostName()
166         {
167             int n = OsGetMaxComputerNameLength();
168             UniquePtr<char> computerNameBuf(cast<char*>(RtMemAlloc(n + 1)));
169             if (OsGetComputerName(computerNameBuf.Get()n + 1))
170             {
171                 host = computerNameBuf.Get();
172             }
173             else
174             {
175                 host = "computer";
176             }
177         }
178         private static UniquePtr<Kernel> instance;
179         private Machine& machine;
180         private SessionTable& sessionTable;
181         private ProcessTable& processTable;
182         private MemoryTable memoryTable;
183         private string programFileName;
184         private List<string> programArguments;
185         private int programPID;
186         private Debugger baseDebugger;
187         private Debugger* debugger;
188         private UniquePtr<cmsx.object.ExecutableFile> executable;
189         private GlobalFileTable globalFileTable;
190         private Session* currentSession;
191         private ConsoleDriver* consoleDriver;
192         private void* kernelEvent;
193         private void* kernelIdleEvent;
194         private string user;
195         private string host;
196         private bool booting;
197         private bool waiting;
198     }
199     
200     public void StopKernelThreads()
201     {
202         GetDiskDriver().Stop();
203         GetConsoleDriver().Stop();
204         GetKernel().SetKernelEvent();
205     }
206 }