KallistiOS git master
Independent SDK for the Sega Dreamcast
Loading...
Searching...
No Matches
pthread.h
Go to the documentation of this file.
1/* KallistiOS ##version##
2
3 pthread.h
4 Copyright (C) 2023, 2024 Lawrence Sebald
5
6*/
7
8/** \file pthread.h
9 \brief POSIX threading support.
10 \ingroup threading_posix
11
12 This file contains functions and declarations related to POSIX threading
13 support. Please note that this support is not exactly actually POSIX-
14 compliant, but it provides much of the functionality that is useful in
15 porting code to KOS.
16
17 It is not recommended to use this POSIX threading support in code designed
18 specifically for KOS -- instead it is recommended to use the built-in
19 threading support provided in-kernel. Most of the support defined in this
20 file is just wrapper functions around the in-kernel functionality.
21
22 In-depth documentation of POSIX threading can be found in IEEE Std
23 1003.1-2017. An online version of the documentation for this header in that
24 specification can be found at the following URL:
25 https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/pthread.h.html
26
27 \author Lawrence Sebald
28*/
29
30#ifndef __PTHREAD_H
31#define __PTHREAD_H
32
33/** \cond **/
34
35#include <sys/cdefs.h>
36#include <sys/features.h>
37#include <sys/_pthreadtypes.h>
38
39#include <sched.h>
40#include <time.h>
41
42__BEGIN_DECLS
43
44/* Process shared/private flag. Since we don't support multiple processes, these
45 don't actually do anything different. */
46#define PTHREAD_PROCESS_PRIVATE 0
47#define PTHREAD_PROCESS_SHARED 1
48
49/* Scope handling. We only support PTHREAD_SCOPE_SYSTEM (although, we don't
50 actually support processes, so maybe they should be the same?) */
51#define PTHREAD_SCOPE_PROCESS 0
52#define PTHREAD_SCOPE_SYSTEM 1
53
54#define PTHREAD_CANCEL_DISABLE 0
55#define PTHREAD_CANCEL_ENABLE 1
56
57#define PTHREAD_CANCEL_DEFERRED 0
58#define PTHREAD_CANCEL_ASYNCHRONOUS 1
59
60#define PTHREAD_CREATE_DETACHED 0
61#define PTHREAD_CREATE_JOINABLE 1
62
63#define PTHREAD_STACK_MIN 256
64#define PTHREAD_STACK_MIN_ALIGNMENT 32
65
66/* Threads */
67int pthread_create(pthread_t *__RESTRICT thread,
68 const pthread_attr_t *__RESTRICT attr,
69 void *(*start_routine)(void *), void *__RESTRICT arg);
70int pthread_detach(pthread_t thread);
71int pthread_equal(pthread_t t1, pthread_t t2);
72void pthread_exit(void *value_ptr);
73int pthread_join(pthread_t thread, void **value_ptr);
74pthread_t pthread_self(void);
75int pthread_setschedprio(pthread_t thread, int prio);
76
77#if __GNU_VISIBLE || __BSD_VISIBLE
78int pthread_getname_np(pthread_t thread, char *buf, size_t buflen);
79int pthread_setname_np(pthread_t thread, const char *buf);
80#endif /* __GNU_VISIBLE || __BSD_VISIBLE */
81
82#if __BSD_VISIBLE
83int pthread_getprio(pthread_t thread);
84int pthread_setprio(pthread_t thread, int prio);
85#endif /* __BSD_VISIBLE */
86
87/* Thread attributes */
88int pthread_attr_init(pthread_attr_t *attr);
89int pthread_attr_destroy(pthread_attr_t *attr);
90
91int pthread_attr_getdetachstate(const pthread_attr_t *attr,
92 int *detachstate);
93int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);
94
95int pthread_attr_getguardsize(const pthread_attr_t *__RESTRICT attr,
96 size_t *__RESTRICT guardsize);
97int pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize);
98
99int pthread_attr_getschedparam(const pthread_attr_t *__RESTRICT attr,
100 struct sched_param *__RESTRICT param);
101int pthread_attr_setschedparam(pthread_attr_t *__RESTRICT attr,
102 const struct sched_param *__RESTRICT par);
103int pthread_attr_getstack(const pthread_attr_t *__RESTRICT attr,
104 void **__RESTRICT stackaddr,
105 size_t *__RESTRICT stacksize);
106int pthread_attr_setstack(pthread_attr_t *__RESTRICT attr,
107 void *__RESTRICT stackaddr, size_t stacksize);
108int pthread_attr_getstacksize(const pthread_attr_t *__RESTRICT attr,
109 size_t *__RESTRICT stacksize);
110int pthread_attr_setstacksize(pthread_attr_t *attr,
111 size_t stacksize);
112int pthread_attr_getscope(const pthread_attr_t *__RESTRICT attr,
113 int *__RESTRICT contentionscope);
114int pthread_attr_setscope(pthread_attr_t *attr, int contentionscope);
115int pthread_attr_getname_np(const pthread_attr_t *__RESTRICT attr,
116 char *__RESTRICT buf, size_t buflen);
117int pthread_attr_setname_np(pthread_attr_t *__RESTRICT attr,
118 const char *__RESTRICT name);
119
120/* Thread cancellation (Not supported) */
121int pthread_cancel(pthread_t thd);
122void pthread_testcancel(void);
123int pthread_setcancelstate(int state, int *oldstate);
124int pthread_setcanceltype(int type, int *oldtype);
125
126/* Condition variables */
127int pthread_cond_init(pthread_cond_t *__RESTRICT cond,
128 const pthread_condattr_t *__RESTRICT attr);
129int pthread_cond_destroy(pthread_cond_t *cond);
130
131#define PTHREAD_COND_INITIALIZER { .__data = { 0 } }
132
133int pthread_cond_broadcast(pthread_cond_t *cond);
134int pthread_cond_signal(pthread_cond_t *cond);
135int pthread_cond_wait(pthread_cond_t *__RESTRICT cond,
137int pthread_cond_timedwait(pthread_cond_t *__RESTRICT cond,
139 const struct timespec *__RESTRICT abstime);
140
141int pthread_condattr_init(pthread_condattr_t *attr);
142int pthread_condattr_destroy(pthread_condattr_t *attr);
143
144int pthread_condattr_getclock(const pthread_condattr_t *__RESTRICT attr,
145 clockid_t *__RESTRICT clock_id);
146int pthread_condattr_setclock(pthread_condattr_t *attr,
147 clockid_t clock_id);
148
149/* Thread-specific data */
150typedef int pthread_key_t;
151int pthread_key_create(pthread_key_t *key, void (*destructor)(void *));
152int pthread_key_delete(pthread_key_t key);
153void *pthread_getspecific(pthread_key_t key);
154int pthread_setspecific(pthread_key_t key, const void *value);
155
156/* Mutexes */
157int pthread_mutex_init(pthread_mutex_t *__RESTRICT mutex,
158 const pthread_mutexattr_t *__RESTRICT attr);
159int pthread_mutex_destroy(pthread_mutex_t *mutex);
160
161#define PTHREAD_MUTEX_INITIALIZER { .__data = { 0 } }
162#define PTHREAD_MUTEX_NORMAL 0
163#define PTHREAD_MUTEX_DEFAULT PTHREAD_MUTEX_NORMAL
164#define PTHREAD_MUTEX_ERRORCHECK 2
165#define PTHREAD_MUTEX_RECURSIVE 3
166
167#define PTHREAD_MUTEX_ROBUST 0
168#define PTHREAD_MUTEX_STALLED 1
169
170int pthread_mutex_lock(pthread_mutex_t *mutex);
171int pthread_mutex_trylock(pthread_mutex_t *mutex);
172int pthread_mutex_timedlock(pthread_mutex_t *__RESTRICT mutex,
173 const struct timespec *__RESTRICT abstime);
174int pthread_mutex_unlock(pthread_mutex_t *mutex);
175int pthread_mutex_consistent(pthread_mutex_t *mutex);
176
177int pthread_mutexattr_init(pthread_mutexattr_t *attr);
178int pthread_mutexattr_destroy(pthread_mutexattr_t *attr);
179
180int pthread_mutexattr_getrobust(const pthread_mutexattr_t *__RESTRICT at,
181 int *__RESTRICT robust);
182int pthread_mutexattr_setrobust(pthread_mutexattr_t *attr, int robust);
183
184int pthread_mutexattr_gettype(const pthread_mutexattr_t *__RESTRICT attr,
185 int *__RESTRICT type);
186int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type);
187
188/* Dynamic package initialization */
189typedef volatile int pthread_once_t;
190#define PTHREAD_ONCE_INIT 0
191
192int pthread_once(pthread_once_t *once_control, void (*init_routine)(void));
193
194/* Reader/writer locks */
195int pthread_rwlock_init(pthread_rwlock_t *__RESTRICT rwlock,
196 const pthread_rwlockattr_t *__RESTRICT attr);
197int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);
198
199#define PTHREAD_RWLOCK_INITIALIZER { .__data = { 0 } }
200
201int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
202int pthread_rwlock_timedrdlock(pthread_rwlock_t *__RESTRICT rwlock,
203 const struct timespec *__RESTRICT abstm);
204int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);
205int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
206int pthread_rwlock_timedwrlock(pthread_rwlock_t *__RESTRICT rwlock,
207 const struct timespec *__RESTRICT abstm);
208int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);
209int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);
210
211int pthread_rwlockattr_init(pthread_rwlockattr_t *attr);
212int pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr);
213
214/* Spin locks */
215typedef volatile int pthread_spinlock_t;
216
217int pthread_spin_init(pthread_spinlock_t *lock, int pshared);
218int pthread_spin_destroy(pthread_spinlock_t *lock);
219int pthread_spin_lock(pthread_spinlock_t *lock);
220int pthread_spin_trylock(pthread_spinlock_t *lock);
221int pthread_spin_unlock(pthread_spinlock_t *lock);
222
223/* Barriers */
224#define PTHREAD_BARRIER_SERIAL_THREAD 0x7fffffff
225
226int pthread_barrier_init(pthread_barrier_t *__RESTRICT barrier,
228 unsigned count);
229int pthread_barrier_destroy(pthread_barrier_t *barrier);
230int pthread_barrier_wait(pthread_barrier_t *barrier);
231
232int pthread_barrierattr_init(pthread_barrierattr_t *attr);
233int pthread_barrierattr_destroy(pthread_barrierattr_t *attr);
234
235/* Misc. */
236int pthread_getconcurrency(void);
237int pthread_setconcurrency(int new_level);
238int pthread_atfork(void (*prepare)(void), void (*parent)(void),
239 void (*child)(void));
240
241#if __GNU_VISIBLE || __BSD_VISIBLE
242/* Technically, the BSD prototype for this is to return void, not int.
243 Oh well... It always returns 0 anyway. */
244int pthread_yield(void);
245#endif /* __GNU_VISIBLE || __BSD_VISIBLE */
246
247__END_DECLS
248
249#endif /* !__PTHREAD_H */
250
251/** \endcond */
unsigned long int pthread_t
Definition _pthreadtypes.h:11
#define __RESTRICT
Definition cdefs.h:195
Basic sys/sched.h file for newlib.
Definition _pthreadtypes.h:35
Definition _pthreadtypes.h:13
Definition _pthreadtypes.h:18
Scheduling Parameters, P1003.1b-1993, p.
Definition sched.h:23
KOS-implementation of select C11 and POSIX extensions.
Definition _pthreadtypes.h:47
Definition _pthreadtypes.h:95
Definition _pthreadtypes.h:71
Definition _pthreadtypes.h:27
Definition _pthreadtypes.h:59
Definition _pthreadtypes.h:83