xref: /openbsd/lib/librthread/rthread.h (revision 771fbea0)
1 /*	$OpenBSD: rthread.h,v 1.64 2019/02/13 13:22:14 mpi 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_rwlockattr {
56 	int pshared;
57 };
58 
59 struct pthread_barrier {
60 	pthread_mutex_t mutex;
61 	pthread_cond_t cond;
62 	int threshold;
63 	int in;
64 	int out;
65 	int generation;
66 };
67 
68 struct pthread_barrierattr {
69 	int pshared;
70 };
71 
72 struct pthread_spinlock {
73 	_atomic_lock_t lock;
74 	pthread_t owner;
75 };
76 
77 
78 #define	ROUND_TO_PAGE(size) \
79 	(((size) + (_thread_pagesize - 1)) & ~(_thread_pagesize - 1))
80 
81 __BEGIN_HIDDEN_DECLS
82 int	_sem_wait(sem_t, int, const struct timespec *, int *);
83 int	_sem_post(sem_t);
84 
85 void	_rthread_init(void);
86 struct stack *_rthread_alloc_stack(pthread_t);
87 void	_rthread_free_stack(struct stack *);
88 #ifndef NO_PIC
89 void	_rthread_dl_lock(int what);
90 #endif
91 
92 extern int _threads_ready;
93 extern size_t _thread_pagesize;
94 extern LIST_HEAD(listhead, pthread) _thread_list;
95 extern _atomic_lock_t _thread_lock;
96 extern struct pthread_attr _rthread_attr_default;
97 __END_HIDDEN_DECLS
98 
99 void	_thread_dump_info(void);
100 
101 #define REDIRECT_SYSCALL(x)		typeof(x) x asm("_thread_sys_"#x)
102 
103 #endif /* _RTHREAD_H_ */
104