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 <kos/cdefs.h>
36#include <sys/features.h>
37#include <sys/_pthreadtypes.h>
38
39#include <sched.h>
40#include <time.h>
41
42#include <kos/cond.h>
43#include <kos/mutex.h>
44#include <kos/rwsem.h>
45
46__BEGIN_DECLS
47
48/* Process shared/private flag. Since we don't support multiple processes, these
49 don't actually do anything different. */
50#define PTHREAD_PROCESS_PRIVATE 0
51#define PTHREAD_PROCESS_SHARED 1
52
53/* Scope handling. We only support PTHREAD_SCOPE_SYSTEM (although, we don't
54 actually support processes, so maybe they should be the same?) */
55#define PTHREAD_SCOPE_PROCESS 0
56#define PTHREAD_SCOPE_SYSTEM 1
57
58#define PTHREAD_CANCEL_DISABLE 0
59#define PTHREAD_CANCEL_ENABLE 1
60
61#define PTHREAD_CANCEL_DEFERRED 0
62#define PTHREAD_CANCEL_ASYNCHRONOUS 1
63
64#define PTHREAD_CREATE_DETACHED 0
65#define PTHREAD_CREATE_JOINABLE 1
66
67#define PTHREAD_STACK_MIN 256
68#define PTHREAD_STACK_MIN_ALIGNMENT 32
69
70/* Threads */
71int pthread_create(pthread_t *__RESTRICT thread,
72 const pthread_attr_t *__RESTRICT attr,
73 void *(*start_routine)(void *), void *__RESTRICT arg);
74int pthread_detach(pthread_t thread);
75int pthread_equal(pthread_t t1, pthread_t t2);
76void pthread_exit(void *value_ptr);
77int pthread_join(pthread_t thread, void **value_ptr);
78pthread_t pthread_self(void);
79int pthread_setschedprio(pthread_t thread, int prio);
80
81#if __GNU_VISIBLE || __BSD_VISIBLE
82int pthread_getname_np(pthread_t thread, char *buf, size_t buflen);
83int pthread_setname_np(pthread_t thread, const char *buf);
84#endif /* __GNU_VISIBLE || __BSD_VISIBLE */
85
86#if __BSD_VISIBLE
87int pthread_getprio(pthread_t thread);
88int pthread_setprio(pthread_t thread, int prio);
89#endif /* __BSD_VISIBLE */
90
91/* Thread attributes */
92int pthread_attr_init(pthread_attr_t *attr);
93int pthread_attr_destroy(pthread_attr_t *attr);
94
95int pthread_attr_getdetachstate(const pthread_attr_t *attr,
96 int *detachstate);
97int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate);
98
99int pthread_attr_getguardsize(const pthread_attr_t *__RESTRICT attr,
100 size_t *__RESTRICT guardsize);
101int pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize);
102
103int pthread_attr_getschedparam(const pthread_attr_t *__RESTRICT attr,
104 struct sched_param *__RESTRICT param);
105int pthread_attr_setschedparam(pthread_attr_t *__RESTRICT attr,
106 const struct sched_param *__RESTRICT par);
107int pthread_attr_getstack(const pthread_attr_t *__RESTRICT attr,
108 void **__RESTRICT stackaddr,
109 size_t *__RESTRICT stacksize);
110int pthread_attr_setstack(pthread_attr_t *__RESTRICT attr,
111 void *__RESTRICT stackaddr, size_t stacksize);
112int pthread_attr_getstacksize(const pthread_attr_t *__RESTRICT attr,
113 size_t *__RESTRICT stacksize);
114int pthread_attr_setstacksize(pthread_attr_t *attr,
115 size_t stacksize);
116int pthread_attr_getscope(const pthread_attr_t *__RESTRICT attr,
117 int *__RESTRICT contentionscope);
118int pthread_attr_setscope(pthread_attr_t *attr, int contentionscope);
119int pthread_attr_getname_np(const pthread_attr_t *__RESTRICT attr,
120 char *__RESTRICT buf, size_t buflen);
121int pthread_attr_setname_np(pthread_attr_t *__RESTRICT attr,
122 const char *__RESTRICT name);
123
124/* Thread cancellation (Not supported) */
125int pthread_cancel(pthread_t thd);
126void pthread_testcancel(void);
127int pthread_setcancelstate(int state, int *oldstate);
128int pthread_setcanceltype(int type, int *oldtype);
129
130/* Condition variables */
131int pthread_cond_init(pthread_cond_t *__RESTRICT cond,
132 const pthread_condattr_t *__RESTRICT attr);
133int pthread_cond_destroy(pthread_cond_t *cond);
134
135#define PTHREAD_COND_INITIALIZER { .cond = COND_INITIALIZER }
136
137int pthread_cond_broadcast(pthread_cond_t *cond);
138int pthread_cond_signal(pthread_cond_t *cond);
139int pthread_cond_wait(pthread_cond_t *__RESTRICT cond,
141int pthread_cond_timedwait(pthread_cond_t *__RESTRICT cond,
143 const struct timespec *__RESTRICT abstime);
144
145int pthread_condattr_init(pthread_condattr_t *attr);
146int pthread_condattr_destroy(pthread_condattr_t *attr);
147
148int pthread_condattr_getclock(const pthread_condattr_t *__RESTRICT attr,
149 clockid_t *__RESTRICT clock_id);
150int pthread_condattr_setclock(pthread_condattr_t *attr,
151 clockid_t clock_id);
152
153/* Thread-specific data */
154typedef int pthread_key_t;
155int pthread_key_create(pthread_key_t *key, void (*destructor)(void *));
156int pthread_key_delete(pthread_key_t key);
157void *pthread_getspecific(pthread_key_t key);
158int pthread_setspecific(pthread_key_t key, const void *value);
159
160/* Mutexes */
161int pthread_mutex_init(pthread_mutex_t *__RESTRICT mutex,
162 const pthread_mutexattr_t *__RESTRICT attr);
163int pthread_mutex_destroy(pthread_mutex_t *mutex);
164
165#define PTHREAD_MUTEX_INITIALIZER { .mutex = MUTEX_INITIALIZER }
166#define PTHREAD_MUTEX_NORMAL 0
167#define PTHREAD_MUTEX_DEFAULT PTHREAD_MUTEX_NORMAL
168#define PTHREAD_MUTEX_ERRORCHECK 2
169#define PTHREAD_MUTEX_RECURSIVE 3
170
171#define PTHREAD_MUTEX_ROBUST 0
172#define PTHREAD_MUTEX_STALLED 1
173
174int pthread_mutex_lock(pthread_mutex_t *mutex);
175int pthread_mutex_trylock(pthread_mutex_t *mutex);
176int pthread_mutex_timedlock(pthread_mutex_t *__RESTRICT mutex,
177 const struct timespec *__RESTRICT abstime);
178int pthread_mutex_unlock(pthread_mutex_t *mutex);
179int pthread_mutex_consistent(pthread_mutex_t *mutex);
180
181int pthread_mutexattr_init(pthread_mutexattr_t *attr);
182int pthread_mutexattr_destroy(pthread_mutexattr_t *attr);
183
184int pthread_mutexattr_getrobust(const pthread_mutexattr_t *__RESTRICT at,
185 int *__RESTRICT robust);
186int pthread_mutexattr_setrobust(pthread_mutexattr_t *attr, int robust);
187
188int pthread_mutexattr_gettype(const pthread_mutexattr_t *__RESTRICT attr,
189 int *__RESTRICT type);
190int pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type);
191
192/* Dynamic package initialization */
193typedef volatile int pthread_once_t;
194#define PTHREAD_ONCE_INIT 0
195
196int pthread_once(pthread_once_t *once_control, void (*init_routine)(void));
197
198/* Reader/writer locks */
199int pthread_rwlock_init(pthread_rwlock_t *__RESTRICT rwlock,
200 const pthread_rwlockattr_t *__RESTRICT attr);
201int pthread_rwlock_destroy(pthread_rwlock_t *rwlock);
202
203#define PTHREAD_RWLOCK_INITIALIZER { .rwsem = RWSEM_INITIALIZER }
204
205int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock);
206int pthread_rwlock_timedrdlock(pthread_rwlock_t *__RESTRICT rwlock,
207 const struct timespec *__RESTRICT abstm);
208int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock);
209int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock);
210int pthread_rwlock_timedwrlock(pthread_rwlock_t *__RESTRICT rwlock,
211 const struct timespec *__RESTRICT abstm);
212int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock);
213int pthread_rwlock_unlock(pthread_rwlock_t *rwlock);
214
215int pthread_rwlockattr_init(pthread_rwlockattr_t *attr);
216int pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr);
217
218/* Spin locks */
219typedef volatile int pthread_spinlock_t;
220
221int pthread_spin_init(pthread_spinlock_t *lock, int pshared);
222int pthread_spin_destroy(pthread_spinlock_t *lock);
223int pthread_spin_lock(pthread_spinlock_t *lock);
224int pthread_spin_trylock(pthread_spinlock_t *lock);
225int pthread_spin_unlock(pthread_spinlock_t *lock);
226
227/* Barriers */
228#define PTHREAD_BARRIER_SERIAL_THREAD 0x7fffffff
229
230int pthread_barrier_init(pthread_barrier_t *__RESTRICT barrier,
232 unsigned count);
233int pthread_barrier_destroy(pthread_barrier_t *barrier);
234int pthread_barrier_wait(pthread_barrier_t *barrier);
235
236int pthread_barrierattr_init(pthread_barrierattr_t *attr);
237int pthread_barrierattr_destroy(pthread_barrierattr_t *attr);
238
239/* Misc. */
240int pthread_getconcurrency(void);
241int pthread_setconcurrency(int new_level);
242int pthread_atfork(void (*prepare)(void), void (*parent)(void),
243 void (*child)(void));
244
245#if __GNU_VISIBLE || __BSD_VISIBLE
246/* Technically, the BSD prototype for this is to return void, not int.
247 Oh well... It always returns 0 anyway. */
248int pthread_yield(void);
249#endif /* __GNU_VISIBLE || __BSD_VISIBLE */
250
251__END_DECLS
252
253/** \endcond */
254
255#endif /* !__PTHREAD_H */
unsigned long int pthread_t
Definition _pthreadtypes.h:18
static int thread(void *arg)
Definition atomics.c:112
thd_barrier_t barrier
Definition barrier.c:37
Various common macros used throughout the codebase.
void * thd(void *v)
Definition compiler_tls.c:63
Condition variables.
int key
Definition nehe26.c:48
int cond[LOCSIZ]
Definition globalvars.c:35
#define __RESTRICT
Definition cdefs.h:98
int at(int objj)
Definition subr.c:79
Mutual exclusion locks.
static mutex_t lock
Definition once_test.c:30
static int buflen
Definition porthelper.c:12
Definition for a reader/writer semaphore.
Definition _pthreadtypes.h:42
Definition _pthreadtypes.h:20
Definition _pthreadtypes.h:25
KOS-implementation of select C11 and POSIX extensions.
static void destructor(void *data)
Definition tls_test.c:35
Definition _pthreadtypes.h:54
Definition _pthreadtypes.h:105
Definition _pthreadtypes.h:79
Definition _pthreadtypes.h:34
Definition _pthreadtypes.h:66
Definition _pthreadtypes.h:92