xref: /openbsd-src/lib/librthread/rthread.h (revision ae3cb403620ab940fbaabb3055fac045a63d56b7)
1 /*	$OpenBSD: rthread.h,v 1.63 2017/09/05 02:40:54 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 _atomic_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  * THE MACHINE DEPENDENT CERROR CODE HAS HARD CODED OFFSETS INTO PTHREAD_T!
28  */
29 
30 #ifndef _RTHREAD_H_
31 #define _RTHREAD_H_
32 
33 #include <semaphore.h>
34 #include "thread_private.h"
35 
36 #ifdef __LP64__
37 #define RTHREAD_STACK_SIZE_DEF (512 * 1024)
38 #else
39 #define RTHREAD_STACK_SIZE_DEF (256 * 1024)
40 #endif
41 
42 struct stack {
43 	SLIST_ENTRY(stack)	link;	/* link for free default stacks */
44 	void	*sp;			/* machine stack pointer */
45 	void	*base;			/* bottom of allocated area */
46 	size_t	guardsize;		/* size of PROT_NONE zone or */
47 					/* ==1 if application alloced */
48 	size_t	len;			/* total size of allocated stack */
49 };
50 
51 #define	PTHREAD_MIN_PRIORITY	0
52 #define	PTHREAD_MAX_PRIORITY	31
53 
54 
55 struct pthread_rwlock {
56 	_atomic_lock_t lock;
57 	pthread_t owner;
58 	struct pthread_queue writers;
59 	int readers;
60 };
61 
62 struct pthread_rwlockattr {
63 	int pshared;
64 };
65 
66 struct pthread_barrier {
67 	pthread_mutex_t mutex;
68 	pthread_cond_t cond;
69 	int threshold;
70 	int in;
71 	int out;
72 	int generation;
73 };
74 
75 struct pthread_barrierattr {
76 	int pshared;
77 };
78 
79 struct pthread_spinlock {
80 	_atomic_lock_t lock;
81 	pthread_t owner;
82 };
83 
84 
85 #define	ROUND_TO_PAGE(size) \
86 	(((size) + (_thread_pagesize - 1)) & ~(_thread_pagesize - 1))
87 
88 __BEGIN_HIDDEN_DECLS
89 int	_sem_wait(sem_t, int, const struct timespec *, int *);
90 int	_sem_post(sem_t);
91 
92 void	_rthread_init(void);
93 struct stack *_rthread_alloc_stack(pthread_t);
94 void	_rthread_free_stack(struct stack *);
95 #ifndef NO_PIC
96 void	_rthread_dl_lock(int what);
97 #endif
98 
99 extern int _threads_ready;
100 extern size_t _thread_pagesize;
101 extern LIST_HEAD(listhead, pthread) _thread_list;
102 extern _atomic_lock_t _thread_lock;
103 extern struct pthread_attr _rthread_attr_default;
104 __END_HIDDEN_DECLS
105 
106 void	_thread_dump_info(void);
107 
108 #define REDIRECT_SYSCALL(x)		typeof(x) x asm("_thread_sys_"#x)
109 
110 #endif /* _RTHREAD_H_ */
111