xref: /openbsd-src/lib/librthread/rthread.h (revision daf88648c0e349d5c02e1504293082072c981640)
1 /*	$OpenBSD: rthread.h,v 1.17 2006/01/05 04:06:48 marc 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 	int sigpend;
123 	struct rthread_cleanup_fn *cleanup_fns;
124 };
125 #define	THREAD_DONE		0x001
126 #define	THREAD_DETACHED		0x002
127 #define THREAD_CANCELLED	0x004
128 #define THREAD_CANCEL_ENABLE	0x008
129 #define THREAD_CANCEL_DEFERRED	0x010
130 
131 extern int _threads_ready;
132 extern LIST_HEAD(listhead, pthread) _thread_list;
133 extern struct pthread _initial_thread;
134 extern _spinlock_lock_t _thread_lock;
135 extern int _rthread_kq;
136 
137 void	_spinlock(_spinlock_lock_t *);
138 void	_spinunlock(_spinlock_lock_t *);
139 int	_sem_wait(sem_t, int, int);
140 int	_sem_waitl(sem_t, int, int);
141 int	_sem_post(sem_t);
142 int	_sem_wakeup(sem_t);
143 int	_sem_wakeall(sem_t);
144 
145 struct stack *_rthread_alloc_stack(pthread_t);
146 void	_rthread_free_stack(struct stack *);
147 void	_rthread_tls_destructors(pthread_t);
148 void	_rthread_debug(int, const char *, ...)
149 		__attribute__((__format__ (printf, 2, 3)));
150 void	_rthread_debug_init(void);
151 void	_rthread_add_to_reaper(pid_t, struct stack *);
152 void 	_rthread_reaper(void);
153 
154 
155 void	_thread_dump_info(void);
156 
157 int	_atomic_lock(register volatile _spinlock_lock_t *);
158 
159 /* syscalls */
160 int getthrid(void);
161 void threxit(int);
162 int thrsleep(void *, int, void *);
163 int thrwakeup(void *, int n);
164 int sched_yield(void);
165 int thrsigdivert(const sigset_t *);
166