1 1.27 riastrad /* $NetBSD: pthread_types.h,v 1.27 2022/04/10 10:38:33 riastradh Exp $ */ 2 1.2 thorpej 3 1.2 thorpej /*- 4 1.24 ad * Copyright (c) 2001, 2008, 2020 The NetBSD Foundation, Inc. 5 1.2 thorpej * All rights reserved. 6 1.2 thorpej * 7 1.2 thorpej * This code is derived from software contributed to The NetBSD Foundation 8 1.2 thorpej * by Nathan J. Williams. 9 1.2 thorpej * 10 1.2 thorpej * Redistribution and use in source and binary forms, with or without 11 1.2 thorpej * modification, are permitted provided that the following conditions 12 1.2 thorpej * are met: 13 1.2 thorpej * 1. Redistributions of source code must retain the above copyright 14 1.2 thorpej * notice, this list of conditions and the following disclaimer. 15 1.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright 16 1.2 thorpej * notice, this list of conditions and the following disclaimer in the 17 1.2 thorpej * documentation and/or other materials provided with the distribution. 18 1.2 thorpej * 19 1.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 1.2 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 1.2 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 1.2 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 1.2 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 1.2 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 1.2 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 1.2 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 1.2 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 1.2 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 1.2 thorpej * POSSIBILITY OF SUCH DAMAGE. 30 1.2 thorpej */ 31 1.2 thorpej 32 1.2 thorpej #ifndef _LIB_PTHREAD_TYPES_H 33 1.2 thorpej #define _LIB_PTHREAD_TYPES_H 34 1.2 thorpej 35 1.5 nathanw /* 36 1.5 nathanw * We use the "pthread_spin_t" name internally; "pthread_spinlock_t" is the 37 1.27 riastrad * POSIX spinlock object. 38 1.17 pooka * 39 1.17 pooka * C++ expects to be using PTHREAD_FOO_INITIALIZER as a member initializer. 40 1.17 pooka * This does not work for volatile types. Since C++ does not touch the guts 41 1.17 pooka * of those types, we do not include volatile in the C++ definitions. 42 1.2 thorpej */ 43 1.17 pooka typedef __cpu_simple_lock_t pthread_spin_t; 44 1.17 pooka #ifdef __cplusplus 45 1.17 pooka typedef __cpu_simple_lock_nv_t __pthread_spin_t; 46 1.17 pooka #define __pthread_volatile 47 1.17 pooka #else 48 1.17 pooka typedef pthread_spin_t __pthread_spin_t; 49 1.17 pooka #define __pthread_volatile volatile 50 1.17 pooka #endif 51 1.2 thorpej 52 1.2 thorpej /* 53 1.2 thorpej * Copied from PTQ_HEAD in pthread_queue.h 54 1.2 thorpej */ 55 1.2 thorpej #define _PTQ_HEAD(name, type) \ 56 1.2 thorpej struct name { \ 57 1.2 thorpej struct type *ptqh_first;/* first element */ \ 58 1.2 thorpej struct type **ptqh_last;/* addr of last next element */ \ 59 1.2 thorpej } 60 1.2 thorpej 61 1.7 ad _PTQ_HEAD(pthread_queue_struct_t, __pthread_st); 62 1.7 ad typedef struct pthread_queue_struct_t pthread_queue_t; 63 1.2 thorpej 64 1.4 nathanw struct __pthread_st; 65 1.4 nathanw struct __pthread_attr_st; 66 1.4 nathanw struct __pthread_mutex_st; 67 1.4 nathanw struct __pthread_mutexattr_st; 68 1.4 nathanw struct __pthread_cond_st; 69 1.4 nathanw struct __pthread_condattr_st; 70 1.4 nathanw struct __pthread_spin_st; 71 1.4 nathanw struct __pthread_rwlock_st; 72 1.4 nathanw struct __pthread_rwlockattr_st; 73 1.4 nathanw struct __pthread_barrier_st; 74 1.4 nathanw struct __pthread_barrierattr_st; 75 1.4 nathanw 76 1.4 nathanw typedef struct __pthread_st *pthread_t; 77 1.4 nathanw typedef struct __pthread_attr_st pthread_attr_t; 78 1.4 nathanw typedef struct __pthread_mutex_st pthread_mutex_t; 79 1.4 nathanw typedef struct __pthread_mutexattr_st pthread_mutexattr_t; 80 1.4 nathanw typedef struct __pthread_cond_st pthread_cond_t; 81 1.4 nathanw typedef struct __pthread_condattr_st pthread_condattr_t; 82 1.4 nathanw typedef struct __pthread_once_st pthread_once_t; 83 1.4 nathanw typedef struct __pthread_spinlock_st pthread_spinlock_t; 84 1.4 nathanw typedef struct __pthread_rwlock_st pthread_rwlock_t; 85 1.4 nathanw typedef struct __pthread_rwlockattr_st pthread_rwlockattr_t; 86 1.4 nathanw typedef struct __pthread_barrier_st pthread_barrier_t; 87 1.4 nathanw typedef struct __pthread_barrierattr_st pthread_barrierattr_t; 88 1.2 thorpej typedef int pthread_key_t; 89 1.2 thorpej 90 1.4 nathanw struct __pthread_attr_st { 91 1.2 thorpej unsigned int pta_magic; 92 1.2 thorpej 93 1.2 thorpej int pta_flags; 94 1.2 thorpej void *pta_private; 95 1.2 thorpej }; 96 1.2 thorpej 97 1.6 ad /* 98 1.13 matt * ptm_owner is the actual lock field which is locked via CAS operation. 99 1.13 matt * This structure's layout is designed to compatible with the previous 100 1.13 matt * version used in SA pthreads. 101 1.6 ad */ 102 1.13 matt #ifdef __CPU_SIMPLE_LOCK_PAD 103 1.13 matt /* 104 1.13 matt * If __SIMPLE_UNLOCKED != 0 and we have to pad, we have to worry about 105 1.13 matt * endianness. Currently that isn't an issue but put in a check in case 106 1.13 matt * something changes in the future. 107 1.13 matt */ 108 1.13 matt #if __SIMPLELOCK_UNLOCKED != 0 109 1.13 matt #error __CPU_SIMPLE_LOCK_PAD incompatible with __SIMPLELOCK_UNLOCKED == 0 110 1.13 matt #endif 111 1.13 matt #endif 112 1.4 nathanw struct __pthread_mutex_st { 113 1.2 thorpej unsigned int ptm_magic; 114 1.17 pooka __pthread_spin_t ptm_errorcheck; 115 1.13 matt #ifdef __CPU_SIMPLE_LOCK_PAD 116 1.13 matt uint8_t ptm_pad1[3]; 117 1.23 kamil #if (__STDC_VERSION__ - 0) >= 199901L 118 1.23 kamil #define _PTHREAD_MUTEX_PAD(a) .a = { 0, 0, 0 }, 119 1.23 kamil #else 120 1.21 christos #define _PTHREAD_MUTEX_PAD(a) { 0, 0, 0 }, 121 1.21 christos #endif 122 1.20 christos #else 123 1.20 christos #define _PTHREAD_MUTEX_PAD(a) 124 1.13 matt #endif 125 1.19 skrll union { 126 1.19 skrll unsigned char ptm_ceiling; 127 1.19 skrll __pthread_spin_t ptm_unused; 128 1.19 skrll }; 129 1.13 matt #ifdef __CPU_SIMPLE_LOCK_PAD 130 1.13 matt uint8_t ptm_pad2[3]; 131 1.13 matt #endif 132 1.17 pooka __pthread_volatile pthread_t ptm_owner; 133 1.25 ad void * __pthread_volatile ptm_waiters; 134 1.9 ad unsigned int ptm_recursed; 135 1.13 matt void *ptm_spare2; /* unused - backwards compat */ 136 1.2 thorpej }; 137 1.2 thorpej 138 1.2 thorpej #define _PT_MUTEX_MAGIC 0x33330003 139 1.2 thorpej #define _PT_MUTEX_DEAD 0xDEAD0003 140 1.2 thorpej 141 1.23 kamil #if (__STDC_VERSION__ - 0) >= 199901L 142 1.23 kamil #define _PTHREAD_MUTEX_INI(a, b) .a = b 143 1.23 kamil #define _PTHREAD_MUTEX_UNI(a) .a = 0 144 1.23 kamil #else 145 1.21 christos #define _PTHREAD_MUTEX_INI(a, b) b 146 1.22 christos #define _PTHREAD_MUTEX_UNI(a) { 0 } 147 1.21 christos #endif 148 1.21 christos 149 1.21 christos #define _PTHREAD_MUTEX_INITIALIZER { \ 150 1.21 christos _PTHREAD_MUTEX_INI(ptm_magic, _PT_MUTEX_MAGIC), \ 151 1.21 christos _PTHREAD_MUTEX_INI(ptm_errorcheck, __SIMPLELOCK_UNLOCKED), \ 152 1.21 christos _PTHREAD_MUTEX_PAD(ptm_pad1) \ 153 1.22 christos _PTHREAD_MUTEX_UNI(ptm_ceiling), \ 154 1.21 christos _PTHREAD_MUTEX_PAD(ptm_pad2) \ 155 1.21 christos _PTHREAD_MUTEX_INI(ptm_owner, NULL), \ 156 1.21 christos _PTHREAD_MUTEX_INI(ptm_waiters, NULL), \ 157 1.21 christos _PTHREAD_MUTEX_INI(ptm_recursed, 0), \ 158 1.21 christos _PTHREAD_MUTEX_INI(ptm_spare2, NULL), \ 159 1.20 christos } 160 1.2 thorpej 161 1.4 nathanw struct __pthread_mutexattr_st { 162 1.2 thorpej unsigned int ptma_magic; 163 1.2 thorpej void *ptma_private; 164 1.2 thorpej }; 165 1.2 thorpej 166 1.2 thorpej #define _PT_MUTEXATTR_MAGIC 0x44440004 167 1.2 thorpej #define _PT_MUTEXATTR_DEAD 0xDEAD0004 168 1.2 thorpej 169 1.2 thorpej 170 1.4 nathanw struct __pthread_cond_st { 171 1.2 thorpej unsigned int ptc_magic; 172 1.2 thorpej 173 1.2 thorpej /* Protects the queue of waiters */ 174 1.17 pooka __pthread_spin_t ptc_lock; 175 1.26 christos void *__pthread_volatile ptc_waiters; 176 1.24 ad void *ptc_spare; 177 1.2 thorpej 178 1.2 thorpej pthread_mutex_t *ptc_mutex; /* Current mutex */ 179 1.2 thorpej void *ptc_private; 180 1.2 thorpej }; 181 1.2 thorpej 182 1.2 thorpej #define _PT_COND_MAGIC 0x55550005 183 1.2 thorpej #define _PT_COND_DEAD 0xDEAD0005 184 1.2 thorpej 185 1.4 nathanw #define _PTHREAD_COND_INITIALIZER { _PT_COND_MAGIC, \ 186 1.2 thorpej __SIMPLELOCK_UNLOCKED, \ 187 1.24 ad NULL, \ 188 1.24 ad NULL, \ 189 1.2 thorpej NULL, \ 190 1.2 thorpej NULL \ 191 1.2 thorpej } 192 1.2 thorpej 193 1.4 nathanw struct __pthread_condattr_st { 194 1.2 thorpej unsigned int ptca_magic; 195 1.2 thorpej void *ptca_private; 196 1.2 thorpej }; 197 1.2 thorpej 198 1.2 thorpej #define _PT_CONDATTR_MAGIC 0x66660006 199 1.2 thorpej #define _PT_CONDATTR_DEAD 0xDEAD0006 200 1.2 thorpej 201 1.4 nathanw struct __pthread_once_st { 202 1.2 thorpej pthread_mutex_t pto_mutex; 203 1.2 thorpej int pto_done; 204 1.2 thorpej }; 205 1.2 thorpej 206 1.4 nathanw #define _PTHREAD_ONCE_INIT { PTHREAD_MUTEX_INITIALIZER, 0 } 207 1.2 thorpej 208 1.4 nathanw struct __pthread_spinlock_st { 209 1.2 thorpej unsigned int pts_magic; 210 1.17 pooka __pthread_spin_t pts_spin; 211 1.2 thorpej int pts_flags; 212 1.2 thorpej }; 213 1.27 riastrad 214 1.2 thorpej #define _PT_SPINLOCK_MAGIC 0x77770007 215 1.2 thorpej #define _PT_SPINLOCK_DEAD 0xDEAD0007 216 1.2 thorpej #define _PT_SPINLOCK_PSHARED 0x00000001 217 1.2 thorpej 218 1.2 thorpej /* PTHREAD_SPINLOCK_INITIALIZER is an extension not specified by POSIX. */ 219 1.4 nathanw #define _PTHREAD_SPINLOCK_INITIALIZER { _PT_SPINLOCK_MAGIC, \ 220 1.2 thorpej __SIMPLELOCK_UNLOCKED, \ 221 1.2 thorpej 0 \ 222 1.2 thorpej } 223 1.2 thorpej 224 1.4 nathanw struct __pthread_rwlock_st { 225 1.2 thorpej unsigned int ptr_magic; 226 1.2 thorpej 227 1.2 thorpej /* Protects data below */ 228 1.17 pooka __pthread_spin_t ptr_interlock; 229 1.2 thorpej 230 1.7 ad pthread_queue_t ptr_rblocked; 231 1.7 ad pthread_queue_t ptr_wblocked; 232 1.2 thorpej unsigned int ptr_nreaders; 233 1.17 pooka __pthread_volatile pthread_t ptr_owner; 234 1.2 thorpej void *ptr_private; 235 1.2 thorpej }; 236 1.2 thorpej 237 1.2 thorpej #define _PT_RWLOCK_MAGIC 0x99990009 238 1.2 thorpej #define _PT_RWLOCK_DEAD 0xDEAD0009 239 1.2 thorpej 240 1.4 nathanw #define _PTHREAD_RWLOCK_INITIALIZER { _PT_RWLOCK_MAGIC, \ 241 1.2 thorpej __SIMPLELOCK_UNLOCKED, \ 242 1.2 thorpej {NULL, NULL}, \ 243 1.2 thorpej {NULL, NULL}, \ 244 1.2 thorpej 0, \ 245 1.2 thorpej NULL, \ 246 1.2 thorpej NULL, \ 247 1.2 thorpej } 248 1.2 thorpej 249 1.4 nathanw struct __pthread_rwlockattr_st { 250 1.2 thorpej unsigned int ptra_magic; 251 1.2 thorpej void *ptra_private; 252 1.2 thorpej }; 253 1.2 thorpej 254 1.2 thorpej #define _PT_RWLOCKATTR_MAGIC 0x99990909 255 1.2 thorpej #define _PT_RWLOCKATTR_DEAD 0xDEAD0909 256 1.2 thorpej 257 1.4 nathanw struct __pthread_barrier_st { 258 1.2 thorpej unsigned int ptb_magic; 259 1.2 thorpej 260 1.2 thorpej /* Protects data below */ 261 1.2 thorpej pthread_spin_t ptb_lock; 262 1.2 thorpej 263 1.7 ad pthread_queue_t ptb_waiters; 264 1.2 thorpej unsigned int ptb_initcount; 265 1.2 thorpej unsigned int ptb_curcount; 266 1.3 nathanw unsigned int ptb_generation; 267 1.2 thorpej 268 1.2 thorpej void *ptb_private; 269 1.2 thorpej }; 270 1.2 thorpej 271 1.2 thorpej #define _PT_BARRIER_MAGIC 0x88880008 272 1.2 thorpej #define _PT_BARRIER_DEAD 0xDEAD0008 273 1.2 thorpej 274 1.4 nathanw struct __pthread_barrierattr_st { 275 1.2 thorpej unsigned int ptba_magic; 276 1.2 thorpej void *ptba_private; 277 1.2 thorpej }; 278 1.2 thorpej 279 1.2 thorpej #define _PT_BARRIERATTR_MAGIC 0x88880808 280 1.2 thorpej #define _PT_BARRIERATTR_DEAD 0xDEAD0808 281 1.2 thorpej 282 1.2 thorpej #endif /* _LIB_PTHREAD_TYPES_H */ 283