xref: /freebsd/lib/libstdthreads/threads.h (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
1fc6f0665SEd Schouten /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
35e53a4f9SPedro F. Giffuni  *
4fc6f0665SEd Schouten  * Copyright (c) 2011 Ed Schouten <ed@FreeBSD.org>
5fc6f0665SEd Schouten  * All rights reserved.
6fc6f0665SEd Schouten  *
7fc6f0665SEd Schouten  * Redistribution and use in source and binary forms, with or without
8fc6f0665SEd Schouten  * modification, are permitted provided that the following conditions
9fc6f0665SEd Schouten  * are met:
10fc6f0665SEd Schouten  * 1. Redistributions of source code must retain the above copyright
11fc6f0665SEd Schouten  *    notice, this list of conditions and the following disclaimer.
12fc6f0665SEd Schouten  * 2. Redistributions in binary form must reproduce the above copyright
13fc6f0665SEd Schouten  *    notice, this list of conditions and the following disclaimer in the
14fc6f0665SEd Schouten  *    documentation and/or other materials provided with the distribution.
15fc6f0665SEd Schouten  *
16fc6f0665SEd Schouten  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17fc6f0665SEd Schouten  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18fc6f0665SEd Schouten  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19fc6f0665SEd Schouten  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20fc6f0665SEd Schouten  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21fc6f0665SEd Schouten  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22fc6f0665SEd Schouten  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23fc6f0665SEd Schouten  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24fc6f0665SEd Schouten  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25fc6f0665SEd Schouten  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26fc6f0665SEd Schouten  * SUCH DAMAGE.
27fc6f0665SEd Schouten  */
28fc6f0665SEd Schouten 
29fc6f0665SEd Schouten #ifndef _THREADS_H_
30fc6f0665SEd Schouten #define	_THREADS_H_
31fc6f0665SEd Schouten 
32fc6f0665SEd Schouten #include <time.h>
33fc6f0665SEd Schouten 
34fc6f0665SEd Schouten /*
35fc6f0665SEd Schouten  * The C11 threads interface.
36fc6f0665SEd Schouten  *
37fc6f0665SEd Schouten  * This interface is implemented as a light-weight wrapper around
38fc6f0665SEd Schouten  * <pthread.h>.  To prevent namespace pollution, the once_flag object,
39fc6f0665SEd Schouten  * its corresponding ONCE_FLAG_INIT and TSS_DTOR_ITERATIONS have been
40fc6f0665SEd Schouten  * copied from this header file.  They must be kept in sync.
41fc6f0665SEd Schouten  */
42fc6f0665SEd Schouten 
43fc6f0665SEd Schouten typedef struct pthread_cond	*cnd_t;
44fc6f0665SEd Schouten typedef struct pthread_mutex	*mtx_t;
45fc6f0665SEd Schouten typedef struct pthread		*thrd_t;
46fc6f0665SEd Schouten typedef int			 tss_t;
47fc6f0665SEd Schouten 
48fc6f0665SEd Schouten typedef struct {
49fc6f0665SEd Schouten 	int	__state;
50fc6f0665SEd Schouten 	mtx_t	__mutex;
51fc6f0665SEd Schouten } once_flag;
52fc6f0665SEd Schouten 
53fc6f0665SEd Schouten typedef void (*tss_dtor_t)(void *);
54fc6f0665SEd Schouten typedef int (*thrd_start_t)(void *);
55fc6f0665SEd Schouten 
56fc6f0665SEd Schouten enum {
57fc6f0665SEd Schouten 	mtx_plain = 0x1,
58fc6f0665SEd Schouten 	mtx_recursive = 0x2,
59fc6f0665SEd Schouten 	mtx_timed = 0x4
60fc6f0665SEd Schouten };
61fc6f0665SEd Schouten 
62fc6f0665SEd Schouten enum {
63fc6f0665SEd Schouten 	thrd_busy = 1,
64fc6f0665SEd Schouten 	thrd_error = 2,
65fc6f0665SEd Schouten 	thrd_nomem = 3,
66fc6f0665SEd Schouten 	thrd_success = 4,
67fc6f0665SEd Schouten 	thrd_timedout = 5
68fc6f0665SEd Schouten };
69fc6f0665SEd Schouten 
70fc6f0665SEd Schouten #if !defined(__cplusplus) || __cplusplus < 201103L
71fc6f0665SEd Schouten #define	thread_local		_Thread_local
72fc6f0665SEd Schouten #endif
73fc6f0665SEd Schouten #define	ONCE_FLAG_INIT		{ 0, NULL }
74fc6f0665SEd Schouten #define	TSS_DTOR_ITERATIONS	4
75fc6f0665SEd Schouten 
76fc6f0665SEd Schouten __BEGIN_DECLS
77fc6f0665SEd Schouten void	call_once(once_flag *, void (*)(void));
78fc6f0665SEd Schouten int	cnd_broadcast(cnd_t *);
79fc6f0665SEd Schouten void	cnd_destroy(cnd_t *);
80fc6f0665SEd Schouten int	cnd_init(cnd_t *);
81fc6f0665SEd Schouten int	cnd_signal(cnd_t *);
8249891e45SEd Schouten int	cnd_timedwait(cnd_t *__restrict, mtx_t *__restrict __mtx,
8349891e45SEd Schouten     const struct timespec *__restrict)
8449891e45SEd Schouten     __requires_exclusive(*__mtx);
8549891e45SEd Schouten int	cnd_wait(cnd_t *, mtx_t *__mtx)
8649891e45SEd Schouten     __requires_exclusive(*__mtx);
8749891e45SEd Schouten void	mtx_destroy(mtx_t *__mtx)
8849891e45SEd Schouten     __requires_unlocked(*__mtx);
8949891e45SEd Schouten int	mtx_init(mtx_t *__mtx, int)
9049891e45SEd Schouten     __requires_unlocked(*__mtx);
9149891e45SEd Schouten int	mtx_lock(mtx_t *__mtx)
9249891e45SEd Schouten     __locks_exclusive(*__mtx);
9349891e45SEd Schouten int	mtx_timedlock(mtx_t *__restrict __mtx,
9449891e45SEd Schouten     const struct timespec *__restrict)
9549891e45SEd Schouten     __trylocks_exclusive(thrd_success, *__mtx);
9649891e45SEd Schouten int	mtx_trylock(mtx_t *__mtx)
9749891e45SEd Schouten     __trylocks_exclusive(thrd_success, *__mtx);
9849891e45SEd Schouten int	mtx_unlock(mtx_t *__mtx)
9949891e45SEd Schouten     __unlocks(*__mtx);
100fc6f0665SEd Schouten int	thrd_create(thrd_t *, thrd_start_t, void *);
101fc6f0665SEd Schouten thrd_t	thrd_current(void);
102fc6f0665SEd Schouten int	thrd_detach(thrd_t);
103fc6f0665SEd Schouten int	thrd_equal(thrd_t, thrd_t);
104fc6f0665SEd Schouten _Noreturn void
105fc6f0665SEd Schouten 	thrd_exit(int);
106fc6f0665SEd Schouten int	thrd_join(thrd_t, int *);
107fc6f0665SEd Schouten int	thrd_sleep(const struct timespec *, struct timespec *);
108fc6f0665SEd Schouten void	thrd_yield(void);
109fc6f0665SEd Schouten int	tss_create(tss_t *, tss_dtor_t);
110fc6f0665SEd Schouten void	tss_delete(tss_t);
111fc6f0665SEd Schouten void *	tss_get(tss_t);
112fc6f0665SEd Schouten int	tss_set(tss_t, void *);
113fc6f0665SEd Schouten __END_DECLS
114fc6f0665SEd Schouten 
115fc6f0665SEd Schouten #endif /* !_THREADS_H_ */
116