1168404Spjd/*
2168404Spjd * CDDL HEADER START
3168404Spjd *
4168404Spjd * The contents of this file are subject to the terms of the
5168404Spjd * Common Development and Distribution License (the "License").
6168404Spjd * You may not use this file except in compliance with the License.
7168404Spjd *
8168404Spjd * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9168404Spjd * or http://www.opensolaris.org/os/licensing.
10168404Spjd * See the License for the specific language governing permissions
11168404Spjd * and limitations under the License.
12168404Spjd *
13168404Spjd * When distributing Covered Code, include this CDDL HEADER in each
14168404Spjd * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15168404Spjd * If applicable, add the following below this CDDL HEADER, with the
16168404Spjd * fields enclosed by brackets "[]" replaced with your own identifying
17168404Spjd * information: Portions Copyright [yyyy] [name of copyright owner]
18168404Spjd *
19168404Spjd * CDDL HEADER END
20168404Spjd */
21168404Spjd
22168404Spjd/*
23168404Spjd * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24168404Spjd * Use is subject to license terms.
25168404Spjd */
26168404Spjd
27168404Spjd#ifndef	_THREAD_H
28168404Spjd#define	_THREAD_H
29168404Spjd
30168404Spjd#pragma ident	"%Z%%M%	%I%	%E% SMI"
31168404Spjd
32168404Spjd#include <pthread.h>
33185029Spjd#include <pthread_np.h>
34168404Spjd#include <assert.h>
35168404Spjd
36168404Spjd/*
37168404Spjd * Compatibility thread stuff needed for Solaris -> Linux port
38168404Spjd */
39168404Spjd
40168404Spjdtypedef pthread_t thread_t;
41168404Spjdtypedef pthread_mutex_t mutex_t;
42168404Spjdtypedef pthread_cond_t cond_t;
43168404Spjdtypedef pthread_rwlock_t rwlock_t;
44168404Spjd
45168404Spjd#define USYNC_THREAD 0
46168404Spjd
47168404Spjd#define	thr_self()		(unsigned long)pthread_self()
48168404Spjd#define	thr_equal(a,b)		pthread_equal(a,b)
49168404Spjd#define	thr_join(t,d,s)		pthread_join(t,s)
50168404Spjd#define	thr_exit(r)		pthread_exit(r)
51168404Spjd#define	_mutex_init(l,f,a)	pthread_mutex_init(l,NULL)
52168404Spjd#define	_mutex_destroy(l)	pthread_mutex_destroy(l)
53168404Spjd#define	mutex_lock(l)		pthread_mutex_lock(l)
54168404Spjd#define	mutex_trylock(l)	pthread_mutex_trylock(l)
55168404Spjd#define	mutex_unlock(l)		pthread_mutex_unlock(l)
56168404Spjd#define	rwlock_init(l,f,a)	pthread_rwlock_init(l,NULL)
57168404Spjd#define	rwlock_destroy(l)	pthread_rwlock_destroy(l)
58168404Spjd#define	rw_rdlock(l)		pthread_rwlock_rdlock(l)
59168404Spjd#define	rw_wrlock(l)		pthread_rwlock_wrlock(l)
60168404Spjd#define	rw_tryrdlock(l)		pthread_rwlock_tryrdlock(l)
61168404Spjd#define	rw_trywrlock(l)		pthread_rwlock_trywrlock(l)
62168404Spjd#define	rw_unlock(l)		pthread_rwlock_unlock(l)
63168404Spjd#define	cond_init(l,f,a)	pthread_cond_init(l,NULL)
64168404Spjd#define	cond_destroy(l)		pthread_cond_destroy(l)
65168404Spjd#define	cond_wait(l,m)		pthread_cond_wait(l,m)
66168404Spjd#define	cond_signal(l)		pthread_cond_signal(l)
67168404Spjd#define	cond_broadcast(l)	pthread_cond_broadcast(l)
68168404Spjd
69168404Spjd#define THR_BOUND     0x00000001  /* = PTHREAD_SCOPE_SYSTEM */
70168404Spjd#define THR_NEW_LWP   0x00000002
71168404Spjd#define THR_DETACHED  0x00000040  /* = PTHREAD_CREATE_DETACHED */
72168404Spjd#define THR_SUSPENDED 0x00000080
73168404Spjd#define THR_DAEMON    0x00000100
74168404Spjd
75168404Spjdstatic __inline int
76168404Spjdthr_create(void *stack_base, size_t stack_size, void *(*start_func) (void*),
77168404Spjd    void *arg, long flags, thread_t *new_thread_ID)
78168404Spjd{
79227703Spjd	pthread_t dummy;
80168404Spjd	int ret;
81168404Spjd
82168404Spjd	assert(stack_base == NULL);
83168404Spjd	assert(stack_size == 0);
84168404Spjd	assert((flags & ~THR_BOUND & ~THR_DETACHED) == 0);
85168404Spjd
86168404Spjd	pthread_attr_t attr;
87168404Spjd	pthread_attr_init(&attr);
88168404Spjd
89227703Spjd	if (flags & THR_DETACHED)
90168404Spjd		pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
91168404Spjd
92227703Spjd	if (new_thread_ID == NULL)
93227703Spjd		new_thread_ID = &dummy;
94227703Spjd
95168404Spjd	/* This function ignores the THR_BOUND flag, since NPTL doesn't seem to support PTHREAD_SCOPE_PROCESS */
96168404Spjd
97168404Spjd	ret = pthread_create(new_thread_ID, &attr, start_func, arg);
98168404Spjd
99168404Spjd	pthread_attr_destroy(&attr);
100168404Spjd
101168404Spjd	return (ret);
102168404Spjd}
103168404Spjd
104168404Spjd#endif	/* _THREAD_H */
105