xref: /freebsd/lib/libstdthreads/thrd.c (revision 4543ef516683042d46f3bd3bb8a4f3f746e00499)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2011 Ed Schouten <ed@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 #include <pthread.h>
31 #include <stdint.h>
32 #include <stdlib.h>
33 
34 #include "threads.h"
35 
36 struct thrd_param {
37 	thrd_start_t	 func;
38 	void		*arg;
39 };
40 
41 static void *
42 thrd_entry(void *arg)
43 {
44 	struct thrd_param tp;
45 
46 	tp = *(struct thrd_param *)arg;
47 	free(arg);
48 	return ((void *)(intptr_t)tp.func(tp.arg));
49 }
50 
51 int
52 thrd_create(thrd_t *thr, thrd_start_t func, void *arg)
53 {
54 	struct thrd_param *tp;
55 
56 	/*
57 	 * Work around return type inconsistency.  Wrap execution using
58 	 * a function conforming to pthread_create()'s start_routine.
59 	 */
60 	tp = malloc(sizeof(*tp));
61 	if (tp == NULL)
62 		return (thrd_nomem);
63 	tp->func = func;
64 	tp->arg = arg;
65 	if (pthread_create(thr, NULL, thrd_entry, tp) != 0) {
66 		free(tp);
67 		return (thrd_error);
68 	}
69 	return (thrd_success);
70 }
71 
72 thrd_t
73 thrd_current(void)
74 {
75 
76 	return (pthread_self());
77 }
78 
79 int
80 thrd_detach(thrd_t thr)
81 {
82 
83 	if (pthread_detach(thr) != 0)
84 		return (thrd_error);
85 	return (thrd_success);
86 }
87 
88 int
89 thrd_equal(thrd_t thr0, thrd_t thr1)
90 {
91 
92 	return (pthread_equal(thr0, thr1));
93 }
94 
95 _Noreturn void
96 thrd_exit(int res)
97 {
98 
99 	pthread_exit((void *)(intptr_t)res);
100 }
101 
102 int
103 thrd_join(thrd_t thr, int *res)
104 {
105 	void *value_ptr;
106 
107 	if (pthread_join(thr, &value_ptr) != 0)
108 		return (thrd_error);
109 	if (res != NULL)
110 		*res = (intptr_t)value_ptr;
111 	return (thrd_success);
112 }
113 
114 int
115 thrd_sleep(const struct timespec *duration, struct timespec *remaining)
116 {
117 
118 	return (nanosleep(duration, remaining));
119 }
120 
121 void
122 thrd_yield(void)
123 {
124 
125 	pthread_yield();
126 }
127