Home | History | Annotate | Line # | Download | only in head
      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 (at) 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 
     34 #ifndef __NetBSD__
     35 #include <pthread_np.h>
     36 #endif
     37 
     38 #include <assert.h>
     39 
     40 /*
     41  * Compatibility thread stuff needed for Solaris -> Linux port
     42  */
     43 
     44 typedef pthread_t thread_t;
     45 typedef pthread_mutex_t mutex_t;
     46 typedef pthread_cond_t cond_t;
     47 typedef pthread_rwlock_t rwlock_t;
     48 
     49 #define USYNC_THREAD 0
     50 
     51 #define	thr_self()		(unsigned long)pthread_self()
     52 #define	thr_equal(a,b)		pthread_equal(a,b)
     53 #define	thr_join(t,d,s)		pthread_join(t,s)
     54 #define	thr_exit(r)		pthread_exit(r)
     55 #define	_mutex_init(l,f,a)	pthread_mutex_init(l,NULL)
     56 #define	_mutex_destroy(l)	pthread_mutex_destroy(l)
     57 #define	mutex_lock(l)		pthread_mutex_lock(l)
     58 #define	mutex_trylock(l)	pthread_mutex_trylock(l)
     59 #define	mutex_unlock(l)		pthread_mutex_unlock(l)
     60 #define	rwlock_init(l,f,a)	pthread_rwlock_init(l,NULL)
     61 #define	rwlock_destroy(l)	pthread_rwlock_destroy(l)
     62 #define	rw_rdlock(l)		pthread_rwlock_rdlock(l)
     63 #define	rw_wrlock(l)		pthread_rwlock_wrlock(l)
     64 #define	rw_tryrdlock(l)		pthread_rwlock_tryrdlock(l)
     65 #define	rw_trywrlock(l)		pthread_rwlock_trywrlock(l)
     66 #define	rw_unlock(l)		pthread_rwlock_unlock(l)
     67 #define	cond_init(l,f,a)	pthread_cond_init(l,NULL)
     68 #define	cond_destroy(l)		pthread_cond_destroy(l)
     69 #define	cond_wait(l,m)		pthread_cond_wait(l,m)
     70 #define	cond_signal(l)		pthread_cond_signal(l)
     71 #define	cond_broadcast(l)	pthread_cond_broadcast(l)
     72 
     73 #define THR_BOUND     0x00000001  /* = PTHREAD_SCOPE_SYSTEM */
     74 #define THR_NEW_LWP   0x00000002
     75 #define THR_DETACHED  0x00000040  /* = PTHREAD_CREATE_DETACHED */
     76 #define THR_SUSPENDED 0x00000080
     77 #define THR_DAEMON    0x00000100
     78 
     79 static __inline int
     80 thr_create(void *stack_base, size_t stack_size, void *(*start_func) (void*),
     81     void *arg, long flags, thread_t *new_thread_ID)
     82 {
     83 	pthread_t dummy;
     84 	int ret;
     85 
     86 	assert(stack_base == NULL);
     87 	assert(stack_size == 0);
     88 	assert((flags & ~THR_BOUND & ~THR_DETACHED) == 0);
     89 
     90 	pthread_attr_t attr;
     91 	pthread_attr_init(&attr);
     92 
     93 	if (flags & THR_DETACHED)
     94 		pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
     95 
     96 	if (new_thread_ID == NULL)
     97 		new_thread_ID = &dummy;
     98 
     99 	/* This function ignores the THR_BOUND flag, since NPTL doesn't seem to support PTHREAD_SCOPE_PROCESS */
    100 
    101 	ret = pthread_create(new_thread_ID, &attr, start_func, arg);
    102 
    103 	pthread_attr_destroy(&attr);
    104 
    105 	return (ret);
    106 }
    107 
    108 #endif	/* _THREAD_H */
    109