diff --git a/KThread.cpp b/KThread.cpp index 02583d0..ad812f3 100644 --- a/KThread.cpp +++ b/KThread.cpp @@ -47,7 +47,7 @@ void KernelMainThread() KPrint("Initializing Network Interface Manager..."); NIManager = new NetworkInterfaceManager::NetworkInterface; KPrint("Starting Network Interface Manager..."); - // NIManager->StartService(); + NIManager->StartService(); KPrint("Setting up userspace..."); @@ -91,10 +91,7 @@ Exit: void KernelShutdownThread(bool Reboot) { - delete NIManager; - - if (DriverManager) - DriverManager->UnloadAllDrivers(); + BeforeShutdown(); trace("Shutting Down/Rebooting..."); if (Reboot) diff --git a/Kernel.cpp b/Kernel.cpp index 9d7a5be..f51a158 100644 --- a/Kernel.cpp +++ b/Kernel.cpp @@ -30,11 +30,11 @@ * * BUGS: * - [ ] Kernel crashes when receiving interrupts for drivers only if the system has one core and the tasking is running. - * + * * CREDITS AND REFERENCES: * - General: * https://wiki.osdev.org/Main_Page - * + * * - Network: * https://web.archive.org/web/20051210132103/http://users.pcnet.ro/dmoroian/beej/Beej.html * https://web.archive.org/web/20060229214053/http://www.cs.rutgers.edu/~pxk/417/notes/sockets/udp.html @@ -248,6 +248,26 @@ EXTERNC __no_stack_protector __no_instrument_function void Entry(BootInfo *Info) EXTERNC __no_stack_protector __no_instrument_function void BeforeShutdown() { + /* TODO: Announce shutdown */ + + trace("\n\n\n#################### SYSTEM SHUTTING DOWN ####################\n\n"); + delete NIManager; + + delete DiskManager; + if (DriverManager) + DriverManager->UnloadAllDrivers(); + delete DriverManager; + + TaskManager->SignalShutdown(); + delete TaskManager; + + delete RecoveryScreen; + delete vfs; + delete TimeManager; + delete KernelSymbolTable; + delete Display; + // PowerManager should not be called + // https://wiki.osdev.org/Calling_Global_Constructors debug("Calling destructors..."); for (CallPtr *func = __fini_array_start; func != __fini_array_end; func++) diff --git a/Tasking/Task.cpp b/Tasking/Task.cpp index c8c917d..6548d9b 100644 --- a/Tasking/Task.cpp +++ b/Tasking/Task.cpp @@ -743,6 +743,13 @@ namespace Tasking OneShot(1); } + void Task::SignalShutdown() + { + fixme("SignalShutdown()"); + // TODO: Implement this + // This should hang until all processes are terminated + } + TCB *Task::CreateThread(PCB *Parent, IP EntryPoint, const char **argv, @@ -1210,5 +1217,34 @@ namespace Tasking { SmartCriticalSection(TaskingLock); trace("Stopping tasking"); + foreach (auto Process in ListProcess) + { + for (auto &Thread : Process->Threads) + { + Thread->Status = TaskStatus::Terminated; + } + Process->Status = TaskStatus::Terminated; + } + + TaskingLock.Unlock(); + SchedulerLock.Unlock(); + + while (ListProcess.size() > 0) + { + trace("Waiting for %d processes to terminate", ListProcess.size()); + int NotTerminated = 0; + foreach (auto Process in ListProcess) + { + debug("Process %s(%d) is still running (or waiting to be removed status %#lx)", Process->Name, Process->ID, Process->Status); + if (Process->Status == TaskStatus::Terminated) + continue; + NotTerminated++; + } + if (NotTerminated == 0) + break; + OneShot(100); + } + + trace("Tasking stopped"); } } diff --git a/include/task.hpp b/include/task.hpp index 1d3982d..a5141bf 100644 --- a/include/task.hpp +++ b/include/task.hpp @@ -267,6 +267,7 @@ namespace Tasking Vector GetProcessList() { return ListProcess; } void Panic() { StopScheduler = true; } void Schedule(); + void SignalShutdown(); long GetUsage(int Core) { if (IdleProcess)