1 /* $OpenBSD: rthread.h,v 1.22 2009/10/21 16:05:48 guenther Exp $ */ 2 /* 3 * Copyright (c) 2004,2005 Ted Unangst <tedu@openbsd.org> 4 * All Rights Reserved. 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 /* 19 * Private data structures that back up the typedefs in pthread.h. 20 * Since only the thread library cares about their size or arrangement, 21 * it should be possible to switch libraries without relinking. 22 * 23 * Do not reorder _spinlock_lock_t and sem_t variables in the structs. 24 * This is due to alignment requirements of certain arches like hppa. 25 * The current requirement is 16 bytes. 26 */ 27 28 #include <sys/queue.h> 29 #include <semaphore.h> 30 31 #ifdef __LP64__ 32 #define RTHREAD_STACK_SIZE_DEF (512 * 1024) 33 #else 34 #define RTHREAD_STACK_SIZE_DEF (256 * 1024) 35 #endif 36 37 struct stack { 38 void *sp; 39 void *base; 40 void *guard; 41 size_t guardsize; 42 size_t len; 43 }; 44 45 struct sem { 46 _spinlock_lock_t lock; 47 volatile int waitcount; 48 volatile int value; 49 int pad; 50 }; 51 52 TAILQ_HEAD(pthread_queue, pthread); 53 54 struct pthread_mutex { 55 struct sem sem; 56 int type; 57 pthread_t owner; 58 int count; 59 }; 60 61 struct pthread_mutex_attr { 62 int type; 63 }; 64 65 struct pthread_cond { 66 struct sem sem; 67 }; 68 69 struct pthread_cond_attr { 70 int shared; 71 }; 72 73 struct pthread_rwlock { 74 struct sem sem; 75 _spinlock_lock_t lock; 76 int readers; 77 int writer; 78 }; 79 80 struct pthread_rwlockattr { 81 int dummy; 82 }; 83 84 struct pthread_attr { 85 void *stack_addr; 86 size_t stack_size; 87 size_t guard_size; 88 int detach_state; 89 int contention_scope; 90 int sched_policy; 91 struct sched_param sched_param; 92 int sched_inherit; 93 int create_suspended; 94 }; 95 96 struct rthread_key { 97 int used; 98 void (*destructor)(void *); 99 }; 100 101 struct rthread_storage { 102 int keyid; 103 struct rthread_storage *next; 104 void *data; 105 }; 106 107 struct rthread_cleanup_fn { 108 void (*fn)(void *); 109 void *arg; 110 struct rthread_cleanup_fn *next; 111 }; 112 113 struct pthread { 114 struct sem donesem; 115 pid_t tid; 116 unsigned int flags; 117 _spinlock_lock_t flags_lock; 118 void *retval; 119 void *(*fn)(void *); 120 void *arg; 121 char name[32]; 122 struct stack *stack; 123 LIST_ENTRY(pthread) threads; 124 TAILQ_ENTRY(pthread) waiting; 125 int sched_policy; 126 struct pthread_attr attr; 127 struct sched_param sched_param; 128 struct rthread_storage *local_storage; 129 struct rthread_cleanup_fn *cleanup_fns; 130 }; 131 #define THREAD_DONE 0x001 132 #define THREAD_DETACHED 0x002 133 #define THREAD_CANCELLED 0x004 134 #define THREAD_CANCEL_ENABLE 0x008 135 #define THREAD_CANCEL_DEFERRED 0x010 136 137 extern int _threads_ready; 138 extern LIST_HEAD(listhead, pthread) _thread_list; 139 extern struct pthread _initial_thread; 140 extern _spinlock_lock_t _thread_lock; 141 extern int _rthread_kq; 142 143 void _spinlock(_spinlock_lock_t *); 144 void _spinunlock(_spinlock_lock_t *); 145 int _sem_wait(sem_t, int, int); 146 int _sem_waitl(sem_t, int, int); 147 int _sem_post(sem_t); 148 int _sem_wakeup(sem_t); 149 int _sem_wakeall(sem_t); 150 151 struct stack *_rthread_alloc_stack(pthread_t); 152 void _rthread_free_stack(struct stack *); 153 void _rthread_tls_destructors(pthread_t); 154 void _rthread_debug(int, const char *, ...) 155 __attribute__((__format__ (printf, 2, 3))); 156 void _rthread_debug_init(void); 157 void _rthread_add_to_reaper(pid_t, struct stack *); 158 void _rthread_reaper(void); 159 int _rthread_open_kqueue(void); 160 #if defined(__ELF__) && defined(PIC) 161 void _rthread_dl_lock(int what); 162 void _rthread_bind_lock(int); 163 #endif 164 165 166 void _thread_dump_info(void); 167 168 int _atomic_lock(register volatile _spinlock_lock_t *); 169 170 /* syscalls */ 171 int getthrid(void); 172 void threxit(int); 173 int thrsleep(void *, int, void *); 174 int thrwakeup(void *, int n); 175 int sched_yield(void); 176 int thrsigdivert(sigset_t); 177