1 /* $OpenBSD: rthread.h,v 1.21 2009/04/21 12:43:01 kurt 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 struct pthread_mutex { 53 struct sem sem; 54 int type; 55 pthread_t owner; 56 int count; 57 }; 58 59 struct pthread_mutex_attr { 60 int type; 61 }; 62 63 struct pthread_cond { 64 struct sem sem; 65 }; 66 67 struct pthread_cond_attr { 68 int shared; 69 }; 70 71 struct pthread_rwlock { 72 struct sem sem; 73 _spinlock_lock_t lock; 74 int readers; 75 int writer; 76 }; 77 78 struct pthread_rwlockattr { 79 int dummy; 80 }; 81 82 struct pthread_attr { 83 void *stack_addr; 84 size_t stack_size; 85 size_t guard_size; 86 int detach_state; 87 int contention_scope; 88 int sched_policy; 89 struct sched_param sched_param; 90 int sched_inherit; 91 int create_suspended; 92 }; 93 94 struct rthread_key { 95 int used; 96 void (*destructor)(void *); 97 }; 98 99 struct rthread_storage { 100 int keyid; 101 struct rthread_storage *next; 102 void *data; 103 }; 104 105 struct rthread_cleanup_fn { 106 void (*fn)(void *); 107 void *arg; 108 struct rthread_cleanup_fn *next; 109 }; 110 111 struct pthread { 112 struct sem donesem; 113 pid_t tid; 114 unsigned int flags; 115 _spinlock_lock_t flags_lock; 116 void *retval; 117 void *(*fn)(void *); 118 void *arg; 119 char name[32]; 120 struct stack *stack; 121 LIST_ENTRY(pthread) threads; 122 int sched_policy; 123 struct pthread_attr attr; 124 struct sched_param sched_param; 125 struct rthread_storage *local_storage; 126 struct rthread_cleanup_fn *cleanup_fns; 127 }; 128 #define THREAD_DONE 0x001 129 #define THREAD_DETACHED 0x002 130 #define THREAD_CANCELLED 0x004 131 #define THREAD_CANCEL_ENABLE 0x008 132 #define THREAD_CANCEL_DEFERRED 0x010 133 134 extern int _threads_ready; 135 extern LIST_HEAD(listhead, pthread) _thread_list; 136 extern struct pthread _initial_thread; 137 extern _spinlock_lock_t _thread_lock; 138 extern int _rthread_kq; 139 140 void _spinlock(_spinlock_lock_t *); 141 void _spinunlock(_spinlock_lock_t *); 142 int _sem_wait(sem_t, int, int); 143 int _sem_waitl(sem_t, int, int); 144 int _sem_post(sem_t); 145 int _sem_wakeup(sem_t); 146 int _sem_wakeall(sem_t); 147 148 struct stack *_rthread_alloc_stack(pthread_t); 149 void _rthread_free_stack(struct stack *); 150 void _rthread_tls_destructors(pthread_t); 151 void _rthread_debug(int, const char *, ...) 152 __attribute__((__format__ (printf, 2, 3))); 153 void _rthread_debug_init(void); 154 void _rthread_add_to_reaper(pid_t, struct stack *); 155 void _rthread_reaper(void); 156 int _rthread_open_kqueue(void); 157 #if defined(__ELF__) && defined(PIC) 158 void _rthread_dl_lock(int what); 159 void _rthread_bind_lock(int); 160 #endif 161 162 163 void _thread_dump_info(void); 164 165 int _atomic_lock(register volatile _spinlock_lock_t *); 166 167 /* syscalls */ 168 int getthrid(void); 169 void threxit(int); 170 int thrsleep(void *, int, void *); 171 int thrwakeup(void *, int n); 172 int sched_yield(void); 173 int thrsigdivert(sigset_t); 174