1/* $NetBSD: threads.h,v 1.2 2019/04/24 18:47:54 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 */
58typedef pthread_cond_t cnd_t;
59typedef pthread_t thrd_t;
60typedef pthread_key_t tss_t;
61typedef pthread_mutex_t mtx_t;
62typedef void (*tss_dtor_t) (void *);
63typedef int (*thrd_start_t) (void *);
64typedef pthread_once_t once_flag;
65
66/* ISO/IEC 9899:201x 7.26.1/5 */
67enum {
68 mtx_plain = 1,
69 mtx_recursive = 2,
70 mtx_timed = 4,
71 _MTX_MAXTYPE = 0x7fffffff
72};
73
74enum {
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 */
85void call_once(once_flag *, void (*)(void));
86
87/* ISO/IEC 9899:201x 7.26.3 Condition variable functions */
88int cnd_broadcast(cnd_t *);
89void cnd_destroy(cnd_t *);
90int cnd_init(cnd_t *);
91int cnd_signal(cnd_t *);
92int cnd_timedwait(cnd_t * __restrict, mtx_t * __restrict,
93 const struct timespec * __restrict);
94int cnd_wait(cnd_t *, mtx_t *);
95
96/* ISO/IEC 9899:201x 7.26.4 Mutex functions */
97void mtx_destroy(mtx_t *);
98int mtx_init(mtx_t *, int);
99int mtx_lock(mtx_t *);
100int mtx_timedlock(mtx_t *__restrict, const struct timespec *__restrict);
101int mtx_trylock(mtx_t *);
102int mtx_unlock(mtx_t *);
103
104/* ISO/IEC 9899:201x 7.26.5 Thread functions */
105int thrd_create(thrd_t *, thrd_start_t, void *);
106thrd_t thrd_current(void);
107int thrd_detach(thrd_t);
108int thrd_equal(thrd_t, thrd_t);
109_Noreturn void thrd_exit(int);
110int thrd_join(thrd_t, int *);
111int thrd_sleep(const struct timespec *, struct timespec *);
112void thrd_yield(void);
113
114/* ISO/IEC 9899:201x 7.26.6 Thread-specific storage functions */
115int tss_create(tss_t *, tss_dtor_t);
116void tss_delete(tss_t);
117void *tss_get(tss_t);
118int tss_set(tss_t, void *);
119__END_DECLS
120
121#endif
122