xref: /netbsd/external/cddl/osnet/dist/head/thread.h (revision 6e66cb26)
1*6e66cb26Shannken /*
2*6e66cb26Shannken  * CDDL HEADER START
3*6e66cb26Shannken  *
4*6e66cb26Shannken  * The contents of this file are subject to the terms of the
5*6e66cb26Shannken  * Common Development and Distribution License (the "License").
6*6e66cb26Shannken  * You may not use this file except in compliance with the License.
7*6e66cb26Shannken  *
8*6e66cb26Shannken  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*6e66cb26Shannken  * or http://www.opensolaris.org/os/licensing.
10*6e66cb26Shannken  * See the License for the specific language governing permissions
11*6e66cb26Shannken  * and limitations under the License.
12*6e66cb26Shannken  *
13*6e66cb26Shannken  * When distributing Covered Code, include this CDDL HEADER in each
14*6e66cb26Shannken  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*6e66cb26Shannken  * If applicable, add the following below this CDDL HEADER, with the
16*6e66cb26Shannken  * fields enclosed by brackets "[]" replaced with your own identifying
17*6e66cb26Shannken  * information: Portions Copyright [yyyy] [name of copyright owner]
18*6e66cb26Shannken  *
19*6e66cb26Shannken  * CDDL HEADER END
20*6e66cb26Shannken  */
21*6e66cb26Shannken 
22*6e66cb26Shannken /*
23*6e66cb26Shannken  * Copyright 2014 Garrett D'Amore <garrett@damore.org>
24*6e66cb26Shannken  *
25*6e66cb26Shannken  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
26*6e66cb26Shannken  * Use is subject to license terms.
27*6e66cb26Shannken  */
28*6e66cb26Shannken 
29*6e66cb26Shannken #ifndef	_THREAD_H
30*6e66cb26Shannken #define	_THREAD_H
31*6e66cb26Shannken 
32*6e66cb26Shannken #include <pthread.h>
33*6e66cb26Shannken 
34*6e66cb26Shannken #ifndef __NetBSD__
35*6e66cb26Shannken #include <pthread_np.h>
36*6e66cb26Shannken #endif
37*6e66cb26Shannken 
38*6e66cb26Shannken #include <assert.h>
39*6e66cb26Shannken 
40*6e66cb26Shannken /*
41*6e66cb26Shannken  * Compatibility thread stuff needed for Solaris -> Linux port
42*6e66cb26Shannken  */
43*6e66cb26Shannken 
44*6e66cb26Shannken typedef pthread_t thread_t;
45*6e66cb26Shannken typedef pthread_mutex_t mutex_t;
46*6e66cb26Shannken typedef pthread_cond_t cond_t;
47*6e66cb26Shannken typedef pthread_rwlock_t rwlock_t;
48*6e66cb26Shannken 
49*6e66cb26Shannken #define USYNC_THREAD 0
50*6e66cb26Shannken 
51*6e66cb26Shannken #define	thr_self()		(unsigned long)pthread_self()
52*6e66cb26Shannken #define	thr_equal(a,b)		pthread_equal(a,b)
53*6e66cb26Shannken #define	thr_join(t,d,s)		pthread_join(t,s)
54*6e66cb26Shannken #define	thr_exit(r)		pthread_exit(r)
55*6e66cb26Shannken #define	_mutex_init(l,f,a)	pthread_mutex_init(l,NULL)
56*6e66cb26Shannken #define	_mutex_destroy(l)	pthread_mutex_destroy(l)
57*6e66cb26Shannken #define	mutex_lock(l)		pthread_mutex_lock(l)
58*6e66cb26Shannken #define	mutex_trylock(l)	pthread_mutex_trylock(l)
59*6e66cb26Shannken #define	mutex_unlock(l)		pthread_mutex_unlock(l)
60*6e66cb26Shannken #define	rwlock_init(l,f,a)	pthread_rwlock_init(l,NULL)
61*6e66cb26Shannken #define	rwlock_destroy(l)	pthread_rwlock_destroy(l)
62*6e66cb26Shannken #define	rw_rdlock(l)		pthread_rwlock_rdlock(l)
63*6e66cb26Shannken #define	rw_wrlock(l)		pthread_rwlock_wrlock(l)
64*6e66cb26Shannken #define	rw_tryrdlock(l)		pthread_rwlock_tryrdlock(l)
65*6e66cb26Shannken #define	rw_trywrlock(l)		pthread_rwlock_trywrlock(l)
66*6e66cb26Shannken #define	rw_unlock(l)		pthread_rwlock_unlock(l)
67*6e66cb26Shannken #define	cond_init(l,f,a)	pthread_cond_init(l,NULL)
68*6e66cb26Shannken #define	cond_destroy(l)		pthread_cond_destroy(l)
69*6e66cb26Shannken #define	cond_wait(l,m)		pthread_cond_wait(l,m)
70*6e66cb26Shannken #define	cond_signal(l)		pthread_cond_signal(l)
71*6e66cb26Shannken #define	cond_broadcast(l)	pthread_cond_broadcast(l)
72*6e66cb26Shannken 
73*6e66cb26Shannken #define THR_BOUND     0x00000001  /* = PTHREAD_SCOPE_SYSTEM */
74*6e66cb26Shannken #define THR_NEW_LWP   0x00000002
75*6e66cb26Shannken #define THR_DETACHED  0x00000040  /* = PTHREAD_CREATE_DETACHED */
76*6e66cb26Shannken #define THR_SUSPENDED 0x00000080
77*6e66cb26Shannken #define THR_DAEMON    0x00000100
78*6e66cb26Shannken 
79*6e66cb26Shannken static __inline int
thr_create(void * stack_base,size_t stack_size,void * (* start_func)(void *),void * arg,long flags,thread_t * new_thread_ID)80*6e66cb26Shannken thr_create(void *stack_base, size_t stack_size, void *(*start_func) (void*),
81*6e66cb26Shannken     void *arg, long flags, thread_t *new_thread_ID)
82*6e66cb26Shannken {
83*6e66cb26Shannken 	pthread_t dummy;
84*6e66cb26Shannken 	int ret;
85*6e66cb26Shannken 
86*6e66cb26Shannken 	assert(stack_base == NULL);
87*6e66cb26Shannken 	assert(stack_size == 0);
88*6e66cb26Shannken 	assert((flags & ~THR_BOUND & ~THR_DETACHED) == 0);
89*6e66cb26Shannken 
90*6e66cb26Shannken 	pthread_attr_t attr;
91*6e66cb26Shannken 	pthread_attr_init(&attr);
92*6e66cb26Shannken 
93*6e66cb26Shannken 	if (flags & THR_DETACHED)
94*6e66cb26Shannken 		pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
95*6e66cb26Shannken 
96*6e66cb26Shannken 	if (new_thread_ID == NULL)
97*6e66cb26Shannken 		new_thread_ID = &dummy;
98*6e66cb26Shannken 
99*6e66cb26Shannken 	/* This function ignores the THR_BOUND flag, since NPTL doesn't seem to support PTHREAD_SCOPE_PROCESS */
100*6e66cb26Shannken 
101*6e66cb26Shannken 	ret = pthread_create(new_thread_ID, &attr, start_func, arg);
102*6e66cb26Shannken 
103*6e66cb26Shannken 	pthread_attr_destroy(&attr);
104*6e66cb26Shannken 
105*6e66cb26Shannken 	return (ret);
106*6e66cb26Shannken }
107*6e66cb26Shannken 
108*6e66cb26Shannken #endif	/* _THREAD_H */
109