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