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