BarryServer : Git

All the code for all my projects
// BarryServer : Git / Nucleus / blob / master / task / wait.c

// Related

Nucleus

Barry System headers (remove libc dependency) 18495cf (3 years, 2 months ago)
/*
 * 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 <sys/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;
}