1 #ifndef _PTHREAD_H
2 #define _PTHREAD_H
3 #ifdef __cplusplus
4 extern "C" {
5 #endif
6 
7 #include <features.h>
8 
9 #define __NEED_time_t
10 #define __NEED_clockid_t
11 #define __NEED_struct_timespec
12 #define __NEED_sigset_t
13 #define __NEED_pthread_t
14 #define __NEED_pthread_attr_t
15 #define __NEED_pthread_mutexattr_t
16 #define __NEED_pthread_condattr_t
17 #define __NEED_pthread_rwlockattr_t
18 #define __NEED_pthread_barrierattr_t
19 #define __NEED_pthread_mutex_t
20 #define __NEED_pthread_cond_t
21 #define __NEED_pthread_rwlock_t
22 #define __NEED_pthread_barrier_t
23 #define __NEED_pthread_spinlock_t
24 #define __NEED_pthread_key_t
25 #define __NEED_pthread_once_t
26 #define __NEED_size_t
27 
28 #include <bits/alltypes.h>
29 
30 #include <sched.h>
31 #include <time.h>
32 
33 #define PTHREAD_CREATE_JOINABLE 0
34 #define PTHREAD_CREATE_DETACHED 1
35 
36 #define PTHREAD_MUTEX_NORMAL 0
37 #define PTHREAD_MUTEX_DEFAULT 0
38 #define PTHREAD_MUTEX_RECURSIVE 1
39 #define PTHREAD_MUTEX_ERRORCHECK 2
40 #define PTHREAD_MUTEX_DESTROYED (-1)
41 
42 #define PTHREAD_MUTEX_STALLED 0
43 #define PTHREAD_MUTEX_ROBUST 1
44 
45 #define PTHREAD_PRIO_NONE 0
46 #define PTHREAD_PRIO_INHERIT 1
47 #define PTHREAD_PRIO_PROTECT 2
48 
49 #define PTHREAD_INHERIT_SCHED 0
50 #define PTHREAD_EXPLICIT_SCHED 1
51 
52 #define PTHREAD_SCOPE_SYSTEM 0
53 #define PTHREAD_SCOPE_PROCESS 1
54 
55 #define PTHREAD_PROCESS_PRIVATE 0
56 #define PTHREAD_PROCESS_SHARED 1
57 
58 
59 #define PTHREAD_MUTEX_INITIALIZER {{{0}}}
60 #define PTHREAD_RWLOCK_INITIALIZER {{{0}}}
61 #define PTHREAD_COND_INITIALIZER {{{0}}}
62 #define PTHREAD_ONCE_INIT 0
63 
64 
65 #define PTHREAD_CANCEL_ENABLE 0
66 #define PTHREAD_CANCEL_DISABLE 1
67 #define PTHREAD_CANCEL_MASKED 2
68 
69 #define PTHREAD_CANCEL_DEFERRED 0
70 #define PTHREAD_CANCEL_ASYNCHRONOUS 1
71 
72 #define PTHREAD_CANCELED ((void *)-1)
73 
74 
75 #define PTHREAD_BARRIER_SERIAL_THREAD (-1)
76 
77 
78 #define PTHREAD_NULL ((pthread_t)0)
79 
80 
81 int pthread_create(pthread_t *__restrict, const pthread_attr_t *__restrict, void *(*)(void *), void *__restrict);
82 int pthread_detach(pthread_t);
83 _Noreturn void pthread_exit(void *);
84 int pthread_join(pthread_t, void **);
85 pid_t pthread_gettid_np(pthread_t);
86 
87 #ifdef __GNUC__
88 __attribute__((const))
89 #endif
90 pthread_t pthread_self(void);
91 
92 int pthread_equal(pthread_t, pthread_t);
93 #ifndef __cplusplus
94 #define pthread_equal(x,y) ((x)==(y))
95 #endif
96 
97 int pthread_setcancelstate(int, int *);
98 int pthread_setcanceltype(int, int *);
99 void pthread_testcancel(void);
100 int pthread_cancel(pthread_t);
101 
102 int pthread_getschedparam(pthread_t, int *__restrict, struct sched_param *__restrict);
103 int pthread_setschedparam(pthread_t, int, const struct sched_param *);
104 int pthread_setschedprio(pthread_t, int);
105 
106 int pthread_once(pthread_once_t *, void (*)(void));
107 
108 int pthread_mutex_init(pthread_mutex_t *__restrict, const pthread_mutexattr_t *__restrict);
109 int pthread_mutex_lock(pthread_mutex_t *);
110 int pthread_mutex_unlock(pthread_mutex_t *);
111 int pthread_mutex_trylock(pthread_mutex_t *);
112 int pthread_mutex_timedlock(pthread_mutex_t *__restrict, const struct timespec *__restrict);
113 int pthread_mutex_destroy(pthread_mutex_t *);
114 int pthread_mutex_consistent(pthread_mutex_t *);
115 /**
116   * @brief lock the mutex object referenced by mutex. If the mutex is already locked,
117   *        the calling thread shall block until the mutex becomes available as in the
118   *        pthread_mutex_lock() function. If the mutex cannot be locked without waiting for
119   *        another thread to unlock the mutex, this wait shall be terminated when the specified
120   *        timeout expires. The timeout shall be based on the CLOCK_REALTIME or CLOCK_MONOTONIC clock.
121   *        The resolution of the timeout shall be the resolution of the clock on which it is based.
122   * @param mutex a robust mutex and the process containing the owning thread terminated while holding the mutex lock.
123   * @param clock_id specified CLOCK_REALTIME or CLOCK_MONOTONIC clock.
124   * @param timespec the timeout shall expire specified by abstime passes.
125   * @return clocklock result.
126   * @retval 0 is returned on success.
127   * @retval -1 is returned on failure, and errno is set to indicate the error.
128   */
129 int pthread_mutex_clocklock(pthread_mutex_t *__restrict, clockid_t, const struct timespec *__restrict);
130 /**
131   * @brief lock the mutex object referenced by mutex. If the mutex is already locked,
132   *        the calling thread shall block until the mutex becomes available as in the
133   *        pthread_mutex_lock() function. If the mutex cannot be locked without waiting for
134   *        another thread to unlock the mutex, this wait shall be terminated when the specified
135   *        timeout expires. The timeout shall be based on the CLOCK_MONOTONIC clock.
136   *        The resolution of the timeout shall be the resolution of the clock on which it is based.
137   * @param mutex a robust mutex and the process containing the owning thread terminated while holding the mutex lock.
138   * @param timespec the timeout shall expire specified by abstime passes.
139   * @return clocklock result.
140   * @retval 0 is returned on success.
141   * @retval -1 is returned on failure, and errno is set to indicate the error.
142   */
143 int pthread_mutex_timedlock_monotonic_np(pthread_mutex_t *__restrict, const struct timespec *__restrict);
144 /**
145   * @brief lock the mutex object referenced by mutex. If the mutex is already locked,
146   *        the calling thread shall block until the mutex becomes available as in the
147   *        pthread_mutex_lock() function. If the mutex cannot be locked without waiting for
148   *        another thread to unlock the mutex, this wait shall be terminated when the specified
149   *        timeout expires. The timeout shall be based on the CLOCK_MONOTONIC clock.
150   *        The resolution of the timeout shall be the resolution of the clock on which it is based.
151   * @param mutex a robust mutex and the process containing the owning thread terminated while holding the mutex lock.
152   * @param ms the timeout shall expire specified by relative time(ms) passes.
153   * @return clocklock result.
154   * @retval 0 is returned on success.
155   * @retval -1 is returned on failure, and errno is set to indicate the error.
156   */
157 int pthread_mutex_lock_timeout_np(pthread_mutex_t *__restrict, unsigned int);
158 /**
159   * @brief The thread waits for a signal to trigger, and if timeout or signal is triggered,
160   *        the thread wakes up.
161   * @param pthread_cond_t Condition variables for multithreading.
162   * @param pthread_mutex_t Thread mutex variable.
163   * @param clockid_t Clock ID used in clock and timer functions.
164   * @param timespec The timeout shall expire specified by abstime passes.
165   * @return pthread_cond_clockwait result.
166   * @retval 0 pthread_cond_clockwait successful.
167   * @retval ETIMEDOUT pthread_cond_clockwait Connection timed out.
168   * @retval EINVAL pthread_cond_clockwait error.
169   */
170 int pthread_cond_clockwait(pthread_cond_t *__restrict, pthread_mutex_t *__restrict,
171                            clockid_t, const struct timespec *__restrict);
172 
173 /**
174   * @brief Condition variables have an initialization option to use CLOCK_MONOTONIC.
175   *        The thread waits for a signal to trigger, and if timeout or signal is triggered,
176   *        the thread wakes up.
177   * @param pthread_cond_t Condition variables for multithreading.
178   * @param pthread_mutex_t Thread mutex variable.
179   * @param timespec The timeout shall expire specified by abstime passes.
180   * @return pthread_cond_timedwait_monotonic_np result.
181   * @retval 0 pthread_cond_timedwait_monotonic_np successful.
182   * @retval ETIMEDOUT pthread_cond_timedwait_monotonic_np Connection timed out.
183   * @retval EINVAL pthread_cond_timedwait_monotonic_np error.
184   */
185 int pthread_cond_timedwait_monotonic_np(pthread_cond_t *__restrict, pthread_mutex_t *__restrict,
186                                         const struct timespec *__restrict);
187 
188 /**
189   * @brief Condition variables have an initialization option to use CLOCK_MONOTONIC and The time
190   *        parameter is in milliseconds. The thread waits for a signal to trigger, and if timeout or
191   *        signal is triggered, the thread wakes up.
192   * @param pthread_cond_t Condition variables for multithreading.
193   * @param pthread_mutex_t Thread mutex variable.
194   * @param unsigned Timeout, in milliseconds.
195   * @return pthread_cond_timeout_np result.
196   * @retval 0 pthread_cond_timeout_np successful.
197   * @retval ETIMEDOUT pthread_cond_timeout_np Connection timed out.
198   * @retval EINVAL pthread_cond_timeout_np error.
199   */
200 int pthread_cond_timeout_np(pthread_cond_t* __restrict, pthread_mutex_t* __restrict, unsigned int);
201 /**
202   * @brief Apply a read lock to the read-write lock referenced by rwlock as in the
203   *        pthread_rwlock_rdlock() function. However, if the lock cannot be acquired without
204   *        waiting for other threads to unlock the lock, this wait shall be terminated when
205   *        the specified timeout expires. The timeout shall expire when the absolute time specified by
206   *        abstime passes, as measured by the clock on which timeouts are based, or if the absolute time
207   *        specified by abstime has already been passed at the time of the call.
208   *        The timeout shall be based on the CLOCK_REALTIME or CLOCK_MONOTONIC clock.
209   * @param rw a read lock to the read-write lock referenced.
210   * @param clock_id specified CLOCK_REALTIME or CLOCK_MONOTONIC clock.
211   * @param timespec the timeout shall expire specified by abstime passes.
212   * @return clockrdlock result.
213   * @retval 0 is returned on success.
214   * @retval -1 is returned on failure, and errno is set to indicate the error.
215   */
216 int pthread_rwlock_clockrdlock(pthread_rwlock_t *__restrict, clockid_t, const struct timespec *__restrict);
217 /**
218   * @brief Apply a read lock to the read-write lock referenced by rwlock as in the
219   *        pthread_rwlock_rdlock() function. However, if the lock cannot be acquired without
220   *        waiting for other threads to unlock the lock, this wait shall be terminated when
221   *        the specified timeout expires. The timeout shall expire when the absolute time specified by
222   *        abstime passes, as measured by the clock on which timeouts are based, or if the absolute time
223   *        specified by abstime has already been passed at the time of the call.
224   *        The timeout shall be based on the CLOCK_MONOTONIC clock.
225   * @param rw a read lock to the read-write lock referenced.
226   * @param timespec the timeout shall expire specified by abstime passes.
227   * @return clockrdlock result.
228   * @retval 0 is returned on success.
229   * @retval -1 is returned on failure, and errno is set to indicate the error.
230   */
231 int pthread_rwlock_timedrdlock_monotonic_np(pthread_rwlock_t *__restrict, const struct timespec *__restrict);
232 /**
233   * @brief Read-write lock variables have an initialization option to use CLOCK_MONOTONIC.
234   *        apply a read lock to the read-write lock referenced by rwlock as in the
235   *        pthread_rwlock_wrlock() function. However, if the lock cannot be acquired without
236   *        waiting for other threads to unlock the lock, this wait shall be terminated when
237   *        the specified timeout expires. The timeout shall expire when the absolute time specified by
238   *        abstime passes, as measured by the clock on which timeouts are based, or if the absolute time
239   *        specified by abstime has already been passed at the time of the call.
240   *        The timeout shall be based on the CLOCK_MONOTONIC clock.
241   * @param rw a read lock to the read-write lock referenced.
242   * @param timespec the timeout shall expire specified by abstime passes.
243   * @return clockrdlock result.
244   * @retval 0 is returned on success.
245   * @retval -1 is returned on failure, and errno is set to indicate the error.
246   */
247 int pthread_rwlock_timedwrlock_monotonic_np(pthread_rwlock_t *__restrict, const struct timespec *__restrict);
248 
249 /**
250   * @brief Apply a read lock to the read-write lock referenced by rwlock as in the
251   *        pthread_rwlock_wrlock() function. However, if the lock cannot be acquired without
252   *        waiting for other threads to unlock the lock, this wait shall be terminated when
253   *        the specified timeout expires. The timeout shall expire when the absolute time specified by
254   *        abstime passes, as measured by the clock on which timeouts are based, or if the absolute time
255   *        specified by abstime has already been passed at the time of the call.
256   *        The timeout shall be based on the CLOCK_REALTIME or CLOCK_MONOTONIC clock.
257   * @param rw a read lock to the read-write lock referenced.
258   * @param clock_id specified CLOCK_REALTIME or CLOCK_MONOTONIC clock.
259   * @param timespec the timeout shall expire specified by abstime passes.
260   * @return clockrdlock result.
261   * @retval 0 is returned on success.
262   * @retval -1 is returned on failure, and errno is set to indicate the error.
263   */
264 int pthread_rwlock_clockwrlock(pthread_rwlock_t *__restrict, clockid_t, const struct timespec *__restrict);
265 int pthread_mutex_getprioceiling(const pthread_mutex_t *__restrict, int *__restrict);
266 int pthread_mutex_setprioceiling(pthread_mutex_t *__restrict, int, int *__restrict);
267 
268 int pthread_cond_init(pthread_cond_t *__restrict, const pthread_condattr_t *__restrict);
269 int pthread_cond_destroy(pthread_cond_t *);
270 int pthread_cond_wait(pthread_cond_t *__restrict, pthread_mutex_t *__restrict);
271 int pthread_cond_timedwait(pthread_cond_t *__restrict, pthread_mutex_t *__restrict, const struct timespec *__restrict);
272 int pthread_cond_broadcast(pthread_cond_t *);
273 int pthread_cond_signal(pthread_cond_t *);
274 
275 int pthread_rwlock_init(pthread_rwlock_t *__restrict, const pthread_rwlockattr_t *__restrict);
276 int pthread_rwlock_destroy(pthread_rwlock_t *);
277 int pthread_rwlock_rdlock(pthread_rwlock_t *);
278 int pthread_rwlock_tryrdlock(pthread_rwlock_t *);
279 int pthread_rwlock_timedrdlock(pthread_rwlock_t *__restrict, const struct timespec *__restrict);
280 int pthread_rwlock_wrlock(pthread_rwlock_t *);
281 int pthread_rwlock_trywrlock(pthread_rwlock_t *);
282 int pthread_rwlock_timedwrlock(pthread_rwlock_t *__restrict, const struct timespec *__restrict);
283 int pthread_rwlock_unlock(pthread_rwlock_t *);
284 
285 int pthread_spin_init(pthread_spinlock_t *, int);
286 int pthread_spin_destroy(pthread_spinlock_t *);
287 int pthread_spin_lock(pthread_spinlock_t *);
288 int pthread_spin_trylock(pthread_spinlock_t *);
289 int pthread_spin_unlock(pthread_spinlock_t *);
290 
291 int pthread_barrier_init(pthread_barrier_t *__restrict, const pthread_barrierattr_t *__restrict, unsigned);
292 int pthread_barrier_destroy(pthread_barrier_t *);
293 int pthread_barrier_wait(pthread_barrier_t *);
294 
295 int pthread_key_create(pthread_key_t *, void (*)(void *));
296 int pthread_key_delete(pthread_key_t);
297 void *pthread_getspecific(pthread_key_t);
298 int pthread_setspecific(pthread_key_t, const void *);
299 
300 int pthread_attr_init(pthread_attr_t *);
301 int pthread_attr_destroy(pthread_attr_t *);
302 
303 int pthread_attr_getguardsize(const pthread_attr_t *__restrict, size_t *__restrict);
304 int pthread_attr_setguardsize(pthread_attr_t *, size_t);
305 int pthread_attr_getstacksize(const pthread_attr_t *__restrict, size_t *__restrict);
306 int pthread_attr_setstacksize(pthread_attr_t *, size_t);
307 int pthread_attr_getdetachstate(const pthread_attr_t *, int *);
308 int pthread_attr_setdetachstate(pthread_attr_t *, int);
309 int pthread_attr_getstack(const pthread_attr_t *__restrict, void **__restrict, size_t *__restrict);
310 int pthread_attr_setstack(pthread_attr_t *, void *, size_t);
311 int pthread_attr_getscope(const pthread_attr_t *__restrict, int *__restrict);
312 int pthread_attr_setscope(pthread_attr_t *, int);
313 int pthread_attr_getschedpolicy(const pthread_attr_t *__restrict, int *__restrict);
314 int pthread_attr_setschedpolicy(pthread_attr_t *, int);
315 int pthread_attr_getschedparam(const pthread_attr_t *__restrict, struct sched_param *__restrict);
316 int pthread_attr_setschedparam(pthread_attr_t *__restrict, const struct sched_param *__restrict);
317 int pthread_attr_getinheritsched(const pthread_attr_t *__restrict, int *__restrict);
318 int pthread_attr_setinheritsched(pthread_attr_t *, int);
319 
320 int pthread_mutexattr_destroy(pthread_mutexattr_t *);
321 int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *__restrict, int *__restrict);
322 int pthread_mutexattr_getprotocol(const pthread_mutexattr_t *__restrict, int *__restrict);
323 int pthread_mutexattr_getpshared(const pthread_mutexattr_t *__restrict, int *__restrict);
324 int pthread_mutexattr_getrobust(const pthread_mutexattr_t *__restrict, int *__restrict);
325 int pthread_mutexattr_gettype(const pthread_mutexattr_t *__restrict, int *__restrict);
326 int pthread_mutexattr_init(pthread_mutexattr_t *);
327 int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *, int);
328 int pthread_mutexattr_setprotocol(pthread_mutexattr_t *, int);
329 int pthread_mutexattr_setpshared(pthread_mutexattr_t *, int);
330 int pthread_mutexattr_setrobust(pthread_mutexattr_t *, int);
331 int pthread_mutexattr_settype(pthread_mutexattr_t *, int);
332 
333 int pthread_condattr_init(pthread_condattr_t *);
334 int pthread_condattr_destroy(pthread_condattr_t *);
335 int pthread_condattr_setclock(pthread_condattr_t *, clockid_t);
336 int pthread_condattr_setpshared(pthread_condattr_t *, int);
337 int pthread_condattr_getclock(const pthread_condattr_t *__restrict, clockid_t *__restrict);
338 int pthread_condattr_getpshared(const pthread_condattr_t *__restrict, int *__restrict);
339 
340 int pthread_rwlockattr_init(pthread_rwlockattr_t *);
341 int pthread_rwlockattr_destroy(pthread_rwlockattr_t *);
342 int pthread_rwlockattr_setpshared(pthread_rwlockattr_t *, int);
343 int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *__restrict, int *__restrict);
344 
345 int pthread_barrierattr_destroy(pthread_barrierattr_t *);
346 int pthread_barrierattr_getpshared(const pthread_barrierattr_t *__restrict, int *__restrict);
347 int pthread_barrierattr_init(pthread_barrierattr_t *);
348 int pthread_barrierattr_setpshared(pthread_barrierattr_t *, int);
349 
350 int pthread_atfork(void (*)(void), void (*)(void), void (*)(void));
351 int pthread_atfork_for_gwpasan(void (*)(void), void (*)(void), void (*)(void));
352 
353 int pthread_getconcurrency(void);
354 int pthread_setconcurrency(int);
355 
356 int pthread_getcpuclockid(pthread_t, clockid_t *);
357 
358 struct __ptcb {
359 	void (*__f)(void *);
360 	void *__x;
361 	struct __ptcb *__next;
362 };
363 
364 void _pthread_cleanup_push(struct __ptcb *, void (*)(void *), void *);
365 void _pthread_cleanup_pop(struct __ptcb *, int);
366 
367 #define pthread_cleanup_push(f, x) do { struct __ptcb __cb; _pthread_cleanup_push(&__cb, f, x);
368 #define pthread_cleanup_pop(r) _pthread_cleanup_pop(&__cb, (r)); } while(0)
369 
370 #ifdef _GNU_SOURCE
371 struct cpu_set_t;
372 int pthread_getaffinity_np(pthread_t, size_t, struct cpu_set_t *);
373 int pthread_setaffinity_np(pthread_t, size_t, const struct cpu_set_t *);
374 int pthread_getattr_np(pthread_t, pthread_attr_t *);
375 int pthread_setname_np(pthread_t, const char *);
376 int pthread_getname_np(pthread_t, char *, size_t);
377 int pthread_getattr_default_np(pthread_attr_t *);
378 int pthread_setattr_default_np(const pthread_attr_t *);
379 int pthread_tryjoin_np(pthread_t, void **);
380 int pthread_timedjoin_np(pthread_t, void **, const struct timespec *);
381 #endif
382 
383 #if _REDIR_TIME64
384 __REDIR(pthread_mutex_timedlock, __pthread_mutex_timedlock_time64);
385 __REDIR(pthread_cond_timedwait, __pthread_cond_timedwait_time64);
386 __REDIR(pthread_rwlock_timedrdlock, __pthread_rwlock_timedrdlock_time64);
387 __REDIR(pthread_rwlock_timedwrlock, __pthread_rwlock_timedwrlock_time64);
388 #ifdef _GNU_SOURCE
389 __REDIR(pthread_timedjoin_np, __pthread_timedjoin_np_time64);
390 #endif
391 #endif
392 
393 #ifdef __cplusplus
394 }
395 #endif
396 #endif
397