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