1/*	$NetBSD: thrd.c,v 1.4 2019/09/10 22:34:19 kamil Exp $	*/
2
3/*-
4 * Copyright (c) 2016 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Kamil Rytarowski.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#include <sys/cdefs.h>
33__RCSID("$NetBSD: thrd.c,v 1.4 2019/09/10 22:34:19 kamil Exp $");
34
35#include <assert.h>
36#include <errno.h>
37#include <pthread.h>
38#include <sched.h>
39#include <stdlib.h>
40#include <time.h>
41#include <threads.h>
42
43struct __thrd_tramp_data {
44	thrd_start_t func;
45	void *arg;
46};
47
48static void *
49__thrd_create_tramp(void *arg)
50{
51	struct __thrd_tramp_data *cookie;
52	int ret;
53
54	_DIAGASSERT(arg != NULL);
55
56	cookie = (struct __thrd_tramp_data *)arg;
57
58	ret = (cookie->func)(cookie->arg);
59
60	free(cookie);
61
62	return (void *)(intptr_t)ret;
63}
64
65int
66thrd_create(thrd_t *thr, thrd_start_t func, void *arg)
67{
68	struct __thrd_tramp_data *cookie;
69	int error;
70
71	_DIAGASSERT(thr != NULL);
72	_DIAGASSERT(func != NULL);
73
74	cookie = malloc(sizeof(*cookie));
75	if (cookie == NULL)
76		return thrd_nomem;
77
78	cookie->func = func;
79	cookie->arg = arg;
80
81	switch(pthread_create(thr, NULL, __thrd_create_tramp, cookie)) {
82	case 0:
83		return thrd_success;
84	case ENOMEM:
85		error = thrd_nomem;
86		break;
87	default:
88		error = thrd_error;
89	}
90
91	free(cookie);
92
93	return error;
94}
95
96thrd_t
97thrd_current(void)
98{
99
100	return pthread_self();
101}
102
103int
104thrd_detach(thrd_t thr)
105{
106
107	_DIAGASSERT(thr != NULL);
108
109	if (pthread_detach(thr) == 0)
110		return thrd_success;
111
112	return thrd_error;
113}
114
115int
116thrd_equal(thrd_t t1, thrd_t t2)
117{
118
119	_DIAGASSERT(t1 != NULL);
120	_DIAGASSERT(t2 != NULL);
121
122	return pthread_equal(t1, t2);
123}
124
125__dead void
126thrd_exit(int res)
127{
128
129	pthread_exit((void *)(intptr_t)res);
130}
131
132int
133thrd_join(thrd_t thrd, int *res)
134{
135	void *ptr;
136
137	_DIAGASSERT(thrd != NULL);
138
139	if (pthread_join(thrd, &ptr) == 0) {
140		if (res)
141			*res = (int)(intptr_t)ptr;
142
143		return thrd_success;
144	}
145
146	return thrd_error;
147}
148
149int
150thrd_sleep(const struct timespec *duration, struct timespec *remaining)
151{
152
153	_DIAGASSERT(duration != NULL);
154
155	/* Use clock_nanosleep(3) to skip handling errno */
156
157	switch (clock_nanosleep(CLOCK_MONOTONIC, TIMER_RELTIME, duration,
158	    remaining)) {
159	case 0:
160		return 0;
161	case EINTR:
162		return -1;
163	default:
164		/* Negative value different than -1 */
165		return -2;
166	}
167}
168
169void
170thrd_yield(void)
171{
172
173	sched_yield();
174}
175