Nucleus
Barry Signals and small task functions 9d6eb50 (3 years, 2 months ago)
diff --git a/task/wait.c b/task/wait.c
new file mode 100644
index 0000000..35ee29a
--- /dev/null
+++ b/task/wait.c
@@ -0,0 +1,36 @@
+/*
+ * This file implements task waiting. A task may wait for another task to
+ * change state. While waiting, the current task is blocked. When the target
+ * process changes state the current task is unblocked and gets any relevant
+ * information from it before releasing it.
+ */
+
+#include <errno.h>
+#include <nucleus/task.h>
+
+/* Wait for a child process to change state */
+pid_t
+waitpid(pid_t pid, int *wstatus, int options)
+{
+ if (wstatus && !verify_access(wstatus, sizeof(int), PROT_WRITE))
+ return -EFAULT;
+
+ Task *task = find_task(pid);
+ if (!task)
+ return -ECHILD;
+ current->target = get(task);
+
+ if (task->state != TERMINATED) {
+ if (!task->wait)
+ task->wait = create_list(&taskType, LIST_NORMAL);
+ block_task(WAITING, task->wait);
+ }
+
+ if (wstatus)
+ *wstatus = task->status;
+
+ put(current->target);
+ current->target = NULL;
+
+ return task->tid;
+}