• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #define PTHREAD_NULL ((pthread_t)0)
89 
90 
91 int pthread_create(pthread_t *__restrict, const pthread_attr_t *__restrict, void *(*)(void *), void *__restrict);
92 int pthread_detach(pthread_t);
93 _Noreturn void pthread_exit(void *);
94 int pthread_join(pthread_t, void **);
95 pid_t pthread_gettid_np(pthread_t);
96 
97 #ifdef __GNUC__
98 __attribute__((const))
99 #endif
100 pthread_t pthread_self(void);
101 
102 int pthread_equal(pthread_t, pthread_t);
103 #ifndef __cplusplus
104 #define pthread_equal(x,y) ((x)==(y))
105 #endif
106 
107 int pthread_setcancelstate(int, int *);
108 int pthread_setcanceltype(int, int *);
109 void pthread_testcancel(void);
110 int pthread_cancel(pthread_t);
111 
112 int pthread_getschedparam(pthread_t, int *__restrict, struct sched_param *__restrict);
113 int pthread_setschedparam(pthread_t, int, const struct sched_param *);
114 int pthread_setschedprio(pthread_t, int);
115 
116 int pthread_once(pthread_once_t *, void (*)(void));
117 
118 int pthread_mutex_init(pthread_mutex_t *__restrict, const pthread_mutexattr_t *__restrict);
119 int pthread_mutex_lock(pthread_mutex_t *);
120 int pthread_mutex_unlock(pthread_mutex_t *);
121 int pthread_mutex_trylock(pthread_mutex_t *);
122 int pthread_mutex_timedlock(pthread_mutex_t *__restrict, const struct timespec *__restrict);
123 int pthread_mutex_destroy(pthread_mutex_t *);
124 int pthread_mutex_consistent(pthread_mutex_t *);
125 /**
126   * @brief lock the mutex object referenced by mutex. If the mutex is already locked,
127   *        the calling thread shall block until the mutex becomes available as in the
128   *        pthread_mutex_lock() function. If the mutex cannot be locked without waiting for
129   *        another thread to unlock the mutex, this wait shall be terminated when the specified
130   *        timeout expires. The timeout shall be based on the CLOCK_REALTIME or CLOCK_MONOTONIC clock.
131   *        The resolution of the timeout shall be the resolution of the clock on which it is based.
132   * @param mutex a robust mutex and the process containing the owning thread terminated while holding the mutex lock.
133   * @param clock_id specified CLOCK_REALTIME or CLOCK_MONOTONIC clock.
134   * @param timespec the timeout shall expire specified by abstime passes.
135   * @return clocklock result.
136   * @retval 0 is returned on success.
137   * @retval -1 is returned on failure, and errno is set to indicate the error.
138   */
139 int pthread_mutex_clocklock(pthread_mutex_t *__restrict, clockid_t, const struct timespec *__restrict);
140 /**
141   * @brief lock the mutex object referenced by mutex. If the mutex is already locked,
142   *        the calling thread shall block until the mutex becomes available as in the
143   *        pthread_mutex_lock() function. If the mutex cannot be locked without waiting for
144   *        another thread to unlock the mutex, this wait shall be terminated when the specified
145   *        timeout expires. The timeout shall be based on the CLOCK_MONOTONIC clock.
146   *        The resolution of the timeout shall be the resolution of the clock on which it is based.
147   * @param mutex a robust mutex and the process containing the owning thread terminated while holding the mutex lock.
148   * @param timespec the timeout shall expire specified by abstime passes.
149   * @return clocklock result.
150   * @retval 0 is returned on success.
151   * @retval -1 is returned on failure, and errno is set to indicate the error.
152   */
153 int pthread_mutex_timedlock_monotonic_np(pthread_mutex_t *__restrict, const struct timespec *__restrict);
154 /**
155   * @brief lock the mutex object referenced by mutex. If the mutex is already locked,
156   *        the calling thread shall block until the mutex becomes available as in the
157   *        pthread_mutex_lock() function. If the mutex cannot be locked without waiting for
158   *        another thread to unlock the mutex, this wait shall be terminated when the specified
159   *        timeout expires. The timeout shall be based on the CLOCK_MONOTONIC clock.
160   *        The resolution of the timeout shall be the resolution of the clock on which it is based.
161   * @param mutex a robust mutex and the process containing the owning thread terminated while holding the mutex lock.
162   * @param ms the timeout shall expire specified by relative time(ms) passes.
163   * @return clocklock result.
164   * @retval 0 is returned on success.
165   * @retval -1 is returned on failure, and errno is set to indicate the error.
166   */
167 int pthread_mutex_lock_timeout_np(pthread_mutex_t *__restrict, unsigned int);
168 
169 int pthread_mutex_getprioceiling(const pthread_mutex_t *__restrict, int *__restrict);
170 int pthread_mutex_setprioceiling(pthread_mutex_t *__restrict, int, int *__restrict);
171 
172 int pthread_cond_init(pthread_cond_t *__restrict, const pthread_condattr_t *__restrict);
173 int pthread_cond_destroy(pthread_cond_t *);
174 int pthread_cond_wait(pthread_cond_t *__restrict, pthread_mutex_t *__restrict);
175 int pthread_cond_timedwait(pthread_cond_t *__restrict, pthread_mutex_t *__restrict, const struct timespec *__restrict);
176 /**
177   * @brief The thread waits for a signal to trigger, and if timeout or signal is triggered,
178   *        the thread wakes up.
179   * @param pthread_cond_t Condition variables for multithreading.
180   * @param pthread_mutex_t Thread mutex variable.
181   * @param clockid_t Clock ID used in clock and timer functions.
182   * @param timespec The timeout shall expire specified by abstime passes.
183   * @return pthread_cond_clockwait result.
184   * @retval 0 pthread_cond_clockwait successful.
185   * @retval ETIMEDOUT pthread_cond_clockwait Connection timed out.
186   * @retval EINVAL pthread_cond_clockwait error.
187   */
188 int pthread_cond_clockwait(pthread_cond_t *__restrict, pthread_mutex_t *__restrict,
189                            clockid_t, const struct timespec *__restrict);
190 
191 /**
192   * @brief Condition variables have an initialization option to use CLOCK_MONOTONIC.
193   *        The thread waits for a signal to trigger, and if timeout or signal is triggered,
194   *        the thread wakes up.
195   * @param pthread_cond_t Condition variables for multithreading.
196   * @param pthread_mutex_t Thread mutex variable.
197   * @param timespec The timeout shall expire specified by abstime passes.
198   * @return pthread_cond_timedwait_monotonic_np result.
199   * @retval 0 pthread_cond_timedwait_monotonic_np successful.
200   * @retval ETIMEDOUT pthread_cond_timedwait_monotonic_np Connection timed out.
201   * @retval EINVAL pthread_cond_timedwait_monotonic_np error.
202   */
203 int pthread_cond_timedwait_monotonic_np(pthread_cond_t *__restrict, pthread_mutex_t *__restrict,
204                                         const struct timespec *__restrict);
205 
206 /**
207   * @brief Condition variables have an initialization option to use CLOCK_MONOTONIC and The time
208   *        parameter is in milliseconds. The thread waits for a signal to trigger, and if timeout or
209   *        signal is triggered, the thread wakes up.
210   * @param pthread_cond_t Condition variables for multithreading.
211   * @param pthread_mutex_t Thread mutex variable.
212   * @param unsigned Timeout, in milliseconds.
213   * @return pthread_cond_timeout_np result.
214   * @retval 0 pthread_cond_timeout_np successful.
215   * @retval ETIMEDOUT pthread_cond_timeout_np Connection timed out.
216   * @retval EINVAL pthread_cond_timeout_np error.
217   */
218 int pthread_cond_timeout_np(pthread_cond_t* __restrict, pthread_mutex_t* __restrict, unsigned int);
219 int pthread_cond_broadcast(pthread_cond_t *);
220 int pthread_cond_signal(pthread_cond_t *);
221 
222 int pthread_rwlock_init(pthread_rwlock_t *__restrict, const pthread_rwlockattr_t *__restrict);
223 int pthread_rwlock_destroy(pthread_rwlock_t *);
224 int pthread_rwlock_rdlock(pthread_rwlock_t *);
225 int pthread_rwlock_tryrdlock(pthread_rwlock_t *);
226 int pthread_rwlock_timedrdlock(pthread_rwlock_t *__restrict, const struct timespec *__restrict);
227 /**
228   * @brief Apply a read lock to the read-write lock referenced by rwlock as in the
229   *        pthread_rwlock_rdlock() function. However, if the lock cannot be acquired without
230   *        waiting for other threads to unlock the lock, this wait shall be terminated when
231   *        the specified timeout expires. The timeout shall expire when the absolute time specified by
232   *        abstime passes, as measured by the clock on which timeouts are based, or if the absolute time
233   *        specified by abstime has already been passed at the time of the call.
234   *        The timeout shall be based on the CLOCK_REALTIME or CLOCK_MONOTONIC clock.
235   * @param rw a read lock to the read-write lock referenced.
236   * @param clock_id specified CLOCK_REALTIME or CLOCK_MONOTONIC clock.
237   * @param timespec the timeout shall expire specified by abstime passes.
238   * @return clockrdlock result.
239   * @retval 0 is returned on success.
240   * @retval -1 is returned on failure, and errno is set to indicate the error.
241   */
242 int pthread_rwlock_clockrdlock(pthread_rwlock_t *__restrict, clockid_t, const struct timespec *__restrict);
243 /**
244   * @brief Apply a read lock to the read-write lock referenced by rwlock as in the
245   *        pthread_rwlock_rdlock() function. However, if the lock cannot be acquired without
246   *        waiting for other threads to unlock the lock, this wait shall be terminated when
247   *        the specified timeout expires. The timeout shall expire when the absolute time specified by
248   *        abstime passes, as measured by the clock on which timeouts are based, or if the absolute time
249   *        specified by abstime has already been passed at the time of the call.
250   *        The timeout shall be based on the CLOCK_MONOTONIC clock.
251   * @param rw a read lock to the read-write lock referenced.
252   * @param timespec the timeout shall expire specified by abstime passes.
253   * @return clockrdlock result.
254   * @retval 0 is returned on success.
255   * @retval -1 is returned on failure, and errno is set to indicate the error.
256   */
257 int pthread_rwlock_timedrdlock_monotonic_np(pthread_rwlock_t *__restrict, const struct timespec *__restrict);
258 int pthread_rwlock_wrlock(pthread_rwlock_t *);
259 int pthread_rwlock_trywrlock(pthread_rwlock_t *);
260 int pthread_rwlock_timedwrlock(pthread_rwlock_t *__restrict, const struct timespec *__restrict);
261 int pthread_rwlock_unlock(pthread_rwlock_t *);
262 /**
263   * @brief Read-write lock variables have an initialization option to use CLOCK_MONOTONIC.
264   *        apply a read lock to the read-write lock referenced by rwlock as in the
265   *        pthread_rwlock_wrlock() function. However, if the lock cannot be acquired without
266   *        waiting for other threads to unlock the lock, this wait shall be terminated when
267   *        the specified timeout expires. The timeout shall expire when the absolute time specified by
268   *        abstime passes, as measured by the clock on which timeouts are based, or if the absolute time
269   *        specified by abstime has already been passed at the time of the call.
270   *        The timeout shall be based on the CLOCK_MONOTONIC clock.
271   * @param rw a read lock to the read-write lock referenced.
272   * @param timespec the timeout shall expire specified by abstime passes.
273   * @return clockrdlock result.
274   * @retval 0 is returned on success.
275   * @retval -1 is returned on failure, and errno is set to indicate the error.
276   */
277 int pthread_rwlock_timedwrlock_monotonic_np(pthread_rwlock_t *__restrict, const struct timespec *__restrict);
278 
279 /**
280   * @brief Apply a read lock to the read-write lock referenced by rwlock as in the
281   *        pthread_rwlock_wrlock() function. However, if the lock cannot be acquired without
282   *        waiting for other threads to unlock the lock, this wait shall be terminated when
283   *        the specified timeout expires. The timeout shall expire when the absolute time specified by
284   *        abstime passes, as measured by the clock on which timeouts are based, or if the absolute time
285   *        specified by abstime has already been passed at the time of the call.
286   *        The timeout shall be based on the CLOCK_REALTIME or CLOCK_MONOTONIC clock.
287   * @param rw a read lock to the read-write lock referenced.
288   * @param clock_id specified CLOCK_REALTIME or CLOCK_MONOTONIC clock.
289   * @param timespec the timeout shall expire specified by abstime passes.
290   * @return clockrdlock result.
291   * @retval 0 is returned on success.
292   * @retval -1 is returned on failure, and errno is set to indicate the error.
293   */
294 int pthread_rwlock_clockwrlock(pthread_rwlock_t *__restrict, clockid_t, const struct timespec *__restrict);
295 
296 int pthread_spin_init(pthread_spinlock_t *, int);
297 int pthread_spin_destroy(pthread_spinlock_t *);
298 int pthread_spin_lock(pthread_spinlock_t *);
299 int pthread_spin_trylock(pthread_spinlock_t *);
300 int pthread_spin_unlock(pthread_spinlock_t *);
301 
302 int pthread_barrier_init(pthread_barrier_t *__restrict, const pthread_barrierattr_t *__restrict, unsigned);
303 int pthread_barrier_destroy(pthread_barrier_t *);
304 int pthread_barrier_wait(pthread_barrier_t *);
305 
306 int pthread_key_create(pthread_key_t *, void (*)(void *));
307 int pthread_key_delete(pthread_key_t);
308 void *pthread_getspecific(pthread_key_t);
309 int pthread_setspecific(pthread_key_t, const void *);
310 
311 int pthread_attr_init(pthread_attr_t *);
312 int pthread_attr_destroy(pthread_attr_t *);
313 
314 int pthread_attr_getguardsize(const pthread_attr_t *__restrict, size_t *__restrict);
315 int pthread_attr_setguardsize(pthread_attr_t *, size_t);
316 int pthread_attr_getstacksize(const pthread_attr_t *__restrict, size_t *__restrict);
317 int pthread_attr_setstacksize(pthread_attr_t *, size_t);
318 int pthread_attr_getdetachstate(const pthread_attr_t *, int *);
319 int pthread_attr_setdetachstate(pthread_attr_t *, int);
320 int pthread_attr_getstack(const pthread_attr_t *__restrict, void **__restrict, size_t *__restrict);
321 int pthread_attr_setstack(pthread_attr_t *, void *, size_t);
322 int pthread_attr_getscope(const pthread_attr_t *__restrict, int *__restrict);
323 int pthread_attr_setscope(pthread_attr_t *, int);
324 int pthread_attr_getschedpolicy(const pthread_attr_t *__restrict, int *__restrict);
325 int pthread_attr_setschedpolicy(pthread_attr_t *, int);
326 int pthread_attr_getschedparam(const pthread_attr_t *__restrict, struct sched_param *__restrict);
327 int pthread_attr_setschedparam(pthread_attr_t *__restrict, const struct sched_param *__restrict);
328 int pthread_attr_getinheritsched(const pthread_attr_t *__restrict, int *__restrict);
329 int pthread_attr_setinheritsched(pthread_attr_t *, int);
330 
331 int pthread_mutexattr_destroy(pthread_mutexattr_t *);
332 int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t *__restrict, int *__restrict);
333 int pthread_mutexattr_getprotocol(const pthread_mutexattr_t *__restrict, int *__restrict);
334 int pthread_mutexattr_getpshared(const pthread_mutexattr_t *__restrict, int *__restrict);
335 int pthread_mutexattr_getrobust(const pthread_mutexattr_t *__restrict, int *__restrict);
336 int pthread_mutexattr_gettype(const pthread_mutexattr_t *__restrict, int *__restrict);
337 int pthread_mutexattr_init(pthread_mutexattr_t *);
338 int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *, int);
339 int pthread_mutexattr_setprotocol(pthread_mutexattr_t *, int);
340 int pthread_mutexattr_setpshared(pthread_mutexattr_t *, int);
341 int pthread_mutexattr_setrobust(pthread_mutexattr_t *, int);
342 int pthread_mutexattr_settype(pthread_mutexattr_t *, int);
343 
344 int pthread_condattr_init(pthread_condattr_t *);
345 int pthread_condattr_destroy(pthread_condattr_t *);
346 int pthread_condattr_setclock(pthread_condattr_t *, clockid_t);
347 int pthread_condattr_setpshared(pthread_condattr_t *, int);
348 int pthread_condattr_getclock(const pthread_condattr_t *__restrict, clockid_t *__restrict);
349 int pthread_condattr_getpshared(const pthread_condattr_t *__restrict, int *__restrict);
350 
351 int pthread_rwlockattr_init(pthread_rwlockattr_t *);
352 int pthread_rwlockattr_destroy(pthread_rwlockattr_t *);
353 int pthread_rwlockattr_setpshared(pthread_rwlockattr_t *, int);
354 int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t *__restrict, int *__restrict);
355 
356 int pthread_barrierattr_destroy(pthread_barrierattr_t *);
357 int pthread_barrierattr_getpshared(const pthread_barrierattr_t *__restrict, int *__restrict);
358 int pthread_barrierattr_init(pthread_barrierattr_t *);
359 int pthread_barrierattr_setpshared(pthread_barrierattr_t *, int);
360 
361 int pthread_atfork(void (*)(void), void (*)(void), void (*)(void));
362 
363 int pthread_getconcurrency(void);
364 int pthread_setconcurrency(int);
365 
366 int pthread_getcpuclockid(pthread_t, clockid_t *);
367 
368 struct __ptcb {
369 	void (*__f)(void *);
370 	void *__x;
371 	struct __ptcb *__next;
372 };
373 
374 void _pthread_cleanup_push(struct __ptcb *, void (*)(void *), void *);
375 void _pthread_cleanup_pop(struct __ptcb *, int);
376 
377 #define pthread_cleanup_push(f, x) do { struct __ptcb __cb; _pthread_cleanup_push(&__cb, f, x);
378 #define pthread_cleanup_pop(r) _pthread_cleanup_pop(&__cb, (r)); } while(0)
379 
380 #ifdef _GNU_SOURCE
381 struct cpu_set_t;
382 int pthread_getaffinity_np(pthread_t, size_t, struct cpu_set_t *);
383 int pthread_setaffinity_np(pthread_t, size_t, const struct cpu_set_t *);
384 int pthread_getattr_np(pthread_t, pthread_attr_t *);
385 int pthread_setname_np(pthread_t, const char *);
386 int pthread_getname_np(pthread_t, char *, size_t);
387 int pthread_getattr_default_np(pthread_attr_t *);
388 int pthread_setattr_default_np(const pthread_attr_t *);
389 int pthread_tryjoin_np(pthread_t, void **);
390 int pthread_timedjoin_np(pthread_t, void **, const struct timespec *);
391 #endif
392 
393 #if _REDIR_TIME64
394 __REDIR(pthread_mutex_timedlock, __pthread_mutex_timedlock_time64);
395 __REDIR(pthread_cond_timedwait, __pthread_cond_timedwait_time64);
396 __REDIR(pthread_rwlock_timedrdlock, __pthread_rwlock_timedrdlock_time64);
397 __REDIR(pthread_rwlock_timedwrlock, __pthread_rwlock_timedwrlock_time64);
398 #ifdef _GNU_SOURCE
399 __REDIR(pthread_timedjoin_np, __pthread_timedjoin_np_time64);
400 #endif
401 #endif
402 
403 #ifdef __cplusplus
404 }
405 #endif
406 #endif
407