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