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