1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  */
  26 
  27 /*
  28  * Copyright 2014 Garrett D'Amore <garrett@damore.org>
  29  */
  30 
  31 #ifndef _PTHREAD_H
  32 #define _PTHREAD_H
  33 
  34 #include <sys/feature_tests.h>
  35 
  36 #ifndef _ASM
  37 #include <sys/types.h>
  38 #include <time.h>
  39 #include <sched.h>
  40 #endif  /* _ASM */
  41 
  42 #ifdef  __cplusplus
  43 extern "C" {
  44 #endif
  45 
  46 /*
  47  * Thread related attribute values defined as in thread.h.
  48  * These are defined as bit pattern in thread.h.
  49  * Any change here should be reflected in thread.h.
  50  */
  51 /* detach */
  52 #define PTHREAD_CREATE_DETACHED         0x40    /* = THR_DETACHED */
  53 #define PTHREAD_CREATE_JOINABLE         0
  54 /* scope */
  55 #define PTHREAD_SCOPE_SYSTEM            0x01    /* = THR_BOUND */
  56 #define PTHREAD_SCOPE_PROCESS           0
  57 
  58 /*
  59  * Other attributes which are not defined in thread.h
  60  */
  61 /* inherit */
  62 #define PTHREAD_INHERIT_SCHED           1
  63 #define PTHREAD_EXPLICIT_SCHED          0
  64 
  65 /*
  66  * Value of process-shared attribute
  67  * These are defined as values defined in sys/synch.h
  68  * Any change here should be reflected in sys/synch.h.
  69  */
  70 #define PTHREAD_PROCESS_SHARED          1       /* = USYNC_PROCESS */
  71 #define PTHREAD_PROCESS_PRIVATE         0       /* = USYNC_THREAD */
  72 
  73 #define _DEFAULT_TYPE                   PTHREAD_PROCESS_PRIVATE
  74 #if !defined(__XOPEN_OR_POSIX) || defined(__EXTENSIONS__)
  75 #define DEFAULT_TYPE                    _DEFAULT_TYPE
  76 #endif
  77 
  78 /*
  79  * mutex types
  80  * keep these in synch which sys/synch.h lock flags
  81  */
  82 #define PTHREAD_MUTEX_NORMAL            0x0
  83 #define PTHREAD_MUTEX_ERRORCHECK        0x2
  84 #define PTHREAD_MUTEX_RECURSIVE         0x4
  85 #define PTHREAD_MUTEX_DEFAULT           PTHREAD_MUTEX_NORMAL
  86 
  87 /*
  88  * Mutex protocol values. Keep these in synch with sys/synch.h lock types.
  89  */
  90 #define PTHREAD_PRIO_NONE               0x0
  91 #define PTHREAD_PRIO_INHERIT            0x10
  92 #define PTHREAD_PRIO_PROTECT            0x20
  93 
  94 /*
  95  * Mutex robust attribute values.
  96  * Keep these in synch with sys/synch.h lock types.
  97  */
  98 #define PTHREAD_MUTEX_STALLED           0x0
  99 #define PTHREAD_MUTEX_ROBUST            0x40
 100 /*
 101  * Historical solaris-specific names,
 102  * from before pthread_mutexattr_getrobust() became standardized
 103  */
 104 #define PTHREAD_MUTEX_STALL_NP          PTHREAD_MUTEX_STALLED
 105 #define PTHREAD_MUTEX_ROBUST_NP         PTHREAD_MUTEX_ROBUST
 106 
 107 /*
 108  * macros - default initializers defined as in synch.h
 109  * Any change here should be reflected in synch.h.
 110  *
 111  * NOTE:
 112  * Make sure that any change in the macros is consistent with the definition
 113  * of the corresponding types in sys/types.h (e.g. PTHREAD_MUTEX_INITIALIZER
 114  * should be consistent with the definition for pthread_mutex_t).
 115  */
 116 #define PTHREAD_MUTEX_INITIALIZER               /* = DEFAULTMUTEX */    \
 117         {{0, 0, 0, _DEFAULT_TYPE, _MUTEX_MAGIC}, {{{0}}}, 0}
 118 
 119 #define PTHREAD_COND_INITIALIZER                /* = DEFAULTCV */       \
 120         {{{0, 0, 0, 0}, _DEFAULT_TYPE, _COND_MAGIC}, 0}
 121 
 122 #define PTHREAD_RWLOCK_INITIALIZER              /* = DEFAULTRWLOCK */   \
 123         {0, _DEFAULT_TYPE, _RWL_MAGIC, PTHREAD_MUTEX_INITIALIZER,       \
 124         PTHREAD_COND_INITIALIZER, PTHREAD_COND_INITIALIZER}
 125 
 126 /* cancellation type and state */
 127 #define PTHREAD_CANCEL_ENABLE           0x00
 128 #define PTHREAD_CANCEL_DISABLE          0x01
 129 #define PTHREAD_CANCEL_DEFERRED         0x00
 130 #define PTHREAD_CANCEL_ASYNCHRONOUS     0x02
 131 #define PTHREAD_CANCELED                (void *)-19
 132 
 133 /* pthread_once related values */
 134 #define PTHREAD_ONCE_NOTDONE    0
 135 #define PTHREAD_ONCE_DONE       1
 136 #define PTHREAD_ONCE_INIT       { {0, 0, 0, PTHREAD_ONCE_NOTDONE} }
 137 
 138 /*
 139  * The key to be created by pthread_key_create_once_np()
 140  * must be statically initialized with PTHREAD_ONCE_KEY_NP.
 141  * This must be the same as THR_ONCE_KEY in <thread.h>
 142  */
 143 #define PTHREAD_ONCE_KEY_NP     (pthread_key_t)(-1)
 144 
 145 /* barriers */
 146 #define PTHREAD_BARRIER_SERIAL_THREAD   -2
 147 
 148 #ifndef _ASM
 149 
 150 /*
 151  * cancellation cleanup structure
 152  */
 153 typedef struct _cleanup {
 154         uintptr_t       pthread_cleanup_pad[4];
 155 } _cleanup_t;
 156 
 157 #ifdef  __STDC__
 158 
 159 void    __pthread_cleanup_push(void (*)(void *), void *, caddr_t, _cleanup_t *);
 160 void    __pthread_cleanup_pop(int, _cleanup_t *);
 161 caddr_t _getfp(void);
 162 
 163 #else   /* __STDC__ */
 164 
 165 void    __pthread_cleanup_push();
 166 void    __pthread_cleanup_pop();
 167 caddr_t _getfp();
 168 
 169 #endif  /* __STDC__ */
 170 
 171 #if __cplusplus
 172 extern "C" {
 173 #endif
 174 
 175 typedef void (*_Voidfp)(void*); /* pointer to extern "C" function */
 176 
 177 #if __cplusplus
 178 } /* extern "C" */
 179 #endif
 180 
 181 #define pthread_cleanup_push(routine, args) { \
 182         _cleanup_t _cleanup_info; \
 183         __pthread_cleanup_push((_Voidfp)(routine), (void *)(args), \
 184                                 (caddr_t)_getfp(), &_cleanup_info);
 185 
 186 #define pthread_cleanup_pop(ex) \
 187         __pthread_cleanup_pop(ex, &_cleanup_info); \
 188 }
 189 
 190 /*
 191  * function prototypes - thread related calls
 192  */
 193 
 194 /*
 195  * pthread_atfork() is also declared in <unistd.h> as per SUSv2. The
 196  * declarations are identical. A change to either one may also require
 197  * appropriate namespace updates in order to avoid redeclaration
 198  * warnings in the case where both prototypes are exposed via inclusion
 199  * of both <pthread.h> and <unistd.h>.
 200  */
 201 extern int pthread_atfork(void (*) (void), void (*) (void), void (*) (void));
 202 extern int pthread_attr_init(pthread_attr_t *);
 203 extern int pthread_attr_destroy(pthread_attr_t *);
 204 extern int pthread_attr_setstack(pthread_attr_t *, void *, size_t);
 205 extern int pthread_attr_getstack(const pthread_attr_t *_RESTRICT_KYWD,
 206                 void **_RESTRICT_KYWD, size_t *_RESTRICT_KYWD);
 207 extern int pthread_attr_setstacksize(pthread_attr_t *, size_t);
 208 extern int pthread_attr_getstacksize(const pthread_attr_t *_RESTRICT_KYWD,
 209                 size_t *_RESTRICT_KYWD);
 210 
 211 #if !defined(_STRICT_SYMBOLS) && !defined(_XPG7)
 212 extern int pthread_attr_setstackaddr(pthread_attr_t *, void *);
 213 extern int pthread_attr_getstackaddr(const pthread_attr_t *_RESTRICT_KYWD,
 214                 void **_RESTRICT_KYWD);
 215 #endif
 216 
 217 extern int pthread_attr_setdetachstate(pthread_attr_t *, int);
 218 extern int pthread_attr_getdetachstate(const pthread_attr_t *, int *);
 219 extern int pthread_attr_setscope(pthread_attr_t *, int);
 220 extern int pthread_attr_getscope(const pthread_attr_t *_RESTRICT_KYWD,
 221         int *_RESTRICT_KYWD);
 222 extern int pthread_attr_setinheritsched(pthread_attr_t *, int);
 223 extern int pthread_attr_getinheritsched(const pthread_attr_t *_RESTRICT_KYWD,
 224         int *_RESTRICT_KYWD);
 225 extern int pthread_attr_setschedpolicy(pthread_attr_t *, int);
 226 extern int pthread_attr_getschedpolicy(const pthread_attr_t *_RESTRICT_KYWD,
 227         int *_RESTRICT_KYWD);
 228 extern int pthread_attr_setschedparam(pthread_attr_t *_RESTRICT_KYWD,
 229                 const struct sched_param *_RESTRICT_KYWD);
 230 extern int pthread_attr_getschedparam(const pthread_attr_t *_RESTRICT_KYWD,
 231                 struct sched_param *_RESTRICT_KYWD);
 232 extern int pthread_create(pthread_t *_RESTRICT_KYWD,
 233                 const pthread_attr_t *_RESTRICT_KYWD, void * (*)(void *),
 234                 void *_RESTRICT_KYWD);
 235 extern int pthread_once(pthread_once_t *, void (*)(void));
 236 extern int pthread_join(pthread_t, void **);
 237 extern int pthread_detach(pthread_t);
 238 extern void pthread_exit(void *) __NORETURN;
 239 extern int pthread_cancel(pthread_t);
 240 extern int pthread_setschedparam(pthread_t, int, const struct sched_param *);
 241 extern int pthread_getschedparam(pthread_t, int *_RESTRICT_KYWD,
 242                 struct sched_param *_RESTRICT_KYWD);
 243 extern int pthread_setschedprio(pthread_t, int);
 244 extern int pthread_setcancelstate(int, int *);
 245 extern int pthread_setcanceltype(int, int *);
 246 extern void pthread_testcancel(void);
 247 extern int pthread_equal(pthread_t, pthread_t);
 248 extern int pthread_key_create(pthread_key_t *, void (*)(void *));
 249 extern int pthread_key_create_once_np(pthread_key_t *, void (*)(void *));
 250 extern int pthread_key_delete(pthread_key_t);
 251 extern int pthread_setspecific(pthread_key_t, const void *);
 252 extern void *pthread_getspecific(pthread_key_t);
 253 extern pthread_t pthread_self(void);
 254 
 255 /*
 256  * function prototypes - synchronization related calls
 257  */
 258 extern int pthread_mutexattr_init(pthread_mutexattr_t *);
 259 extern int pthread_mutexattr_destroy(pthread_mutexattr_t *);
 260 extern int pthread_mutexattr_setpshared(pthread_mutexattr_t *, int);
 261 extern int pthread_mutexattr_getpshared(
 262         const pthread_mutexattr_t *_RESTRICT_KYWD, int *_RESTRICT_KYWD);
 263 extern int pthread_mutexattr_setprotocol(pthread_mutexattr_t *, int);
 264 extern int pthread_mutexattr_getprotocol(
 265         const pthread_mutexattr_t *_RESTRICT_KYWD, int *_RESTRICT_KYWD);
 266 extern int pthread_mutexattr_setprioceiling(pthread_mutexattr_t *, int);
 267 extern int pthread_mutexattr_getprioceiling(
 268         const pthread_mutexattr_t *_RESTRICT_KYWD, int *_RESTRICT_KYWD);
 269 extern int pthread_mutexattr_setrobust(pthread_mutexattr_t *, int);
 270 extern int pthread_mutexattr_getrobust(
 271         const pthread_mutexattr_t *_RESTRICT_KYWD, int *_RESTRICT_KYWD);
 272 extern int pthread_mutex_init(pthread_mutex_t *_RESTRICT_KYWD,
 273         const pthread_mutexattr_t *_RESTRICT_KYWD);
 274 extern int pthread_mutex_consistent(pthread_mutex_t *);
 275 extern int pthread_mutex_destroy(pthread_mutex_t *);
 276 extern int pthread_mutex_lock(pthread_mutex_t *);
 277 extern int pthread_mutex_timedlock(pthread_mutex_t *_RESTRICT_KYWD,
 278         const struct timespec *_RESTRICT_KYWD);
 279 extern int pthread_mutex_reltimedlock_np(pthread_mutex_t *_RESTRICT_KYWD,
 280         const struct timespec *_RESTRICT_KYWD);
 281 extern int pthread_mutex_unlock(pthread_mutex_t *);
 282 extern int pthread_mutex_trylock(pthread_mutex_t *);
 283 extern int pthread_mutex_setprioceiling(pthread_mutex_t *_RESTRICT_KYWD,
 284         int, int *_RESTRICT_KYWD);
 285 extern int pthread_mutex_getprioceiling(const pthread_mutex_t *_RESTRICT_KYWD,
 286         int *_RESTRICT_KYWD);
 287 extern int pthread_condattr_init(pthread_condattr_t *);
 288 extern int pthread_condattr_destroy(pthread_condattr_t *);
 289 extern int pthread_condattr_setclock(pthread_condattr_t *, clockid_t);
 290 extern int pthread_condattr_getclock(const pthread_condattr_t *_RESTRICT_KYWD,
 291         clockid_t *_RESTRICT_KYWD);
 292 extern int pthread_condattr_setpshared(pthread_condattr_t *, int);
 293 extern int pthread_condattr_getpshared(const pthread_condattr_t *_RESTRICT_KYWD,
 294         int *_RESTRICT_KYWD);
 295 extern int pthread_cond_init(pthread_cond_t *_RESTRICT_KYWD,
 296         const pthread_condattr_t *_RESTRICT_KYWD);
 297 extern int pthread_cond_destroy(pthread_cond_t *);
 298 extern int pthread_cond_broadcast(pthread_cond_t *);
 299 extern int pthread_cond_signal(pthread_cond_t *);
 300 extern int pthread_cond_wait(pthread_cond_t *_RESTRICT_KYWD,
 301         pthread_mutex_t *_RESTRICT_KYWD);
 302 extern int pthread_cond_timedwait(pthread_cond_t *_RESTRICT_KYWD,
 303         pthread_mutex_t *_RESTRICT_KYWD, const struct timespec *_RESTRICT_KYWD);
 304 extern int pthread_cond_reltimedwait_np(pthread_cond_t *_RESTRICT_KYWD,
 305         pthread_mutex_t *_RESTRICT_KYWD, const struct timespec *_RESTRICT_KYWD);
 306 extern int pthread_attr_getguardsize(const pthread_attr_t *_RESTRICT_KYWD,
 307         size_t *_RESTRICT_KYWD);
 308 extern int pthread_attr_setguardsize(pthread_attr_t *, size_t);
 309 extern int pthread_getconcurrency(void);
 310 extern int pthread_setconcurrency(int);
 311 extern int pthread_mutexattr_settype(pthread_mutexattr_t *, int);
 312 extern int pthread_mutexattr_gettype(const pthread_mutexattr_t *_RESTRICT_KYWD,
 313         int *_RESTRICT_KYWD);
 314 extern int pthread_rwlock_init(pthread_rwlock_t *_RESTRICT_KYWD,
 315         const pthread_rwlockattr_t *_RESTRICT_KYWD);
 316 extern int pthread_rwlock_destroy(pthread_rwlock_t *);
 317 extern int pthread_rwlock_rdlock(pthread_rwlock_t *);
 318 extern int pthread_rwlock_timedrdlock(pthread_rwlock_t *_RESTRICT_KYWD,
 319         const struct timespec *_RESTRICT_KYWD);
 320 extern int pthread_rwlock_reltimedrdlock_np(pthread_rwlock_t *_RESTRICT_KYWD,
 321         const struct timespec *_RESTRICT_KYWD);
 322 extern int pthread_rwlock_tryrdlock(pthread_rwlock_t *);
 323 extern int pthread_rwlock_wrlock(pthread_rwlock_t *);
 324 extern int pthread_rwlock_timedwrlock(pthread_rwlock_t *_RESTRICT_KYWD,
 325         const struct timespec *_RESTRICT_KYWD);
 326 extern int pthread_rwlock_reltimedwrlock_np(pthread_rwlock_t *_RESTRICT_KYWD,
 327         const struct timespec *_RESTRICT_KYWD);
 328 extern int pthread_rwlock_trywrlock(pthread_rwlock_t *);
 329 extern int pthread_rwlock_unlock(pthread_rwlock_t *);
 330 extern int pthread_rwlockattr_init(pthread_rwlockattr_t *);
 331 extern int pthread_rwlockattr_destroy(pthread_rwlockattr_t *);
 332 extern int pthread_rwlockattr_getpshared(
 333         const pthread_rwlockattr_t *_RESTRICT_KYWD, int *_RESTRICT_KYWD);
 334 extern int pthread_rwlockattr_setpshared(pthread_rwlockattr_t *, int);
 335 extern int pthread_spin_init(pthread_spinlock_t *, int);
 336 extern int pthread_spin_destroy(pthread_spinlock_t *);
 337 extern int pthread_spin_lock(pthread_spinlock_t *);
 338 extern int pthread_spin_trylock(pthread_spinlock_t *);
 339 extern int pthread_spin_unlock(pthread_spinlock_t *);
 340 extern int pthread_barrierattr_init(pthread_barrierattr_t *);
 341 extern int pthread_barrierattr_destroy(pthread_barrierattr_t *);
 342 extern int pthread_barrierattr_setpshared(pthread_barrierattr_t *, int);
 343 extern int pthread_barrierattr_getpshared(
 344         const pthread_barrierattr_t *_RESTRICT_KYWD, int *_RESTRICT_KYWD);
 345 extern int pthread_barrier_init(pthread_barrier_t *_RESTRICT_KYWD,
 346         const pthread_barrierattr_t *_RESTRICT_KYWD, uint_t);
 347 extern int pthread_barrier_destroy(pthread_barrier_t *);
 348 extern int pthread_barrier_wait(pthread_barrier_t *);
 349 
 350 /* Historical names -- present only for binary compatibility */
 351 extern int pthread_mutex_consistent_np(pthread_mutex_t *);
 352 extern int pthread_mutexattr_setrobust_np(pthread_mutexattr_t *, int);
 353 extern int pthread_mutexattr_getrobust_np(
 354         const pthread_mutexattr_t *_RESTRICT_KYWD, int *_RESTRICT_KYWD);
 355 
 356 #endif  /* _ASM */
 357 
 358 #ifdef  __cplusplus
 359 }
 360 #endif
 361 
 362 #endif  /* _PTHREAD_H */