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