BarryServer : Git

All the code for all my projects
// BarryServer : Git / Nucleus / commit / 9d6eb508c283ff07934a3007f68d4f0c933d5430 / task / exit.c

// Related

Nucleus

Barry Signals and small task functions 9d6eb50 (3 years, 2 months ago)
diff --git a/task/exit.c b/task/exit.c
new file mode 100644
index 0000000..52547b9
--- /dev/null
+++ b/task/exit.c
@@ -0,0 +1,39 @@
+/*
+ * This file handles task termination and contains the exit() system call.  It
+ * destroys a task and releases all resources it holds, notifies any waiting
+ * tasks and the parent.
+ */
+
+#include <nucleus/panic.h>
+#include <nucleus/task.h>
+
+extern ObjectList *readyQueue[];
+
+/* Terminate the current task */
+_Noreturn void
+terminate(void)
+{
+	current->state = TERMINATED;
+	current->inCriticalSection = 0;
+
+	/* Unblock waiting tasks */
+	Task *tmp;
+	while (current->wait && count(current->wait)) {
+		tmp = pop_from_start(current->wait);
+		add(readyQueue[tmp->priority], tmp);
+		put(tmp);
+	}
+
+	schedule();
+	__builtin_unreachable();
+}
+
+/* Exit the current task */
+_Noreturn void
+exit(int status)
+{
+	if (current->tid == 1)
+		panic("Attempted to exit init! Exit code %d", status);
+	current->status = (1 << 31) | (status & 0x0F);
+	terminate();
+}