1285307Sed/*-
2285307Sed * Copyright (c) 2015 Nuxi, https://nuxi.nl/
3285307Sed *
4285307Sed * Redistribution and use in source and binary forms, with or without
5285307Sed * modification, are permitted provided that the following conditions
6285307Sed * are met:
7285307Sed * 1. Redistributions of source code must retain the above copyright
8285307Sed *    notice, this list of conditions and the following disclaimer.
9285307Sed * 2. Redistributions in binary form must reproduce the above copyright
10285307Sed *    notice, this list of conditions and the following disclaimer in the
11285307Sed *    documentation and/or other materials provided with the distribution.
12285307Sed *
13285307Sed * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14285307Sed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15285307Sed * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16285307Sed * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17285307Sed * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18285307Sed * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19285307Sed * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20285307Sed * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21285307Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22285307Sed * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23285307Sed * SUCH DAMAGE.
24285307Sed */
25285307Sed
26285307Sed#include <sys/cdefs.h>
27285307Sed__FBSDID("$FreeBSD: stable/11/sys/compat/cloudabi/cloudabi_thread.c 307144 2016-10-12 12:17:41Z ed $");
28285307Sed
29285540Sed#include <sys/param.h>
30285540Sed#include <sys/proc.h>
31285540Sed#include <sys/sched.h>
32285908Sed#include <sys/syscallsubr.h>
33300043Skib#include <sys/umtx.h>
34285540Sed
35297247Sed#include <contrib/cloudabi/cloudabi_types_common.h>
36297247Sed
37285307Sed#include <compat/cloudabi/cloudabi_proto.h>
38285307Sed
39285307Sedint
40285307Sedcloudabi_sys_thread_exit(struct thread *td,
41285307Sed    struct cloudabi_sys_thread_exit_args *uap)
42285307Sed{
43285908Sed	struct cloudabi_sys_lock_unlock_args cloudabi_sys_lock_unlock_args = {
44285908Sed		.lock = uap->lock,
45285908Sed		.scope = uap->scope,
46285908Sed	};
47285307Sed
48300043Skib	umtx_thread_exit(td);
49300043Skib
50285908Sed        /* Wake up joining thread. */
51285908Sed	cloudabi_sys_lock_unlock(td, &cloudabi_sys_lock_unlock_args);
52285908Sed
53285908Sed        /*
54285908Sed	 * Attempt to terminate the thread. Terminate the process if
55285908Sed	 * it's the last thread.
56285908Sed	 */
57285908Sed	kern_thr_exit(td);
58285908Sed	exit1(td, 0, 0);
59285908Sed	/* NOTREACHED */
60285307Sed}
61285307Sed
62285307Sedint
63285307Sedcloudabi_sys_thread_yield(struct thread *td,
64285307Sed    struct cloudabi_sys_thread_yield_args *uap)
65285307Sed{
66285307Sed
67285540Sed	sched_relinquish(td);
68285540Sed	return (0);
69285307Sed}
70