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