Add an undertaker task that is responsible for destroying dead tasks.

This commit is contained in:
Andreas Kling 2018-10-23 15:41:55 +02:00
parent 1c1d0f5362
commit d90d125dfe
3 changed files with 42 additions and 8 deletions

View file

@ -17,6 +17,7 @@ Task* s_kernelTask;
static pid_t next_pid; static pid_t next_pid;
static InlineLinkedList<Task>* s_tasks; static InlineLinkedList<Task>* s_tasks;
static InlineLinkedList<Task>* s_deadTasks;
static bool contextSwitch(Task*); static bool contextSwitch(Task*);
@ -52,6 +53,7 @@ void Task::initialize()
current = nullptr; current = nullptr;
next_pid = 0; next_pid = 0;
s_tasks = new InlineLinkedList<Task>; s_tasks = new InlineLinkedList<Task>;
s_deadTasks = new InlineLinkedList<Task>;
s_kernelTask = new Task(0, "colonel", IPC::Handle::Any, Task::Ring0); s_kernelTask = new Task(0, "colonel", IPC::Handle::Any, Task::Ring0);
redoKernelTaskTSS(); redoKernelTaskTSS();
loadTaskRegister(s_kernelTask->selector()); loadTaskRegister(s_kernelTask->selector());
@ -213,7 +215,6 @@ Task::Task(String&& name, uid_t uid, gid_t gid)
m_tss.esp = m_stackTop; m_tss.esp = m_stackTop;
// Set up a separate stack for Ring0. // Set up a separate stack for Ring0.
// FIXME: Don't leak this stack.
m_kernelStack = kmalloc(defaultStackSize); m_kernelStack = kmalloc(defaultStackSize);
DWORD ring0StackTop = ((DWORD)m_kernelStack + defaultStackSize) & 0xffffff8; DWORD ring0StackTop = ((DWORD)m_kernelStack + defaultStackSize) & 0xffffff8;
m_tss.ss0 = 0x10; m_tss.ss0 = 0x10;
@ -336,14 +337,10 @@ Task::~Task()
delete [] m_ldtEntries; delete [] m_ldtEntries;
m_ldtEntries = nullptr; m_ldtEntries = nullptr;
// FIXME: The task's kernel stack is currently leaked, because otherwise we GPF.
// This obviously needs figuring out.
#if 0
if (m_kernelStack) { if (m_kernelStack) {
kfree(m_kernelStack); kfree(m_kernelStack);
m_kernelStack = nullptr; m_kernelStack = nullptr;
} }
#endif
} }
void Task::dumpRegions() void Task::dumpRegions()
@ -375,7 +372,7 @@ void Task::sys$exit(int status)
HANG; HANG;
} }
delete this; s_deadTasks->append(this);
switchNow(); switchNow();
} }
@ -395,11 +392,21 @@ void Task::taskDidCrash(Task* crashedTask)
HANG; HANG;
} }
delete crashedTask; s_deadTasks->append(crashedTask);
switchNow(); switchNow();
} }
void Task::doHouseKeeping()
{
Task* next = nullptr;
for (auto* deadTask = s_deadTasks->head(); deadTask; deadTask = next) {
next = deadTask->next();
delete deadTask;
}
s_deadTasks->clear();
}
void yield() void yield()
{ {
if (!current) { if (!current) {

View file

@ -67,6 +67,8 @@ public:
FileHandle* fileHandleIfExists(int fd); FileHandle* fileHandleIfExists(int fd);
static void doHouseKeeping();
bool acceptsMessageFrom(Task&); bool acceptsMessageFrom(Task&);
void block(Task::State); void block(Task::State);

View file

@ -28,6 +28,7 @@
#include "ProcFileSystem.h" #include "ProcFileSystem.h"
#define TEST_VFS #define TEST_VFS
//#define STRESS_TEST_SPAWNING
//#define TEST_ELF_LOADER //#define TEST_ELF_LOADER
//#define TEST_CRASHY_USER_PROCESSES //#define TEST_CRASHY_USER_PROCESSES
@ -95,6 +96,15 @@ void banner()
kprintf("\n"); kprintf("\n");
} }
static void undertaker_main() NORETURN;
static void undertaker_main()
{
for (;;) {
Task::doHouseKeeping();
sleep(10);
}
}
static void init_stage2() NORETURN; static void init_stage2() NORETURN;
static void init_stage2() static void init_stage2()
{ {
@ -105,7 +115,7 @@ static void init_stage2()
auto keyboard = make<Keyboard>(); auto keyboard = make<Keyboard>();
extern void panel_main(); extern void panel_main();
new Task(panel_main, "panel", IPC::Handle::PanelTask, Task::Ring0); //new Task(panel_main, "panel", IPC::Handle::PanelTask, Task::Ring0);
//new Task(led_disco, "led-disco", IPC::Handle::Any, Task::Ring0); //new Task(led_disco, "led-disco", IPC::Handle::Any, Task::Ring0);
Disk::initialize(); Disk::initialize();
@ -171,6 +181,19 @@ static void init_stage2()
} }
#endif #endif
#ifdef STRESS_TEST_SPAWNING
dword lastAlloc = sum_alloc;
for (unsigned i = 0; i < 100; ++i) {
auto* shTask = Task::create("/bin/id", (uid_t)100, (gid_t)100);
kprintf("malloc stats: alloc:%u free:%u\n", sum_alloc, sum_free);
kprintf("sizeof(Task):%u\n", sizeof(Task));
kprintf("delta:%u\n",sum_alloc - lastAlloc);
lastAlloc = sum_alloc;
sleep(600);
}
#endif
auto* shTask = Task::create("/bin/sh", (uid_t)100, (gid_t)100); auto* shTask = Task::create("/bin/sh", (uid_t)100, (gid_t)100);
//new Task(motd_main, "motd", IPC::Handle::MotdTask, Task::Ring0); //new Task(motd_main, "motd", IPC::Handle::MotdTask, Task::Ring0);
@ -221,6 +244,8 @@ void init()
Task::initialize(); Task::initialize();
new Task(undertaker_main, "undertaker", IPC::Handle::UserTask, Task::Ring0);
auto* init2 = new Task(init_stage2, "init", IPC::Handle::InitTask, Task::Ring0); auto* init2 = new Task(init_stage2, "init", IPC::Handle::InitTask, Task::Ring0);
scheduleNewTask(); scheduleNewTask();