Module Name:    src
Committed By:   riz
Date:           Mon Apr 29 01:50:20 UTC 2013

Modified Files:
        src/lib/libc/include [netbsd-6]: reentrant.h
        src/lib/libc/thread-stub [netbsd-6]: thread-stub.c
        src/lib/libpthread [netbsd-6]: Makefile pthread.c pthread_cancelstub.c
            pthread_cond.c pthread_int.h pthread_misc.c pthread_mutex.c
            pthread_rwlock.c pthread_specific.c pthread_tsd.c

Log Message:
Pull up following revision(s) (requested by manu in ticket #869):
        lib/libpthread/pthread_rwlock.c: revision 1.33
        lib/libc/include/reentrant.h: revision 1.16
        lib/libpthread/pthread_cond.c: revision 1.59
        lib/libpthread/pthread_misc.c: revision 1.15
        lib/libc/thread-stub/thread-stub.c: revision 1.23
        lib/libpthread/pthread_cancelstub.c: revision 1.38
        lib/libpthread/pthread_specific.c: revision 1.26
        lib/libpthread/pthread_mutex.c: revision 1.56
        lib/libpthread/pthread_tsd.c: revision 1.11
        lib/libpthread/Makefile: revision 1.80
        lib/libpthread/pthread.c: revision 1.143
        lib/libpthread/pthread_int.h: revision 1.89
- Allow libpthread to be dlopened again, by providing libc stubs to
libpthread.
- Fail if the dlopened libpthread does pthread_create(). From manu@
- Discussed at length in the mailing lists; approved by core@
- This was chosen as the least intrusive patch that will provide
  the necessary functionality.
XXX: pullup to 6


To generate a diff of this commit:
cvs rdiff -u -r1.14.10.1 -r1.14.10.2 src/lib/libc/include/reentrant.h
cvs rdiff -u -r1.22 -r1.22.4.1 src/lib/libc/thread-stub/thread-stub.c
cvs rdiff -u -r1.73 -r1.73.2.1 src/lib/libpthread/Makefile
cvs rdiff -u -r1.125.4.2 -r1.125.4.3 src/lib/libpthread/pthread.c
cvs rdiff -u -r1.35.6.1 -r1.35.6.2 src/lib/libpthread/pthread_cancelstub.c
cvs rdiff -u -r1.56.8.2 -r1.56.8.3 src/lib/libpthread/pthread_cond.c
cvs rdiff -u -r1.82.2.2 -r1.82.2.3 src/lib/libpthread/pthread_int.h
cvs rdiff -u -r1.14 -r1.14.8.1 src/lib/libpthread/pthread_misc.c
cvs rdiff -u -r1.51 -r1.51.22.1 src/lib/libpthread/pthread_mutex.c
cvs rdiff -u -r1.32 -r1.32.22.1 src/lib/libpthread/pthread_rwlock.c
cvs rdiff -u -r1.21.22.2 -r1.21.22.3 src/lib/libpthread/pthread_specific.c
cvs rdiff -u -r1.7.24.2 -r1.7.24.3 src/lib/libpthread/pthread_tsd.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/lib/libc/include/reentrant.h
diff -u src/lib/libc/include/reentrant.h:1.14.10.1 src/lib/libc/include/reentrant.h:1.14.10.2
--- src/lib/libc/include/reentrant.h:1.14.10.1	Sat Jun 23 22:54:55 2012
+++ src/lib/libc/include/reentrant.h	Mon Apr 29 01:50:20 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: reentrant.h,v 1.14.10.1 2012/06/23 22:54:55 riz Exp $	*/
+/*	$NetBSD: reentrant.h,v 1.14.10.2 2013/04/29 01:50:20 riz Exp $	*/
 
 /*-
  * Copyright (c) 1997, 1998, 2003 The NetBSD Foundation, Inc.
@@ -221,6 +221,56 @@ __END_DECLS
 #define	thr_enabled()		(__isthreaded)
 #define thr_setcancelstate(n, o) __libc_thr_setcancelstate((n),(o))
 #define thr_curcpu()		__libc_thr_curcpu()
+
+#else /* __LIBC_THREAD_STUBS */
+
+__BEGIN_DECLS
+void	__libc_thr_init_stub(void);
+
+int	__libc_mutex_init_stub(mutex_t *, const mutexattr_t *);
+int	__libc_mutex_lock_stub(mutex_t *);
+int	__libc_mutex_trylock_stub(mutex_t *);
+int	__libc_mutex_unlock_stub(mutex_t *);
+int	__libc_mutex_destroy_stub(mutex_t *);
+
+int	__libc_mutexattr_init_stub(mutexattr_t *); 
+int	__libc_mutexattr_destroy_stub(mutexattr_t *);
+int	__libc_mutexattr_settype_stub(mutexattr_t *, int);
+
+int	__libc_cond_init_stub(cond_t *, const condattr_t *);
+int	__libc_cond_signal_stub(cond_t *);
+int	__libc_cond_broadcast_stub(cond_t *);
+int	__libc_cond_wait_stub(cond_t *, mutex_t *);
+int	__libc_cond_timedwait_stub(cond_t *, mutex_t *,
+				   const struct timespec *);
+int	__libc_cond_destroy_stub(cond_t *);
+
+int	__libc_rwlock_init_stub(rwlock_t *, const rwlockattr_t *);
+int	__libc_rwlock_rdlock_stub(rwlock_t *);
+int	__libc_rwlock_wrlock_stub(rwlock_t *);
+int	__libc_rwlock_tryrdlock_stub(rwlock_t *);
+int	__libc_rwlock_trywrlock_stub(rwlock_t *);
+int	__libc_rwlock_unlock_stub(rwlock_t *);
+int	__libc_rwlock_destroy_stub(rwlock_t *);
+
+int	__libc_thr_keycreate_stub(thread_key_t *, void (*)(void *));
+int	__libc_thr_setspecific_stub(thread_key_t, const void *);
+void	*__libc_thr_getspecific_stub(thread_key_t);
+int	__libc_thr_keydelete_stub(thread_key_t);
+
+int	__libc_thr_once_stub(once_t *, void (*)(void));
+int	__libc_thr_sigsetmask_stub(int, const sigset_t *, sigset_t *);
+thr_t	__libc_thr_self_stub(void);
+int	__libc_thr_yield_stub(void);
+int	__libc_thr_create_stub(thr_t *, const thrattr_t *,
+	    void *(*)(void *), void *);
+void	__libc_thr_exit_stub(void *);
+int	*__libc_thr_errno_stub(void);
+int	__libc_thr_setcancelstate_stub(int, int *);
+int	__libc_thr_equal_stub(pthread_t, pthread_t);
+unsigned int	__libc_thr_curcpu_stub(void);
+__END_DECLS
+
 #endif /* __LIBC_THREAD_STUBS */
 
 #define	FLOCKFILE(fp)		__flockfile_internal(fp, 1)

Index: src/lib/libc/thread-stub/thread-stub.c
diff -u src/lib/libc/thread-stub/thread-stub.c:1.22 src/lib/libc/thread-stub/thread-stub.c:1.22.4.1
--- src/lib/libc/thread-stub/thread-stub.c:1.22	Fri Sep 16 16:05:59 2011
+++ src/lib/libc/thread-stub/thread-stub.c	Mon Apr 29 01:50:20 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: thread-stub.c,v 1.22 2011/09/16 16:05:59 joerg Exp $	*/
+/*	$NetBSD: thread-stub.c,v 1.22.4.1 2013/04/29 01:50:20 riz Exp $	*/
 
 /*-
  * Copyright (c) 2003, 2009 The NetBSD Foundation, Inc.
@@ -31,7 +31,7 @@
 
 #include <sys/cdefs.h>
 #if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: thread-stub.c,v 1.22 2011/09/16 16:05:59 joerg Exp $");
+__RCSID("$NetBSD: thread-stub.c,v 1.22.4.1 2013/04/29 01:50:20 riz Exp $");
 #endif /* LIBC_SCCS and not lint */
 
 /*
@@ -69,9 +69,6 @@ do {					\
 
 /* libpthread init */
 
-void	__libc_thr_init(void);
-void	__libc_thr_init_stub(void);
-
 __weak_alias(__libc_thr_init,__libc_thr_init_stub)
 
 void
@@ -83,8 +80,7 @@ __libc_thr_init_stub(void)
 
 /* mutexes */
 
-int	__libc_mutex_init_stub(mutex_t *, const mutexattr_t *);
-int	__libc_mutex_catchall_stub(mutex_t *);
+int __libc_mutex_catchall_stub(mutex_t *);
 
 __weak_alias(__libc_mutex_init,__libc_mutex_init_stub)
 __weak_alias(__libc_mutex_lock,__libc_mutex_catchall_stub)
@@ -92,13 +88,20 @@ __weak_alias(__libc_mutex_trylock,__libc
 __weak_alias(__libc_mutex_unlock,__libc_mutex_catchall_stub)
 __weak_alias(__libc_mutex_destroy,__libc_mutex_catchall_stub)
 
-int	__libc_mutexattr_catchall_stub(mutexattr_t *);
-int	__libc_mutexattr_settype_stub(mutexattr_t *, int);
+__strong_alias(__libc_mutex_lock_stub,__libc_mutex_catchall_stub)
+__strong_alias(__libc_mutex_trylock_stub,__libc_mutex_catchall_stub)
+__strong_alias(__libc_mutex_unlock_stub,__libc_mutex_catchall_stub)
+__strong_alias(__libc_mutex_destroy_stub,__libc_mutex_catchall_stub)
+
+int __libc_mutexattr_catchall_stub(mutexattr_t *);
 
 __weak_alias(__libc_mutexattr_init,__libc_mutexattr_catchall_stub)
 __weak_alias(__libc_mutexattr_destroy,__libc_mutexattr_catchall_stub)
 __weak_alias(__libc_mutexattr_settype,__libc_mutexattr_settype_stub)
 
+__strong_alias(__libc_mutexattr_init_stub,__libc_mutexattr_catchall_stub)
+__strong_alias(__libc_mutexattr_destroy_stub,__libc_mutexattr_catchall_stub)
+
 int
 __libc_mutex_init_stub(mutex_t *m, const mutexattr_t *a)
 {
@@ -147,11 +150,7 @@ __libc_mutexattr_catchall_stub(mutexattr
 
 /* condition variables */
 
-int	__libc_cond_init_stub(cond_t *, const condattr_t *);
-int	__libc_cond_wait_stub(cond_t *, mutex_t *);
-int	__libc_cond_timedwait_stub(cond_t *, mutex_t *,
-				   const struct timespec *);
-int	__libc_cond_catchall_stub(cond_t *);
+int __libc_cond_catchall_stub(cond_t *);
 
 __weak_alias(__libc_cond_init,__libc_cond_init_stub)
 __weak_alias(__libc_cond_signal,__libc_cond_catchall_stub)
@@ -160,6 +159,11 @@ __weak_alias(__libc_cond_wait,__libc_con
 __weak_alias(__libc_cond_timedwait,__libc_cond_timedwait_stub)
 __weak_alias(__libc_cond_destroy,__libc_cond_catchall_stub)
 
+__strong_alias(__libc_cond_signal_stub,__libc_cond_catchall_stub)
+__strong_alias(__libc_cond_broadcast_stub,__libc_cond_catchall_stub)
+__strong_alias(__libc_cond_wait_stub,__libc_cond_catchall_stub)
+__strong_alias(__libc_cond_destroy_stub,__libc_cond_catchall_stub)
+
 int
 __libc_cond_init_stub(cond_t *c, const condattr_t *a)
 {
@@ -215,8 +219,7 @@ __libc_cond_catchall_stub(cond_t *c)
 
 /* read-write locks */
 
-int	__libc_rwlock_init_stub(rwlock_t *, rwlockattr_t *);
-int	__libc_rwlock_catchall_stub(rwlock_t *);
+int __libc_rwlock_catchall_stub(rwlock_t *);
 
 __weak_alias(__libc_rwlock_init,__libc_rwlock_init_stub)
 __weak_alias(__libc_rwlock_rdlock,__libc_rwlock_catchall_stub)
@@ -226,8 +229,16 @@ __weak_alias(__libc_rwlock_trywrlock,__l
 __weak_alias(__libc_rwlock_unlock,__libc_rwlock_catchall_stub)
 __weak_alias(__libc_rwlock_destroy,__libc_rwlock_catchall_stub)
 
+__strong_alias(__libc_rwlock_rdlock_stub,__libc_rwlock_catchall_stub)
+__strong_alias(__libc_rwlock_wrlock_stub,__libc_rwlock_catchall_stub)
+__strong_alias(__libc_rwlock_tryrdlock_stub,__libc_rwlock_catchall_stub)
+__strong_alias(__libc_rwlock_trywrlock_stub,__libc_rwlock_catchall_stub)
+__strong_alias(__libc_rwlock_unlock_stub,__libc_rwlock_catchall_stub)
+__strong_alias(__libc_rwlock_destroy_stub,__libc_rwlock_catchall_stub)
+
+
 int
-__libc_rwlock_init_stub(rwlock_t *l, rwlockattr_t *a)
+__libc_rwlock_init_stub(rwlock_t *l, const rwlockattr_t *a)
 {
 	/* LINTED deliberate lack of effect */
 	(void)l;
@@ -265,11 +276,6 @@ static struct {
 } __libc_tsd[TSD_KEYS_MAX];
 static int __libc_tsd_nextkey;
 
-int	__libc_thr_keycreate_stub(thread_key_t *, void (*)(void *));
-int	__libc_thr_setspecific_stub(thread_key_t, const void *);
-void	*__libc_thr_getspecific_stub(thread_key_t);
-int	__libc_thr_keydelete_stub(thread_key_t);
-
 __weak_alias(__libc_thr_keycreate,__libc_thr_keycreate_stub)
 __weak_alias(__libc_thr_setspecific,__libc_thr_setspecific_stub)
 __weak_alias(__libc_thr_getspecific,__libc_thr_getspecific_stub)
@@ -337,18 +343,6 @@ __libc_thr_keydelete_stub(thread_key_t k
 
 /* misc. */
 
-int	__libc_thr_once_stub(once_t *, void (*)(void));
-int	__libc_thr_sigsetmask_stub(int, const sigset_t *, sigset_t *);
-thr_t	__libc_thr_self_stub(void);
-int	__libc_thr_yield_stub(void);
-int	__libc_thr_create_stub(thr_t *, const thrattr_t *,
-	    void *(*)(void *), void *);
-__dead void	__libc_thr_exit_stub(void *);
-int	*__libc_thr_errno_stub(void);
-int	__libc_thr_setcancelstate_stub(int, int *);
-int	__libc_thr_equal_stub(pthread_t, pthread_t);
-unsigned int __libc_thr_curcpu_stub(void);
-
 __weak_alias(__libc_thr_once,__libc_thr_once_stub)
 __weak_alias(__libc_thr_sigsetmask,__libc_thr_sigsetmask_stub)
 __weak_alias(__libc_thr_self,__libc_thr_self_stub)

Index: src/lib/libpthread/Makefile
diff -u src/lib/libpthread/Makefile:1.73 src/lib/libpthread/Makefile:1.73.2.1
--- src/lib/libpthread/Makefile:1.73	Thu Nov 10 14:01:11 2011
+++ src/lib/libpthread/Makefile	Mon Apr 29 01:50:19 2013
@@ -1,4 +1,4 @@
-#	$NetBSD: Makefile,v 1.73 2011/11/10 14:01:11 yamt Exp $
+#	$NetBSD: Makefile,v 1.73.2.1 2013/04/29 01:50:19 riz Exp $
 #
 
 WARNS=	4
@@ -25,16 +25,15 @@ INCSDIR=/usr/include
 ARCHDIR=	${.CURDIR}/arch/${ARCHSUBDIR}
 .PATH:	${ARCHDIR}
 
-CPPFLAGS+=	-I${ARCHDIR} -I${.CURDIR} -I${.OBJDIR} -D_LIBC
-CPPFLAGS+=	-D__LIBPTHREAD_SOURCE__
+CPPFLAGS+=	-I${ARCHDIR} -I${.CURDIR} -I${.OBJDIR} -D_LIBC -D_REENTRANT
+CPPFLAGS+=	-I${.CURDIR}/../libc/include
+CPPFLAGS+=	-D__LIBPTHREAD_SOURCE__ -D__LIBC_THREAD_STUBS
 
 # XXX: This crappy poke at libc's internals needs to be fixed.
 CPPFLAGS+=-I${NETBSDSRCDIR}/sys -I${.CURDIR}/../libc
 
 LIB=	pthread
 
-LDFLAGS+=	-Wl,-znodlopen
-
 #
 # NOTE: When you create a new file for libpthread, make sure that pthread.c
 # gets a reference to a symbol in that file.  Otherwise, Unix's stupid static

Index: src/lib/libpthread/pthread.c
diff -u src/lib/libpthread/pthread.c:1.125.4.2 src/lib/libpthread/pthread.c:1.125.4.3
--- src/lib/libpthread/pthread.c:1.125.4.2	Mon Dec  3 19:07:26 2012
+++ src/lib/libpthread/pthread.c	Mon Apr 29 01:50:19 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: pthread.c,v 1.125.4.2 2012/12/03 19:07:26 jdc Exp $	*/
+/*	$NetBSD: pthread.c,v 1.125.4.3 2013/04/29 01:50:19 riz Exp $	*/
 
 /*-
  * Copyright (c) 2001, 2002, 2003, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: pthread.c,v 1.125.4.2 2012/12/03 19:07:26 jdc Exp $");
+__RCSID("$NetBSD: pthread.c,v 1.125.4.3 2013/04/29 01:50:19 riz Exp $");
 
 #define	__EXPOSE_STACK	1
 
@@ -54,6 +54,7 @@ __RCSID("$NetBSD: pthread.c,v 1.125.4.2 
 
 #include "pthread.h"
 #include "pthread_int.h"
+#include "reentrant.h"
 
 pthread_rwlock_t pthread__alltree_lock = PTHREAD_RWLOCK_INITIALIZER;
 RB_HEAD(__pthread__alltree, __pthread_st) pthread__alltree;
@@ -77,6 +78,7 @@ static void	pthread__start(void);
 void	pthread__init(void);
 
 int pthread__started;
+int __uselibcstub = 1;
 pthread_mutex_t pthread__deadqueue_lock = PTHREAD_MUTEX_INITIALIZER;
 pthread_queue_t pthread__deadqueue;
 pthread_queue_t pthread__allqueue;
@@ -161,6 +163,8 @@ pthread__init(void)
 	size_t len;
 	extern int __isthreaded;
 
+	__uselibcstub = 0;
+
 	mib[0] = CTL_HW;
 	mib[1] = HW_NCPU; 
 
@@ -328,6 +332,12 @@ pthread_create(pthread_t *thread, const 
 	void *private_area;
 	int ret;
 
+	if (__predict_false(__uselibcstub)) {
+    		pthread__errorfunc(__FILE__, __LINE__, __func__,
+		    "pthread_create() requires linking with -lpthread");
+		return __libc_thr_create_stub(thread, attr, startfunc, arg);
+	}
+
 	/*
 	 * It's okay to check this without a lock because there can
 	 * only be one thread before it becomes true.
@@ -528,6 +538,11 @@ pthread_exit(void *retval)
 	struct pt_clean_t *cleanup;
 	char *name;
 
+	if (__predict_false(__uselibcstub)) {
+		__libc_thr_exit_stub(retval);
+		goto out;
+	}
+
 	self = pthread__self();
 
 	/* Disable cancellability. */
@@ -570,6 +585,7 @@ pthread_exit(void *retval)
 		_lwp_exit();
 	}
 
+out:
 	/*NOTREACHED*/
 	pthread__abort();
 	exit(1);
@@ -647,6 +663,8 @@ pthread__reap(pthread_t thread)
 int
 pthread_equal(pthread_t t1, pthread_t t2)
 {
+	if (__predict_false(__uselibcstub))
+		return __libc_thr_equal_stub(t1, t2);
 
 	/* Nothing special here. */
 	return (t1 == t2);
@@ -746,6 +764,8 @@ pthread_setname_np(pthread_t thread, con
 pthread_t
 pthread_self(void)
 {
+	if (__predict_false(__uselibcstub))
+		return (pthread_t)__libc_thr_self_stub();
 
 	return pthread__self();
 }
@@ -776,6 +796,9 @@ pthread_setcancelstate(int state, int *o
 	pthread_t self;
 	int retval;
 
+	if (__predict_false(__uselibcstub))
+		return __libc_thr_setcancelstate_stub(state, oldstate);
+
 	self = pthread__self();
 	retval = 0;
 
@@ -948,6 +971,12 @@ pthread__errno(void)
 {
 	pthread_t self;
 
+	if (__predict_false(__uselibcstub)) {
+    		pthread__errorfunc(__FILE__, __LINE__, __func__,
+		    "pthread__errno() requires linking with -lpthread");
+		return __libc_thr_errno_stub();
+	}
+
 	self = pthread__self();
 
 	return &(self->pt_errno);

Index: src/lib/libpthread/pthread_cancelstub.c
diff -u src/lib/libpthread/pthread_cancelstub.c:1.35.6.1 src/lib/libpthread/pthread_cancelstub.c:1.35.6.2
--- src/lib/libpthread/pthread_cancelstub.c:1.35.6.1	Mon Apr  9 18:15:27 2012
+++ src/lib/libpthread/pthread_cancelstub.c	Mon Apr 29 01:50:18 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: pthread_cancelstub.c,v 1.35.6.1 2012/04/09 18:15:27 riz Exp $	*/
+/*	$NetBSD: pthread_cancelstub.c,v 1.35.6.2 2013/04/29 01:50:18 riz Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2007 The NetBSD Foundation, Inc.
@@ -33,7 +33,7 @@
 #undef _FORTIFY_SOURCE
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: pthread_cancelstub.c,v 1.35.6.1 2012/04/09 18:15:27 riz Exp $");
+__RCSID("$NetBSD: pthread_cancelstub.c,v 1.35.6.2 2013/04/29 01:50:18 riz Exp $");
 
 #ifndef lint
 
@@ -81,6 +81,7 @@ __RCSID("$NetBSD: pthread_cancelstub.c,v
 
 #include "pthread.h"
 #include "pthread_int.h"
+#include "reentrant.h"
 
 int	pthread__cancel_stub_binder;
 
@@ -128,7 +129,8 @@ int	____sigtimedwait50(const sigset_t * 
 int	__sigsuspend14(const sigset_t *);
 
 #define TESTCANCEL(id) 	do {						\
-	if (__predict_false((id)->pt_cancel))				\
+	if (__predict_true(!__uselibcstub) &&				\
+	    __predict_false((id)->pt_cancel))				\
 		pthread__cancelled();					\
 	} while (/*CONSTCOND*/0)
 

Index: src/lib/libpthread/pthread_cond.c
diff -u src/lib/libpthread/pthread_cond.c:1.56.8.2 src/lib/libpthread/pthread_cond.c:1.56.8.3
--- src/lib/libpthread/pthread_cond.c:1.56.8.2	Sat Apr 20 15:14:07 2013
+++ src/lib/libpthread/pthread_cond.c	Mon Apr 29 01:50:18 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: pthread_cond.c,v 1.56.8.2 2013/04/20 15:14:07 bouyer Exp $	*/
+/*	$NetBSD: pthread_cond.c,v 1.56.8.3 2013/04/29 01:50:18 riz Exp $	*/
 
 /*-
  * Copyright (c) 2001, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -46,7 +46,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: pthread_cond.c,v 1.56.8.2 2013/04/20 15:14:07 bouyer Exp $");
+__RCSID("$NetBSD: pthread_cond.c,v 1.56.8.3 2013/04/29 01:50:18 riz Exp $");
 
 #include <errno.h>
 #include <sys/time.h>
@@ -55,6 +55,7 @@ __RCSID("$NetBSD: pthread_cond.c,v 1.56.
 
 #include "pthread.h"
 #include "pthread_int.h"
+#include "reentrant.h"
 
 int	_sys___nanosleep50(const struct timespec *, struct timespec *);
 
@@ -84,6 +85,8 @@ pthread_cond_getclock(const pthread_cond
 int
 pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
 {
+	if (__predict_false(__uselibcstub))
+		return __libc_cond_init_stub(cond, attr);
 
 	pthread__error(EINVAL, "Invalid condition variable attribute",
 	    (attr == NULL) || (attr->ptca_magic == _PT_CONDATTR_MAGIC));
@@ -108,6 +111,8 @@ pthread_cond_init(pthread_cond_t *cond, 
 int
 pthread_cond_destroy(pthread_cond_t *cond)
 {
+	if (__predict_false(__uselibcstub))
+		return __libc_cond_destroy_stub(cond);
 
 	pthread__error(EINVAL, "Invalid condition variable",
 	    cond->ptc_magic == _PT_COND_MAGIC);
@@ -220,6 +225,8 @@ pthread_cond_timedwait(pthread_cond_t *c
 int
 pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
 {
+	if (__predict_false(__uselibcstub))
+		return __libc_cond_wait_stub(cond, mutex);
 
 	return pthread_cond_timedwait(cond, mutex, NULL);
 }
@@ -284,6 +291,9 @@ int
 pthread_cond_signal(pthread_cond_t *cond)
 {
 
+	if (__predict_false(__uselibcstub))
+		return __libc_cond_signal_stub(cond);
+
 	if (__predict_true(PTQ_EMPTY(&cond->ptc_waiters)))
 		return 0;
 	return pthread__cond_wake_one(cond);
@@ -331,6 +341,8 @@ pthread__cond_wake_all(pthread_cond_t *c
 int
 pthread_cond_broadcast(pthread_cond_t *cond)
 {
+	if (__predict_false(__uselibcstub))
+		return __libc_cond_broadcast_stub(cond);
 
 	if (__predict_true(PTQ_EMPTY(&cond->ptc_waiters)))
 		return 0;

Index: src/lib/libpthread/pthread_int.h
diff -u src/lib/libpthread/pthread_int.h:1.82.2.2 src/lib/libpthread/pthread_int.h:1.82.2.3
--- src/lib/libpthread/pthread_int.h:1.82.2.2	Sun Dec  2 18:47:36 2012
+++ src/lib/libpthread/pthread_int.h	Mon Apr 29 01:50:19 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: pthread_int.h,v 1.82.2.2 2012/12/02 18:47:36 riz Exp $	*/
+/*	$NetBSD: pthread_int.h,v 1.82.2.3 2013/04/29 01:50:19 riz Exp $	*/
 
 /*-
  * Copyright (c) 2001, 2002, 2003, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -184,6 +184,8 @@ extern int	pthread__concurrency;
 extern int 	pthread__osrev;
 extern int 	pthread__unpark_max;
 
+extern int	__uselibcstub;
+
 /* Flag to be used in a ucontext_t's uc_flags indicating that
  * the saved register state is "user" state only, not full
  * trap state.

Index: src/lib/libpthread/pthread_misc.c
diff -u src/lib/libpthread/pthread_misc.c:1.14 src/lib/libpthread/pthread_misc.c:1.14.8.1
--- src/lib/libpthread/pthread_misc.c:1.14	Wed Aug 12 23:51:23 2009
+++ src/lib/libpthread/pthread_misc.c	Mon Apr 29 01:50:18 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: pthread_misc.c,v 1.14 2009/08/12 23:51:23 enami Exp $	*/
+/*	$NetBSD: pthread_misc.c,v 1.14.8.1 2013/04/29 01:50:18 riz Exp $	*/
 
 /*-
  * Copyright (c) 2001, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: pthread_misc.c,v 1.14 2009/08/12 23:51:23 enami Exp $");
+__RCSID("$NetBSD: pthread_misc.c,v 1.14.8.1 2013/04/29 01:50:18 riz Exp $");
 
 #include <errno.h>
 #include <string.h>
@@ -46,6 +46,7 @@ __RCSID("$NetBSD: pthread_misc.c,v 1.14 
 
 #include "pthread.h"
 #include "pthread_int.h"
+#include "reentrant.h"
 
 int	pthread__sched_yield(void);
 
@@ -142,7 +143,6 @@ pthread_kill(pthread_t thread, int sig)
 int
 pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
 {
-
 	if (_sys___sigprocmask14(how, set, oset))
 		return errno;
 	return 0;
@@ -154,6 +154,9 @@ pthread__sched_yield(void)
 	pthread_t self;
 	int error;
 
+	if (__predict_false(__uselibcstub))
+		return __libc_thr_yield();
+
 	self = pthread__self();
 
 	/* Memory barrier for unlocked mutex release. */

Index: src/lib/libpthread/pthread_mutex.c
diff -u src/lib/libpthread/pthread_mutex.c:1.51 src/lib/libpthread/pthread_mutex.c:1.51.22.1
--- src/lib/libpthread/pthread_mutex.c:1.51	Sat Aug  2 19:46:30 2008
+++ src/lib/libpthread/pthread_mutex.c	Mon Apr 29 01:50:18 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: pthread_mutex.c,v 1.51 2008/08/02 19:46:30 matt Exp $	*/
+/*	$NetBSD: pthread_mutex.c,v 1.51.22.1 2013/04/29 01:50:18 riz Exp $	*/
 
 /*-
  * Copyright (c) 2001, 2003, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -47,7 +47,7 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: pthread_mutex.c,v 1.51 2008/08/02 19:46:30 matt Exp $");
+__RCSID("$NetBSD: pthread_mutex.c,v 1.51.22.1 2013/04/29 01:50:18 riz Exp $");
 
 #include <sys/types.h>
 #include <sys/lwpctl.h>
@@ -56,11 +56,13 @@ __RCSID("$NetBSD: pthread_mutex.c,v 1.51
 #include <errno.h>
 #include <limits.h>
 #include <stdlib.h>
+#include <time.h>
 #include <string.h>
 #include <stdio.h>
 
 #include "pthread.h"
 #include "pthread_int.h"
+#include "reentrant.h"
 
 #define	MUTEX_WAITERS_BIT		((uintptr_t)0x01)
 #define	MUTEX_RECURSIVE_BIT		((uintptr_t)0x02)
@@ -105,6 +107,9 @@ pthread_mutex_init(pthread_mutex_t *ptm,
 {
 	intptr_t type;
 
+	if (__predict_false(__uselibcstub))
+		return __libc_mutex_init_stub(ptm, attr);
+
 	if (attr == NULL)
 		type = PTHREAD_MUTEX_NORMAL;
 	else
@@ -137,6 +142,9 @@ int
 pthread_mutex_destroy(pthread_mutex_t *ptm)
 {
 
+	if (__predict_false(__uselibcstub))
+		return __libc_mutex_destroy_stub(ptm);
+
 	pthread__error(EINVAL, "Invalid mutex",
 	    ptm->ptm_magic == _PT_MUTEX_MAGIC);
 	pthread__error(EBUSY, "Destroying locked mutex",
@@ -152,6 +160,9 @@ pthread_mutex_lock(pthread_mutex_t *ptm)
 	pthread_t self;
 	void *val;
 
+	if (__predict_false(__uselibcstub))
+		return __libc_mutex_lock_stub(ptm);
+
 	self = pthread__self();
 	val = atomic_cas_ptr(&ptm->ptm_owner, NULL, self);
 	if (__predict_true(val == NULL)) {
@@ -335,6 +346,9 @@ pthread_mutex_trylock(pthread_mutex_t *p
 	pthread_t self;
 	void *val, *new, *next;
 
+	if (__predict_false(__uselibcstub))
+		return __libc_mutex_trylock_stub(ptm);
+
 	self = pthread__self();
 	val = atomic_cas_ptr(&ptm->ptm_owner, NULL, self);
 	if (__predict_true(val == NULL)) {
@@ -372,6 +386,9 @@ pthread_mutex_unlock(pthread_mutex_t *pt
 	pthread_t self;
 	void *value;
 
+	if (__predict_false(__uselibcstub))
+		return __libc_mutex_unlock_stub(ptm);
+
 	/*
 	 * Note this may be a non-interlocked CAS.  See lock_slow()
 	 * above and sys/kern/kern_mutex.c for details.
@@ -532,6 +549,8 @@ pthread__mutex_wakeup(pthread_t self, pt
 int
 pthread_mutexattr_init(pthread_mutexattr_t *attr)
 {
+	if (__predict_false(__uselibcstub))
+		return __libc_mutexattr_init_stub(attr);
 
 	attr->ptma_magic = _PT_MUTEXATTR_MAGIC;
 	attr->ptma_private = (void *)PTHREAD_MUTEX_DEFAULT;
@@ -541,6 +560,8 @@ pthread_mutexattr_init(pthread_mutexattr
 int
 pthread_mutexattr_destroy(pthread_mutexattr_t *attr)
 {
+	if (__predict_false(__uselibcstub))
+		return __libc_mutexattr_destroy_stub(attr);
 
 	pthread__error(EINVAL, "Invalid mutex attribute",
 	    attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
@@ -552,7 +573,6 @@ pthread_mutexattr_destroy(pthread_mutexa
 int
 pthread_mutexattr_gettype(const pthread_mutexattr_t *attr, int *typep)
 {
-
 	pthread__error(EINVAL, "Invalid mutex attribute",
 	    attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
 
@@ -564,6 +584,8 @@ pthread_mutexattr_gettype(const pthread_
 int
 pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type)
 {
+	if (__predict_false(__uselibcstub))
+		return __libc_mutexattr_settype_stub(attr, type);
 
 	pthread__error(EINVAL, "Invalid mutex attribute",
 	    attr->ptma_magic == _PT_MUTEXATTR_MAGIC);
@@ -591,6 +613,8 @@ once_cleanup(void *closure)
 int
 pthread_once(pthread_once_t *once_control, void (*routine)(void))
 {
+	if (__predict_false(__uselibcstub))
+		return __libc_thr_once_stub(once_control, routine);
 
 	if (once_control->pto_done == 0) {
 		pthread_mutex_lock(&once_control->pto_mutex);

Index: src/lib/libpthread/pthread_rwlock.c
diff -u src/lib/libpthread/pthread_rwlock.c:1.32 src/lib/libpthread/pthread_rwlock.c:1.32.22.1
--- src/lib/libpthread/pthread_rwlock.c:1.32	Sat Oct 25 14:14:11 2008
+++ src/lib/libpthread/pthread_rwlock.c	Mon Apr 29 01:50:18 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: pthread_rwlock.c,v 1.32 2008/10/25 14:14:11 yamt Exp $ */
+/*	$NetBSD: pthread_rwlock.c,v 1.32.22.1 2013/04/29 01:50:18 riz Exp $ */
 
 /*-
  * Copyright (c) 2002, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -30,16 +30,18 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: pthread_rwlock.c,v 1.32 2008/10/25 14:14:11 yamt Exp $");
+__RCSID("$NetBSD: pthread_rwlock.c,v 1.32.22.1 2013/04/29 01:50:18 riz Exp $");
 
 #include <sys/types.h>
 #include <sys/lwpctl.h>
 
+#include <time.h>
 #include <errno.h>
 #include <stddef.h>
 
 #include "pthread.h"
 #include "pthread_int.h"
+#include "reentrant.h"
 
 #define	_RW_LOCKED		0
 #define	_RW_WANT_WRITE		1
@@ -85,6 +87,8 @@ int
 pthread_rwlock_init(pthread_rwlock_t *ptr,
 	    const pthread_rwlockattr_t *attr)
 {
+	if (__predict_false(__uselibcstub))
+		return __libc_rwlock_init_stub(ptr, attr);
 
 	if (attr && (attr->ptra_magic != _PT_RWLOCKATTR_MAGIC))
 		return EINVAL;
@@ -101,6 +105,8 @@ pthread_rwlock_init(pthread_rwlock_t *pt
 int
 pthread_rwlock_destroy(pthread_rwlock_t *ptr)
 {
+	if (__predict_false(__uselibcstub))
+		return __libc_rwlock_destroy_stub(ptr);
 
 	if ((ptr->ptr_magic != _PT_RWLOCK_MAGIC) ||
 	    (!PTQ_EMPTY(&ptr->ptr_rblocked)) ||
@@ -233,6 +239,9 @@ pthread_rwlock_tryrdlock(pthread_rwlock_
 {
 	uintptr_t owner, next;
 
+	if (__predict_false(__uselibcstub))
+		return __libc_rwlock_tryrdlock_stub(ptr);
+
 #ifdef ERRORCHECK
 	if (ptr->ptr_magic != _PT_RWLOCK_MAGIC)
 		return EINVAL;
@@ -355,6 +364,9 @@ pthread_rwlock_trywrlock(pthread_rwlock_
 	uintptr_t owner, next;
 	pthread_t self;
 
+	if (__predict_false(__uselibcstub))
+		return __libc_rwlock_trywrlock_stub(ptr);
+
 #ifdef ERRORCHECK
 	if (ptr->ptr_magic != _PT_RWLOCK_MAGIC)
 		return EINVAL;
@@ -379,6 +391,8 @@ pthread_rwlock_trywrlock(pthread_rwlock_
 int
 pthread_rwlock_rdlock(pthread_rwlock_t *ptr)
 {
+	if (__predict_false(__uselibcstub))
+		return __libc_rwlock_rdlock_stub(ptr);
 
 	return pthread__rwlock_rdlock(ptr, NULL);
 }
@@ -387,7 +401,6 @@ int
 pthread_rwlock_timedrdlock(pthread_rwlock_t *ptr,
 			   const struct timespec *abs_timeout)
 {
-
 	if (abs_timeout == NULL)
 		return EINVAL;
 	if ((abs_timeout->tv_nsec >= 1000000000) ||
@@ -401,6 +414,8 @@ pthread_rwlock_timedrdlock(pthread_rwloc
 int
 pthread_rwlock_wrlock(pthread_rwlock_t *ptr)
 {
+	if (__predict_false(__uselibcstub))
+		return __libc_rwlock_wrlock_stub(ptr);
 
 	return pthread__rwlock_wrlock(ptr, NULL);
 }
@@ -409,7 +424,6 @@ int
 pthread_rwlock_timedwrlock(pthread_rwlock_t *ptr,
 			   const struct timespec *abs_timeout)
 {
-
 	if (abs_timeout == NULL)
 		return EINVAL;
 	if ((abs_timeout->tv_nsec >= 1000000000) ||
@@ -428,6 +442,9 @@ pthread_rwlock_unlock(pthread_rwlock_t *
 	pthread_mutex_t *interlock;
 	pthread_t self, thread;
 
+	if (__predict_false(__uselibcstub))
+		return __libc_rwlock_unlock_stub(ptr);
+
 #ifdef ERRORCHECK
 	if ((ptr == NULL) || (ptr->ptr_magic != _PT_RWLOCK_MAGIC))
 		return EINVAL;

Index: src/lib/libpthread/pthread_specific.c
diff -u src/lib/libpthread/pthread_specific.c:1.21.22.2 src/lib/libpthread/pthread_specific.c:1.21.22.3
--- src/lib/libpthread/pthread_specific.c:1.21.22.2	Sun Dec  2 18:47:36 2012
+++ src/lib/libpthread/pthread_specific.c	Mon Apr 29 01:50:18 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: pthread_specific.c,v 1.21.22.2 2012/12/02 18:47:36 riz Exp $	*/
+/*	$NetBSD: pthread_specific.c,v 1.21.22.3 2013/04/29 01:50:18 riz Exp $	*/
 
 /*-
  * Copyright (c) 2001, 2007 The NetBSD Foundation, Inc.
@@ -30,12 +30,13 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: pthread_specific.c,v 1.21.22.2 2012/12/02 18:47:36 riz Exp $");
+__RCSID("$NetBSD: pthread_specific.c,v 1.21.22.3 2013/04/29 01:50:18 riz Exp $");
 
 /* Functions and structures dealing with thread-specific data */
 
 #include "pthread.h"
 #include "pthread_int.h"
+#include "reentrant.h"
 
 #include <sys/lwpctl.h>
 
@@ -48,6 +49,9 @@ pthread_setspecific(pthread_key_t key, c
 {
 	pthread_t self;
 
+	if (__predict_false(__uselibcstub))
+		return __libc_thr_setspecific_stub(key, value);
+
 	self = pthread__self();
 	/*
 	 * We can't win here on constness. Having been given a 
@@ -65,6 +69,8 @@ pthread_setspecific(pthread_key_t key, c
 void *
 pthread_getspecific(pthread_key_t key)
 {
+	if (__predict_false(__uselibcstub))
+		return __libc_thr_getspecific_stub(key);
 
 	return pthread__self()->pt_specific[key];
 }
@@ -72,6 +78,8 @@ pthread_getspecific(pthread_key_t key)
 unsigned int
 pthread_curcpu_np(void)
 {
+	if (__predict_false(__uselibcstub))
+		return __libc_thr_curcpu_stub();
 
 	return pthread__self()->pt_lwpctl->lc_curcpu;
 }

Index: src/lib/libpthread/pthread_tsd.c
diff -u src/lib/libpthread/pthread_tsd.c:1.7.24.2 src/lib/libpthread/pthread_tsd.c:1.7.24.3
--- src/lib/libpthread/pthread_tsd.c:1.7.24.2	Sun Dec  2 18:47:36 2012
+++ src/lib/libpthread/pthread_tsd.c	Mon Apr 29 01:50:19 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: pthread_tsd.c,v 1.7.24.2 2012/12/02 18:47:36 riz Exp $	*/
+/*	$NetBSD: pthread_tsd.c,v 1.7.24.3 2013/04/29 01:50:19 riz Exp $	*/
 
 /*-
  * Copyright (c) 2001, 2007 The NetBSD Foundation, Inc.
@@ -30,13 +30,14 @@
  */
 
 #include <sys/cdefs.h>
-__RCSID("$NetBSD: pthread_tsd.c,v 1.7.24.2 2012/12/02 18:47:36 riz Exp $");
+__RCSID("$NetBSD: pthread_tsd.c,v 1.7.24.3 2013/04/29 01:50:19 riz Exp $");
 
 /* Functions and structures dealing with thread-specific data */
 #include <errno.h>
 
 #include "pthread.h"
 #include "pthread_int.h"
+#include "reentrant.h"
 
 static pthread_mutex_t tsd_mutex = PTHREAD_MUTEX_INITIALIZER;
 static int nextkey;
@@ -46,11 +47,16 @@ void (*pthread__tsd_destructors[PTHREAD_
 __strong_alias(__libc_thr_keycreate,pthread_key_create)
 __strong_alias(__libc_thr_keydelete,pthread_key_delete)
 
+#include <err.h>
+#include <stdlib.h>
 int
 pthread_key_create(pthread_key_t *key, void (*destructor)(void *))
 {
 	int i;
 
+	if (__predict_false(__uselibcstub))
+		return __libc_thr_keycreate_stub(key, destructor);
+
 	/* Get a lock on the allocation list */
 	pthread_mutex_lock(&tsd_mutex);
 	
@@ -90,7 +96,6 @@ pthread_key_create(pthread_key_t *key, v
 int
 pthread_key_delete(pthread_key_t key)
 {
-
 	/*
 	 * This is tricky.  The standard says of pthread_key_create()
 	 * that new keys have the value NULL associated with them in
@@ -157,6 +162,9 @@ pthread_key_delete(pthread_key_t key)
 	 * apply in general, just to this implementation.
 	 */
 
+	if (__predict_false(__uselibcstub))
+		return __libc_thr_keydelete_stub(key);
+
 	/* For the momemt, we're going with option 1. */
 	pthread_mutex_lock(&tsd_mutex);
 	pthread__tsd_destructors[key] = NULL;

Reply via email to