xref: /openbsd-src/lib/librthread/rthread.h (revision 5ecdd0566b441ae0b99e73f410875e05dc0fa5b7)
1*5ecdd056Smpi /*	$OpenBSD: rthread.h,v 1.64 2019/02/13 13:22:14 mpi Exp $ */
21a251377Stedu /*
355aa0b8cStedu  * Copyright (c) 2004,2005 Ted Unangst <tedu@openbsd.org>
41a251377Stedu  * All Rights Reserved.
51a251377Stedu  *
61a251377Stedu  * Permission to use, copy, modify, and distribute this software for any
71a251377Stedu  * purpose with or without fee is hereby granted, provided that the above
81a251377Stedu  * copyright notice and this permission notice appear in all copies.
91a251377Stedu  *
101a251377Stedu  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
111a251377Stedu  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
121a251377Stedu  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
131a251377Stedu  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
141a251377Stedu  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
151a251377Stedu  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
161a251377Stedu  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
171a251377Stedu  */
181a251377Stedu /*
191a251377Stedu  * Private data structures that back up the typedefs in pthread.h.
201a251377Stedu  * Since only the thread library cares about their size or arrangement,
211a251377Stedu  * it should be possible to switch libraries without relinking.
228fa6d383Smarco  *
23f050dd83Sakfaew  * Do not reorder _atomic_lock_t and sem_t variables in the structs.
248fa6d383Smarco  * This is due to alignment requirements of certain arches like hppa.
258fa6d383Smarco  * The current requirement is 16 bytes.
26f3d519f9Stedu  *
27f3d519f9Stedu  * THE MACHINE DEPENDENT CERROR CODE HAS HARD CODED OFFSETS INTO PTHREAD_T!
281a251377Stedu  */
291a251377Stedu 
30a5511fa9Sguenther #ifndef _RTHREAD_H_
31a5511fa9Sguenther #define _RTHREAD_H_
32a5511fa9Sguenther 
33e10ae76eSmarc #include <semaphore.h>
34a5511fa9Sguenther #include "thread_private.h"
359fc2d97aSotto 
36f75388a7Skurt #ifdef __LP64__
37f75388a7Skurt #define RTHREAD_STACK_SIZE_DEF (512 * 1024)
38f75388a7Skurt #else
39f75388a7Skurt #define RTHREAD_STACK_SIZE_DEF (256 * 1024)
40f75388a7Skurt #endif
412b8233b0Smarc 
421a251377Stedu struct stack {
4358dbb15cSguenther 	SLIST_ENTRY(stack)	link;	/* link for free default stacks */
4458dbb15cSguenther 	void	*sp;			/* machine stack pointer */
4558dbb15cSguenther 	void	*base;			/* bottom of allocated area */
4658dbb15cSguenther 	size_t	guardsize;		/* size of PROT_NONE zone or */
4758dbb15cSguenther 					/* ==1 if application alloced */
4858dbb15cSguenther 	size_t	len;			/* total size of allocated stack */
491a251377Stedu };
501a251377Stedu 
51a5511fa9Sguenther #define	PTHREAD_MIN_PRIORITY	0
52a5511fa9Sguenther #define	PTHREAD_MAX_PRIORITY	31
531a251377Stedu 
541a251377Stedu 
551a251377Stedu struct pthread_rwlockattr {
5675dc9675Sguenther 	int pshared;
571a251377Stedu };
581a251377Stedu 
59c9961bb2Spirofti struct pthread_barrier {
60c9961bb2Spirofti 	pthread_mutex_t mutex;
61c9961bb2Spirofti 	pthread_cond_t cond;
62c9961bb2Spirofti 	int threshold;
637446b51cStedu 	int in;
647446b51cStedu 	int out;
65c9961bb2Spirofti 	int generation;
66c9961bb2Spirofti };
67c9961bb2Spirofti 
68c9961bb2Spirofti struct pthread_barrierattr {
69c9961bb2Spirofti 	int pshared;
70c9961bb2Spirofti };
71c9961bb2Spirofti 
725037ac14Spirofti struct pthread_spinlock {
73f050dd83Sakfaew 	_atomic_lock_t lock;
745037ac14Spirofti 	pthread_t owner;
755037ac14Spirofti };
765037ac14Spirofti 
7758dbb15cSguenther 
7858dbb15cSguenther #define	ROUND_TO_PAGE(size) \
7958dbb15cSguenther 	(((size) + (_thread_pagesize - 1)) & ~(_thread_pagesize - 1))
809fc2d97aSotto 
81fe38b55cSguenther __BEGIN_HIDDEN_DECLS
8237ebc96dSguenther int	_sem_wait(sem_t, int, const struct timespec *, int *);
83cabfd54fStedu int	_sem_post(sem_t);
841a251377Stedu 
85fe38b55cSguenther void	_rthread_init(void);
862b8233b0Smarc struct stack *_rthread_alloc_stack(pthread_t);
872b8233b0Smarc void	_rthread_free_stack(struct stack *);
88b8ec2dbdSguenther #ifndef NO_PIC
89dc989ac6Skurt void	_rthread_dl_lock(int what);
90dc989ac6Skurt #endif
91e02c9995Sotto 
92fe38b55cSguenther extern int _threads_ready;
93fe38b55cSguenther extern size_t _thread_pagesize;
94fe38b55cSguenther extern LIST_HEAD(listhead, pthread) _thread_list;
95f050dd83Sakfaew extern _atomic_lock_t _thread_lock;
96fe38b55cSguenther extern struct pthread_attr _rthread_attr_default;
97fe38b55cSguenther __END_HIDDEN_DECLS
984524a593Stedu 
992b8233b0Smarc void	_thread_dump_info(void);
1002b8233b0Smarc 
10129088be8Sguenther #define REDIRECT_SYSCALL(x)		typeof(x) x asm("_thread_sys_"#x)
102a5511fa9Sguenther 
103a5511fa9Sguenther #endif /* _RTHREAD_H_ */
104