pthread.h (28093B) - Raw
1 /* 2 * Copyright (c) 2000-2012 Apple Inc. All rights reserved. 3 * 4 * @APPLE_LICENSE_HEADER_START@ 5 * 6 * This file contains Original Code and/or Modifications of Original Code 7 * as defined in and that are subject to the Apple Public Source License 8 * Version 2.0 (the 'License'). You may not use this file except in 9 * compliance with the License. Please obtain a copy of the License at 10 * http://www.opensource.apple.com/apsl/ and read it before using this 11 * file. 12 * 13 * The Original Code and all software distributed under the License are 14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 18 * Please see the License for the specific language governing rights and 19 * limitations under the License. 20 * 21 * @APPLE_LICENSE_HEADER_END@ 22 */ 23 /* 24 * Copyright 1996 1995 by Open Software Foundation, Inc. 1997 1996 1995 1994 1993 1992 1991 25 * All Rights Reserved 26 * 27 * Permission to use, copy, modify, and distribute this software and 28 * its documentation for any purpose and without fee is hereby granted, 29 * provided that the above copyright notice appears in all copies and 30 * that both the copyright notice and this permission notice appear in 31 * supporting documentation. 32 * 33 * OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE 34 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 35 * FOR A PARTICULAR PURPOSE. 36 * 37 * IN NO EVENT SHALL OSF BE LIABLE FOR ANY SPECIAL, INDIRECT, OR 38 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 39 * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT, 40 * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION 41 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 42 * 43 */ 44 /* 45 * MkLinux 46 */ 47 48 /* 49 * POSIX Threads - IEEE 1003.1c 50 */ 51 52 #ifndef _PTHREAD_H 53 #define _PTHREAD_H 54 55 #include <_types.h> 56 #include <pthread/sched.h> 57 #include <time.h> 58 #include <sys/_pthread/_pthread_types.h> 59 #include <sys/_pthread/_pthread_attr_t.h> 60 #include <sys/_pthread/_pthread_cond_t.h> 61 #include <sys/_pthread/_pthread_condattr_t.h> 62 #include <sys/_pthread/_pthread_key_t.h> 63 #include <sys/_pthread/_pthread_mutex_t.h> 64 #include <sys/_pthread/_pthread_mutexattr_t.h> 65 #include <sys/_pthread/_pthread_once_t.h> 66 #include <sys/_pthread/_pthread_rwlock_t.h> 67 #include <sys/_pthread/_pthread_rwlockattr_t.h> 68 #include <sys/_pthread/_pthread_t.h> 69 70 #include <pthread/qos.h> 71 72 #if (!defined(_POSIX_C_SOURCE) && !defined(_XOPEN_SOURCE)) || defined(_DARWIN_C_SOURCE) || defined(__cplusplus) 73 74 #include <sys/_types/_mach_port_t.h> 75 #include <sys/_types/_sigset_t.h> 76 77 #endif /* (!_POSIX_C_SOURCE && !_XOPEN_SOURCE) || _DARWIN_C_SOURCE || __cplusplus */ 78 79 /* 80 * These symbols indicate which [optional] features are available 81 * They can be tested at compile time via '#ifdef XXX' 82 * The way to check for pthreads is like so: 83 84 * #include <unistd.h> 85 * #ifdef _POSIX_THREADS 86 * #include <pthread.h> 87 * #endif 88 89 */ 90 91 /* These will be moved to unistd.h */ 92 93 /* 94 * Note: These data structures are meant to be opaque. Only enough 95 * structure is exposed to support initializers. 96 * All of the typedefs will be moved to <sys/types.h> 97 */ 98 99 #include <sys/cdefs.h> 100 #include <Availability.h> 101 102 #if __has_feature(assume_nonnull) 103 _Pragma("clang assume_nonnull begin") 104 #endif 105 __BEGIN_DECLS 106 /* 107 * Threads 108 */ 109 110 111 /* 112 * Cancel cleanup handler management. Note, since these are implemented as macros, 113 * they *MUST* occur in matched pairs! 114 */ 115 116 #define pthread_cleanup_push(func, val) \ 117 { \ 118 struct __darwin_pthread_handler_rec __handler; \ 119 pthread_t __self = pthread_self(); \ 120 __handler.__routine = func; \ 121 __handler.__arg = val; \ 122 __handler.__next = __self->__cleanup_stack; \ 123 __self->__cleanup_stack = &__handler; 124 125 #define pthread_cleanup_pop(execute) \ 126 /* Note: 'handler' must be in this same lexical context! */ \ 127 __self->__cleanup_stack = __handler.__next; \ 128 if (execute) (__handler.__routine)(__handler.__arg); \ 129 } 130 131 /* 132 * Thread attributes 133 */ 134 135 #define PTHREAD_CREATE_JOINABLE 1 136 #define PTHREAD_CREATE_DETACHED 2 137 138 #define PTHREAD_INHERIT_SCHED 1 139 #define PTHREAD_EXPLICIT_SCHED 2 140 141 #define PTHREAD_CANCEL_ENABLE 0x01 /* Cancel takes place at next cancellation point */ 142 #define PTHREAD_CANCEL_DISABLE 0x00 /* Cancel postponed */ 143 #define PTHREAD_CANCEL_DEFERRED 0x02 /* Cancel waits until cancellation point */ 144 #define PTHREAD_CANCEL_ASYNCHRONOUS 0x00 /* Cancel occurs immediately */ 145 146 /* Value returned from pthread_join() when a thread is canceled */ 147 #define PTHREAD_CANCELED ((void *) 1) 148 149 /* We only support PTHREAD_SCOPE_SYSTEM */ 150 #define PTHREAD_SCOPE_SYSTEM 1 151 #define PTHREAD_SCOPE_PROCESS 2 152 153 #define PTHREAD_PROCESS_SHARED 1 154 #define PTHREAD_PROCESS_PRIVATE 2 155 156 /* 157 * Mutex protocol attributes 158 */ 159 #define PTHREAD_PRIO_NONE 0 160 #define PTHREAD_PRIO_INHERIT 1 161 #define PTHREAD_PRIO_PROTECT 2 162 163 /* 164 * Mutex type attributes 165 */ 166 #define PTHREAD_MUTEX_NORMAL 0 167 #define PTHREAD_MUTEX_ERRORCHECK 1 168 #define PTHREAD_MUTEX_RECURSIVE 2 169 #define PTHREAD_MUTEX_DEFAULT PTHREAD_MUTEX_NORMAL 170 171 /* 172 * Mutex policy attributes 173 */ 174 #define PTHREAD_MUTEX_POLICY_FAIRSHARE_NP 1 175 #define PTHREAD_MUTEX_POLICY_FIRSTFIT_NP 3 176 177 /* 178 * RWLock variables 179 */ 180 #define PTHREAD_RWLOCK_INITIALIZER {_PTHREAD_RWLOCK_SIG_init, {0}} 181 182 /* 183 * Mutex variables 184 */ 185 #define PTHREAD_MUTEX_INITIALIZER {_PTHREAD_MUTEX_SIG_init, {0}} 186 187 /* <rdar://problem/10854763> */ 188 #if ((__MAC_OS_X_VERSION_MIN_REQUIRED && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070) || (__IPHONE_OS_VERSION_MIN_REQUIRED && __IPHONE_OS_VERSION_MIN_REQUIRED >= 50000)) || defined(__DRIVERKIT_VERSION_MIN_REQUIRED) 189 # if (!defined(_POSIX_C_SOURCE) && !defined(_XOPEN_SOURCE)) || defined(_DARWIN_C_SOURCE) 190 # define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER {_PTHREAD_ERRORCHECK_MUTEX_SIG_init, {0}} 191 # define PTHREAD_RECURSIVE_MUTEX_INITIALIZER {_PTHREAD_RECURSIVE_MUTEX_SIG_init, {0}} 192 # endif /* (!_POSIX_C_SOURCE && !_XOPEN_SOURCE) || _DARWIN_C_SOURCE */ 193 #endif 194 195 /* <rdar://problem/25944576> */ 196 #define _PTHREAD_SWIFT_IMPORTER_NULLABILITY_COMPAT() \ 197 defined(SWIFT_CLASS_EXTRA) && (!defined(SWIFT_SDK_OVERLAY_PTHREAD_EPOCH) || (SWIFT_SDK_OVERLAY_PTHREAD_EPOCH < 1)) 198 199 #if __has_attribute(__swift_attr__) 200 #define __PTHREAD_SWIFT_UNAVAILABLE_FROM_ASYNC(msg) \ 201 __attribute__((__swift_attr__("@_unavailableFromAsync(message: \"" msg "\")"))) 202 #else 203 #define __PTHREAD_SWIFT_UNAVAILABLE_FROM_ASYNC(msg) 204 #endif 205 206 /* 207 * Condition variable attributes 208 */ 209 210 /* 211 * Condition variables 212 */ 213 214 #define PTHREAD_COND_INITIALIZER {_PTHREAD_COND_SIG_init, {0}} 215 216 /* 217 * Initialization control (once) variables 218 */ 219 220 #define PTHREAD_ONCE_INIT {_PTHREAD_ONCE_SIG_init, {0}} 221 222 /* 223 * Prototypes for all PTHREAD interfaces 224 */ 225 __API_AVAILABLE(macos(10.4), ios(2.0)) 226 int pthread_atfork(void (* _Nullable)(void), void (* _Nullable)(void), 227 void (* _Nullable)(void)); 228 229 __API_AVAILABLE(macos(10.4), ios(2.0)) 230 int pthread_attr_destroy(pthread_attr_t *); 231 232 __API_AVAILABLE(macos(10.4), ios(2.0)) 233 int pthread_attr_getdetachstate(const pthread_attr_t *, int *); 234 235 __API_AVAILABLE(macos(10.4), ios(2.0)) 236 int pthread_attr_getguardsize(const pthread_attr_t * __restrict, size_t * __restrict); 237 238 __API_AVAILABLE(macos(10.4), ios(2.0)) 239 int pthread_attr_getinheritsched(const pthread_attr_t * __restrict, int * __restrict); 240 241 __API_AVAILABLE(macos(10.4), ios(2.0)) 242 int pthread_attr_getschedparam(const pthread_attr_t * __restrict, 243 struct sched_param * __restrict); 244 245 __API_AVAILABLE(macos(10.4), ios(2.0)) 246 int pthread_attr_getschedpolicy(const pthread_attr_t * __restrict, int * __restrict); 247 248 __API_AVAILABLE(macos(10.4), ios(2.0)) 249 int pthread_attr_getscope(const pthread_attr_t * __restrict, int * __restrict); 250 251 __API_AVAILABLE(macos(10.4), ios(2.0)) 252 int pthread_attr_getstack(const pthread_attr_t * __restrict, 253 void * _Nullable * _Nonnull __restrict, size_t * __restrict); 254 255 __API_AVAILABLE(macos(10.4), ios(2.0)) 256 int pthread_attr_getstackaddr(const pthread_attr_t * __restrict, 257 void * _Nullable * _Nonnull __restrict); 258 259 __API_AVAILABLE(macos(10.4), ios(2.0)) 260 int pthread_attr_getstacksize(const pthread_attr_t * __restrict, size_t * __restrict); 261 262 __API_AVAILABLE(macos(10.4), ios(2.0)) 263 int pthread_attr_init(pthread_attr_t *); 264 265 __API_AVAILABLE(macos(10.4), ios(2.0)) 266 int pthread_attr_setdetachstate(pthread_attr_t *, int); 267 268 __API_AVAILABLE(macos(10.4), ios(2.0)) 269 int pthread_attr_setguardsize(pthread_attr_t *, size_t); 270 271 __API_AVAILABLE(macos(10.4), ios(2.0)) 272 int pthread_attr_setinheritsched(pthread_attr_t *, int); 273 274 __API_AVAILABLE(macos(10.4), ios(2.0)) 275 int pthread_attr_setschedparam(pthread_attr_t * __restrict, 276 const struct sched_param * __restrict); 277 278 __API_AVAILABLE(macos(10.4), ios(2.0)) 279 int pthread_attr_setschedpolicy(pthread_attr_t *, int); 280 281 __API_AVAILABLE(macos(10.4), ios(2.0)) 282 int pthread_attr_setscope(pthread_attr_t *, int); 283 284 __API_AVAILABLE(macos(10.4), ios(2.0)) 285 int pthread_attr_setstack(pthread_attr_t *, void *, size_t); 286 287 __API_AVAILABLE(macos(10.4), ios(2.0)) 288 int pthread_attr_setstackaddr(pthread_attr_t *, void *); 289 290 __API_AVAILABLE(macos(10.4), ios(2.0)) 291 int pthread_attr_setstacksize(pthread_attr_t *, size_t); 292 293 __API_AVAILABLE(macos(10.4), ios(2.0)) 294 int pthread_cancel(pthread_t) __DARWIN_ALIAS(pthread_cancel); 295 296 __API_AVAILABLE(macos(10.4), ios(2.0)) 297 int pthread_cond_broadcast(pthread_cond_t *); 298 299 __API_AVAILABLE(macos(10.4), ios(2.0)) 300 int pthread_cond_destroy(pthread_cond_t *); 301 302 __API_AVAILABLE(macos(10.4), ios(2.0)) 303 int pthread_cond_init( 304 pthread_cond_t * __restrict, 305 const pthread_condattr_t * _Nullable __restrict) 306 __DARWIN_ALIAS(pthread_cond_init); 307 308 __API_AVAILABLE(macos(10.4), ios(2.0)) 309 int pthread_cond_signal(pthread_cond_t *); 310 311 __API_AVAILABLE(macos(10.4), ios(2.0)) 312 __PTHREAD_SWIFT_UNAVAILABLE_FROM_ASYNC("Use an asynchronous wait instead of a synchronous wait") 313 int pthread_cond_timedwait( 314 pthread_cond_t * __restrict, pthread_mutex_t * __restrict, 315 const struct timespec * _Nullable __restrict) 316 __DARWIN_ALIAS_C(pthread_cond_timedwait); 317 318 __API_AVAILABLE(macos(10.4), ios(2.0)) 319 __PTHREAD_SWIFT_UNAVAILABLE_FROM_ASYNC("Use an asynchronous wait instead of a synchronous wait") 320 int pthread_cond_wait(pthread_cond_t * __restrict, 321 pthread_mutex_t * __restrict) __DARWIN_ALIAS_C(pthread_cond_wait); 322 323 __API_AVAILABLE(macos(10.4), ios(2.0)) 324 int pthread_condattr_destroy(pthread_condattr_t *); 325 326 __API_AVAILABLE(macos(10.4), ios(2.0)) 327 int pthread_condattr_init(pthread_condattr_t *); 328 329 __API_AVAILABLE(macos(10.4), ios(2.0)) 330 int pthread_condattr_getpshared(const pthread_condattr_t * __restrict, 331 int * __restrict); 332 333 __API_AVAILABLE(macos(10.4), ios(2.0)) 334 int pthread_condattr_setpshared(pthread_condattr_t *, int); 335 336 __API_AVAILABLE(macos(10.4), ios(2.0)) 337 #if !_PTHREAD_SWIFT_IMPORTER_NULLABILITY_COMPAT() 338 int pthread_create(pthread_t _Nullable * _Nonnull __restrict, 339 const pthread_attr_t * _Nullable __restrict, 340 void * _Nullable (* _Nonnull)(void * _Nullable), 341 void * _Nullable __restrict); 342 #else 343 int pthread_create(pthread_t * __restrict, 344 const pthread_attr_t * _Nullable __restrict, 345 void *(* _Nonnull)(void *), void * _Nullable __restrict); 346 #endif // _PTHREAD_SWIFT_IMPORTER_NULLABILITY_COMPAT() 347 348 __API_AVAILABLE(macos(10.4), ios(2.0)) 349 int pthread_detach(pthread_t); 350 351 __API_AVAILABLE(macos(10.4), ios(2.0)) 352 int pthread_equal(pthread_t _Nullable, pthread_t _Nullable); 353 354 __API_AVAILABLE(macos(10.4), ios(2.0)) 355 __PTHREAD_SWIFT_UNAVAILABLE_FROM_ASYNC("Thread lifecycle is owned by Swift Concurrency runtime") 356 void pthread_exit(void * _Nullable) __dead2; 357 358 __API_AVAILABLE(macos(10.4), ios(2.0)) 359 int pthread_getconcurrency(void); 360 361 __API_AVAILABLE(macos(10.4), ios(2.0)) 362 int pthread_getschedparam(pthread_t , int * _Nullable __restrict, 363 struct sched_param * _Nullable __restrict); 364 365 __PTHREAD_SWIFT_UNAVAILABLE_FROM_ASYNC("Use Task Local Values instead") 366 __API_AVAILABLE(macos(10.4), ios(2.0)) 367 void* _Nullable pthread_getspecific(pthread_key_t); 368 369 __API_AVAILABLE(macos(10.4), ios(2.0)) 370 __PTHREAD_SWIFT_UNAVAILABLE_FROM_ASYNC("Use an asynchronous wait instead of a synchronous wait") 371 int pthread_join(pthread_t , void * _Nullable * _Nullable) 372 __DARWIN_ALIAS_C(pthread_join); 373 374 __API_AVAILABLE(macos(10.4), ios(2.0)) 375 int pthread_key_create(pthread_key_t *, void (* _Nullable)(void *)); 376 377 __API_AVAILABLE(macos(10.4), ios(2.0)) 378 int pthread_key_delete(pthread_key_t); 379 380 __API_AVAILABLE(macos(10.4), ios(2.0)) 381 int pthread_mutex_destroy(pthread_mutex_t *); 382 383 __API_AVAILABLE(macos(10.4), ios(2.0)) 384 int pthread_mutex_getprioceiling(const pthread_mutex_t * __restrict, 385 int * __restrict); 386 387 __API_AVAILABLE(macos(10.4), ios(2.0)) 388 int pthread_mutex_init(pthread_mutex_t * __restrict, 389 const pthread_mutexattr_t * _Nullable __restrict); 390 391 __API_AVAILABLE(macos(10.4), ios(2.0)) 392 __PTHREAD_SWIFT_UNAVAILABLE_FROM_ASYNC("Use OSAllocatedUnfairLock's withLock or NSLock for async-safe scoped locking") 393 int pthread_mutex_lock(pthread_mutex_t *); 394 395 __API_AVAILABLE(macos(10.4), ios(2.0)) 396 int pthread_mutex_setprioceiling(pthread_mutex_t * __restrict, int, 397 int * __restrict); 398 399 __API_AVAILABLE(macos(10.4), ios(2.0)) 400 __PTHREAD_SWIFT_UNAVAILABLE_FROM_ASYNC("Use OSAllocatedUnfairLock's withLockIfAvailable or NSLock for async-safe scoped locking") 401 int pthread_mutex_trylock(pthread_mutex_t *); 402 403 __API_AVAILABLE(macos(10.4), ios(2.0)) 404 __PTHREAD_SWIFT_UNAVAILABLE_FROM_ASYNC("Use OSAllocatedUnfairLock's withLock or NSLock for async-safe scoped locking") 405 int pthread_mutex_unlock(pthread_mutex_t *); 406 407 __API_AVAILABLE(macos(10.4), ios(2.0)) 408 int pthread_mutexattr_destroy(pthread_mutexattr_t *) __DARWIN_ALIAS(pthread_mutexattr_destroy); 409 410 __API_AVAILABLE(macos(10.4), ios(2.0)) 411 int pthread_mutexattr_getprioceiling(const pthread_mutexattr_t * __restrict, 412 int * __restrict); 413 414 __API_AVAILABLE(macos(10.4), ios(2.0)) 415 int pthread_mutexattr_getprotocol(const pthread_mutexattr_t * __restrict, 416 int * __restrict); 417 418 __API_AVAILABLE(macos(10.4), ios(2.0)) 419 int pthread_mutexattr_getpshared(const pthread_mutexattr_t * __restrict, 420 int * __restrict); 421 422 __API_AVAILABLE(macos(10.4), ios(2.0)) 423 int pthread_mutexattr_gettype(const pthread_mutexattr_t * __restrict, 424 int * __restrict); 425 426 __API_AVAILABLE(macos(10.13.4), ios(11.3), watchos(4.3), tvos(11.3)) 427 int pthread_mutexattr_getpolicy_np(const pthread_mutexattr_t * __restrict, 428 int * __restrict); 429 430 __API_AVAILABLE(macos(10.4), ios(2.0)) 431 int pthread_mutexattr_init(pthread_mutexattr_t *); 432 433 __API_AVAILABLE(macos(10.4), ios(2.0)) 434 int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *, int); 435 436 __API_AVAILABLE(macos(10.4), ios(2.0)) 437 int pthread_mutexattr_setprotocol(pthread_mutexattr_t *, int); 438 439 __API_AVAILABLE(macos(10.4), ios(2.0)) 440 int pthread_mutexattr_setpshared(pthread_mutexattr_t *, int); 441 442 __API_AVAILABLE(macos(10.4), ios(2.0)) 443 int pthread_mutexattr_settype(pthread_mutexattr_t *, int); 444 445 __API_AVAILABLE(macos(10.7), ios(5.0)) 446 int pthread_mutexattr_setpolicy_np(pthread_mutexattr_t *, int); 447 448 __SWIFT_UNAVAILABLE_MSG("Use lazily initialized globals instead") 449 __API_AVAILABLE(macos(10.4), ios(2.0)) 450 int pthread_once(pthread_once_t *, void (* _Nonnull)(void)); 451 452 __API_AVAILABLE(macos(10.4), ios(2.0)) 453 int pthread_rwlock_destroy(pthread_rwlock_t * ) __DARWIN_ALIAS(pthread_rwlock_destroy); 454 455 __API_AVAILABLE(macos(10.4), ios(2.0)) 456 int pthread_rwlock_init(pthread_rwlock_t * __restrict, 457 const pthread_rwlockattr_t * _Nullable __restrict) 458 __DARWIN_ALIAS(pthread_rwlock_init); 459 460 __API_AVAILABLE(macos(10.4), ios(2.0)) 461 __PTHREAD_SWIFT_UNAVAILABLE_FROM_ASYNC("Use async-safe scoped locking instead") 462 int pthread_rwlock_rdlock(pthread_rwlock_t *) __DARWIN_ALIAS(pthread_rwlock_rdlock); 463 464 __API_AVAILABLE(macos(10.4), ios(2.0)) 465 __PTHREAD_SWIFT_UNAVAILABLE_FROM_ASYNC("Use async-safe scoped locking instead") 466 int pthread_rwlock_tryrdlock(pthread_rwlock_t *) __DARWIN_ALIAS(pthread_rwlock_tryrdlock); 467 468 __API_AVAILABLE(macos(10.4), ios(2.0)) 469 __PTHREAD_SWIFT_UNAVAILABLE_FROM_ASYNC("Use async-safe scoped locking instead") 470 int pthread_rwlock_trywrlock(pthread_rwlock_t *) __DARWIN_ALIAS(pthread_rwlock_trywrlock); 471 472 __API_AVAILABLE(macos(10.4), ios(2.0)) 473 __PTHREAD_SWIFT_UNAVAILABLE_FROM_ASYNC("Use async-safe scoped locking instead") 474 int pthread_rwlock_wrlock(pthread_rwlock_t *) __DARWIN_ALIAS(pthread_rwlock_wrlock); 475 476 __API_AVAILABLE(macos(10.4), ios(2.0)) 477 __PTHREAD_SWIFT_UNAVAILABLE_FROM_ASYNC("Use async-safe scoped locking instead") 478 int pthread_rwlock_unlock(pthread_rwlock_t *) __DARWIN_ALIAS(pthread_rwlock_unlock); 479 480 __API_AVAILABLE(macos(10.4), ios(2.0)) 481 int pthread_rwlockattr_destroy(pthread_rwlockattr_t *); 482 483 __API_AVAILABLE(macos(10.4), ios(2.0)) 484 int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t * __restrict, 485 int * __restrict); 486 487 __API_AVAILABLE(macos(10.4), ios(2.0)) 488 int pthread_rwlockattr_init(pthread_rwlockattr_t *); 489 490 __API_AVAILABLE(macos(10.4), ios(2.0)) 491 int pthread_rwlockattr_setpshared(pthread_rwlockattr_t *, int); 492 493 __API_AVAILABLE(macos(10.4), ios(2.0)) 494 pthread_t pthread_self(void); 495 496 __API_AVAILABLE(macos(10.4), ios(2.0)) 497 __PTHREAD_SWIFT_UNAVAILABLE_FROM_ASYNC("Use Task cancellation instead") 498 int pthread_setcancelstate(int , int * _Nullable) 499 __DARWIN_ALIAS(pthread_setcancelstate); 500 501 __API_AVAILABLE(macos(10.4), ios(2.0)) 502 __PTHREAD_SWIFT_UNAVAILABLE_FROM_ASYNC("Use Task cancellation instead") 503 int pthread_setcanceltype(int , int * _Nullable) 504 __DARWIN_ALIAS(pthread_setcanceltype); 505 506 __API_AVAILABLE(macos(10.4), ios(2.0)) 507 int pthread_setconcurrency(int); 508 509 __API_AVAILABLE(macos(10.4), ios(2.0)) 510 int pthread_setschedparam(pthread_t, int, const struct sched_param *); 511 512 __API_AVAILABLE(macos(10.4), ios(2.0)) 513 __PTHREAD_SWIFT_UNAVAILABLE_FROM_ASYNC("Use Task Local Values instead") 514 int pthread_setspecific(pthread_key_t , const void * _Nullable); 515 516 __API_AVAILABLE(macos(10.4), ios(2.0)) 517 __PTHREAD_SWIFT_UNAVAILABLE_FROM_ASYNC("Use Task cancellation instead") 518 void pthread_testcancel(void) __DARWIN_ALIAS(pthread_testcancel); 519 520 #if (!defined(_POSIX_C_SOURCE) && !defined(_XOPEN_SOURCE)) || defined(_DARWIN_C_SOURCE) || defined(__cplusplus) 521 522 /* returns non-zero if pthread_create or cthread_fork have been called */ 523 __API_AVAILABLE(macos(10.4), ios(2.0)) 524 int pthread_is_threaded_np(void); 525 526 __API_AVAILABLE(macos(10.6), ios(3.2)) 527 int pthread_threadid_np(pthread_t _Nullable,__uint64_t* _Nullable); 528 529 /*SPI to set and get pthread name*/ 530 __API_AVAILABLE(macos(10.6), ios(3.2)) 531 int pthread_getname_np(pthread_t,char*,size_t); 532 533 __API_AVAILABLE(macos(10.6), ios(3.2)) 534 __PTHREAD_SWIFT_UNAVAILABLE_FROM_ASYNC("Thread lifecycle is owned by Swift Concurrency runtime") 535 int pthread_setname_np(const char*); 536 537 /* returns non-zero if the current thread is the main thread */ 538 __API_AVAILABLE(macos(10.4), ios(2.0)) 539 int pthread_main_np(void); 540 541 /* return the mach thread bound to the pthread */ 542 __API_AVAILABLE(macos(10.4), ios(2.0)) 543 mach_port_t pthread_mach_thread_np(pthread_t); 544 545 __API_AVAILABLE(macos(10.4), ios(2.0)) 546 size_t pthread_get_stacksize_np(pthread_t); 547 548 __API_AVAILABLE(macos(10.4), ios(2.0)) 549 void* pthread_get_stackaddr_np(pthread_t); 550 551 /* Like pthread_cond_signal(), but only wake up the specified pthread */ 552 __API_AVAILABLE(macos(10.4), ios(2.0)) 553 int pthread_cond_signal_thread_np(pthread_cond_t *, pthread_t _Nullable); 554 555 /* Like pthread_cond_timedwait, but use a relative timeout */ 556 __API_AVAILABLE(macos(10.4), ios(2.0)) 557 __PTHREAD_SWIFT_UNAVAILABLE_FROM_ASYNC("Use an asynchronous wait instead of a synchronous wait") 558 int pthread_cond_timedwait_relative_np(pthread_cond_t *, pthread_mutex_t *, 559 const struct timespec * _Nullable); 560 561 /* Like pthread_create(), but leaves the thread suspended */ 562 __API_AVAILABLE(macos(10.4), ios(2.0)) 563 #if !_PTHREAD_SWIFT_IMPORTER_NULLABILITY_COMPAT() 564 int pthread_create_suspended_np( 565 pthread_t _Nullable * _Nonnull, const pthread_attr_t * _Nullable, 566 void * _Nullable (* _Nonnull)(void * _Nullable), void * _Nullable); 567 #else 568 int pthread_create_suspended_np(pthread_t *, const pthread_attr_t * _Nullable, 569 void *(* _Nonnull)(void *), void * _Nullable); 570 #endif 571 572 __API_AVAILABLE(macos(10.4), ios(2.0)) 573 int pthread_kill(pthread_t, int); 574 575 __API_AVAILABLE(macos(10.5), ios(2.0)) 576 _Nullable pthread_t pthread_from_mach_thread_np(mach_port_t); 577 578 __API_AVAILABLE(macos(10.4), ios(2.0)) 579 int pthread_sigmask(int, const sigset_t * _Nullable, sigset_t * _Nullable) 580 __DARWIN_ALIAS(pthread_sigmask); 581 582 __API_AVAILABLE(macos(10.4), ios(2.0)) 583 __PTHREAD_SWIFT_UNAVAILABLE_FROM_ASYNC("Use Task.yield(), or await a condition instead of spinning") 584 void pthread_yield_np(void); 585 586 __API_AVAILABLE(macos(11.0)) 587 __API_UNAVAILABLE(ios, tvos, watchos, driverkit) 588 void pthread_jit_write_protect_np(int enabled); 589 590 __API_AVAILABLE(macos(11.0), ios(17.4)) 591 __API_UNAVAILABLE(tvos, watchos, driverkit, visionos) 592 int pthread_jit_write_protect_supported_np(void); 593 594 /*! 595 * @typedef pthread_jit_write_callback_t 596 * The type of a function that can be supplied to {@link 597 * pthread_jit_write_with_callback_np} to write to the MAP_JIT region while it 598 * is writeable. 599 * 600 * @param ctx 601 * A pointer to context that will be passed through to the callback function. 602 * 603 * @result 604 * A result code to be returned to the caller of @{link 605 * pthread_jit_write_with_callback_np}. The system does not interpret/act on 606 * the value of this result. 607 */ 608 typedef int (*pthread_jit_write_callback_t)(void * _Nullable ctx); 609 610 /*! 611 * @define PTHREAD_JIT_WRITE_ALLOW_CALLBACKS_NP 612 * A macro to be used at file scope to list the functions allowed to be passed 613 * to {@link pthread_jit_write_with_callback_np} to write to the MAP_JIT region 614 * while it is writeable. It may be invoked only once per executable/library. 615 * 616 * @param callbacks 617 * The pthread_jit_write_callback_t functions to allow. They should be supplied 618 * as a comma-delimited list. 619 */ 620 #if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__OBJC__) || defined(__cplusplus) 621 #define PTHREAD_JIT_WRITE_ALLOW_CALLBACKS_NP(...) \ 622 __attribute__((__used__, __section__("__DATA_CONST,__pth_jit_func"))) \ 623 static const pthread_jit_write_callback_t __pthread_jit_write_callback_allowlist[] = { \ 624 __VA_ARGS__, NULL \ 625 } 626 #endif /* (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__OBJC__) || defined(__cplusplus) */ 627 628 /*! 629 * @function pthread_jit_write_with_callback_np 630 * 631 * @abstract 632 * Toggles per-thread write-protection of the MAP_JIT region to writeable, 633 * invokes an allowed callback function to write to it, and toggles protection 634 * back to executable. 635 * 636 * @param callback 637 * The callback function to invoke to write to the MAP_JIT region. It must be 638 * statically allowed using {@link PTHREAD_JIT_WRITE_ALLOW_CALLBACKS_NP}. 639 * 640 * @param ctx 641 * Context to pass through to the invocation of the callback function. 642 * 643 * @result 644 * The result code returned by the callback function. 645 * 646 * @discussion 647 * This function assumes that the MAP_JIT region has executable protection when 648 * called. It is therefore invalid to call it recursively from within a write 649 * callback. The implementation does not detect such invalid recursive calls, 650 * so the client is responsible for preventing them. 651 * 652 * Callbacks _must not_ perform any non-local transfer of control flow (e.g. 653 * throw an exception, longjmp(3)), as doing so would leave the MAP_JIT region 654 * writeable. 655 * 656 * On systems where pthread_jit_write_protect_supported_np(3) is false, this 657 * function calls @callback directly and does nothing else. 658 * 659 * This function only enforces that @callback is allowed if the caller has the 660 * com.apple.security.cs.jit-write-allowlist entitlement. That entitlement also 661 * disallows use of pthread_jit_write_protect_np(3). Adopting the entitlement 662 * is therefore crucial in realizing the security benefits of this interface. 663 * 664 * If the entitlement is not present then this function toggles protection of 665 * the MAP_JIT to writeable, calls @callback and then toggles protection back to 666 * executable, without validating that @callback is an allowed function. This 667 * behavior is intended to permit independent adoption of this interface by 668 * libraries - once all libraries in an application have adopted, the 669 * application should add the entitlement. 670 * 671 * By default, only callbacks in libraries/images present at process start-up 672 * are allowed - callbacks in images loaded dynamically via dlopen(3)/etc. are 673 * not permitted. However, if the additional entitlement 674 * com.apple.security.cs.jit-write-allowlist-freeze-late is _also_ present, any 675 * callbacks in dlopen'd libraries are also added to the set of allowed 676 * callbacks until the {@link pthread_jit_write_freeze_callbacks_np} function is 677 * called. 678 * 679 * The goal of this interface is to allow applications that execute JIT-compiled 680 * code to mitigate against attempts from attackers to escalate to code 681 * execution by getting their own instructions written to the MAP_JIT region. 682 * 683 * Callbacks should assume an attacker can control the input to this function. 684 * They must therefore carefully validate the data that they are passed and do 685 * so using as little attackable state as possible. This means simplifying 686 * control flow and avoiding spills of sensitive registers (e.g. those used for 687 * validation or control flow). 688 * 689 * In the event a callback detects that its input is invalid, it should either 690 * abort in the simplest fashion possible (preferring e.g. __builtin_trap() over 691 * abort(3), the latter being encumbered by various conformance requirements) or 692 * return a result indicating failure. 693 */ 694 __API_AVAILABLE(macos(11.4), ios(17.4)) 695 __API_UNAVAILABLE(tvos, watchos, driverkit, visionos) 696 __SWIFT_UNAVAILABLE_MSG("This interface cannot be safely used from Swift") 697 int pthread_jit_write_with_callback_np( 698 pthread_jit_write_callback_t _Nonnull callback, void * _Nullable ctx); 699 700 /*! 701 * @function pthread_jit_write_freeze_callbacks_np 702 * 703 * @abstract 704 * Freezes the set of allowed pthread JIT write callbacks, preventing any 705 * callbacks in subsequently dlopen'd libraries from being allowed as arguments 706 * to {@link pthread_jit_write_with_callback_np} 707 * 708 * @discussion 709 * If the com.apple.security.cs.jit-write-allowlist-freeze-late entitlement is 710 * present, this function must be called exactly once after all libraries 711 * containing JIT write callbacks have been loaded to prevent any further 712 * runtime modifications to the set of allowed callbacks. Failing to call this 713 * function before calling pthread_jit_write_with_callback_np(3) for the first 714 * time is an error, as is calling it multiple times. 715 * 716 * If the jit-write-allowlist-freeze-late entitlement is not present, calling 717 * this function is an error. 718 * 719 * If an application does not need to dlopen(3) any libraries or frameworks 720 * containing needed JIT write callbacks, it is best to avoid the 721 * jit-write-allowlist-freeze-late entitlement and accompanying need to call 722 * this function, as this allows the runtime to automatically freeze the set of 723 * allowed callbacks early in process initialization. 724 */ 725 __API_AVAILABLE(macos(12.1), ios(17.4)) 726 __API_UNAVAILABLE(tvos, watchos, driverkit, visionos) 727 void pthread_jit_write_freeze_callbacks_np(void); 728 729 /*! 730 * @function pthread_cpu_number_np 731 * 732 * @param cpu_number_out 733 * The CPU number that the thread was running on at the time of query. 734 * This cpu number is in the interval [0, ncpus) (from sysctlbyname("hw.ncpu")) 735 * 736 * @result 737 * This function returns 0 or the value of errno if an error occurred. 738 * 739 * @note 740 * Optimizations of per-CPU datastructures based on the result of this function 741 * still require synchronization since it is not guaranteed that the thread will 742 * still be on the same CPU by the time the function returns. 743 */ 744 __API_AVAILABLE(macos(11.0), ios(14.2), tvos(14.2), watchos(7.1)) 745 int 746 pthread_cpu_number_np(size_t *cpu_number_out); 747 748 #endif /* (!_POSIX_C_SOURCE && !_XOPEN_SOURCE) || _DARWIN_C_SOURCE || __cplusplus */ 749 __END_DECLS 750 #if __has_feature(assume_nonnull) 751 _Pragma("clang assume_nonnull end") 752 #endif 753 754 #endif /* _PTHREAD_H */