xref: /netbsd-src/lib/libpthread/threads.h (revision 36ec32beff138f56e5b9d363d9cab30b5f2b03dd)
1 /*	$NetBSD: threads.h,v 1.3 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 #ifndef _THREADS_H_
33 #define _THREADS_H_
34 
35 #include <sys/cdefs.h>
36 #include <limits.h>
37 #include <pthread.h>
38 #include <stdint.h>
39 #include <time.h>
40 
41 /* ISO/IEC 9899:201x 7.26.1/3 */
42 #ifndef __thread_local_is_defined
43 #if ((__cplusplus - 0) < 201103L)
44 #define thread_local _Thread_local
45 #endif
46 #define __thread_local_is_defined
47 #endif /* __thread_local_is_defined */
48 
49 #ifndef ONCE_FLAG_INIT
50 #define ONCE_FLAG_INIT	PTHREAD_ONCE_INIT
51 #endif /* ONCE_FLAG_INIT */
52 
53 #ifndef TSS_DTOR_ITERATIONS
54 #define TSS_DTOR_ITERATIONS	PTHREAD_DESTRUCTOR_ITERATIONS
55 #endif /* TSS_DTOR_ITERATIONS */
56 
57 /* ISO/IEC 9899:201x 7.26.1/4 */
58 typedef pthread_cond_t	  cnd_t;
59 typedef pthread_t	  thrd_t;
60 typedef pthread_key_t	  tss_t;
61 typedef pthread_mutex_t	  mtx_t;
62 typedef void		(*tss_dtor_t)	(void *);
63 typedef int		(*thrd_start_t)	(void *);
64 typedef	pthread_once_t	  once_flag;
65 
66 /* ISO/IEC 9899:201x 7.26.1/5 */
67 enum {
68 	mtx_plain	= 1,
69 	mtx_recursive	= 2,
70 	mtx_timed	= 4,
71 	_MTX_MAXTYPE	= 0x7fffffff
72 };
73 
74 enum {
75 	thrd_timedout	= -1,
76 	thrd_success	=  0,
77 	thrd_busy	=  1,
78 	thrd_error	=  2,
79 	thrd_nomem	=  3,
80 	_THRD_MAXTYPE	=  0x7fffffff
81 };
82 
83 __BEGIN_DECLS
84 /* ISO/IEC 9899:201x 7.26.2 Initialization functions */
85 void	call_once(once_flag *, void (*)(void));
86 
87 /* ISO/IEC 9899:201x 7.26.3 Condition variable functions */
88 int	cnd_broadcast(cnd_t *);
89 void	cnd_destroy(cnd_t *);
90 int	cnd_init(cnd_t *);
91 int	cnd_signal(cnd_t *);
92 int	cnd_timedwait(cnd_t * __restrict, mtx_t * __restrict,
93 	    const struct timespec * __restrict);
94 int	cnd_wait(cnd_t *, mtx_t *);
95 
96 /* ISO/IEC 9899:201x 7.26.4 Mutex functions */
97 void	mtx_destroy(mtx_t *);
98 int	mtx_init(mtx_t *, int);
99 int	mtx_lock(mtx_t *);
100 int	mtx_timedlock(mtx_t *__restrict, const struct timespec *__restrict);
101 int	mtx_trylock(mtx_t *);
102 int	mtx_unlock(mtx_t *);
103 
104 /* ISO/IEC 9899:201x 7.26.5 Thread functions */
105 int	thrd_create(thrd_t *, thrd_start_t, void *);
106 thrd_t	thrd_current(void);
107 int	thrd_detach(thrd_t);
108 int	thrd_equal(thrd_t, thrd_t);
109 __dead void	thrd_exit(int);
110 int	thrd_join(thrd_t, int *);
111 int	thrd_sleep(const struct timespec *, struct timespec *);
112 void	thrd_yield(void);
113 
114 /* ISO/IEC 9899:201x 7.26.6 Thread-specific storage functions */
115 int	tss_create(tss_t *, tss_dtor_t);
116 void	tss_delete(tss_t);
117 void	*tss_get(tss_t);
118 int	tss_set(tss_t, void *);
119 __END_DECLS
120 
121 #endif
122