CVS commit: [netbsd-6] src/lib/libpthread

2014-02-20 Thread Stephen Borrill
Module Name:src
Committed By:   sborrill
Date:   Thu Feb 20 13:00:40 UTC 2014

Modified Files:
src/lib/libpthread [netbsd-6]: pthread_cond.c pthread_mutex.c

Log Message:
Pull up the following revisions(s) (requested by prlw1 in ticket #1029):
lib/libpthread/pthread_cond.c:  revision 1.62
lib/libpthread/pthread_mutex.c: revision 1.57,1.59

Partial fix for thread deadlock commonly observed with named.
Also address PR/44756.


To generate a diff of this commit:
cvs rdiff -u -r1.56.8.3 -r1.56.8.4 src/lib/libpthread/pthread_cond.c
cvs rdiff -u -r1.51.22.1 -r1.51.22.2 src/lib/libpthread/pthread_mutex.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/libpthread/pthread_cond.c
diff -u src/lib/libpthread/pthread_cond.c:1.56.8.3 src/lib/libpthread/pthread_cond.c:1.56.8.4
--- src/lib/libpthread/pthread_cond.c:1.56.8.3	Mon Apr 29 01:50:18 2013
+++ src/lib/libpthread/pthread_cond.c	Thu Feb 20 13:00:40 2014
@@ -1,4 +1,4 @@
-/*	$NetBSD: pthread_cond.c,v 1.56.8.3 2013/04/29 01:50:18 riz Exp $	*/
+/*	$NetBSD: pthread_cond.c,v 1.56.8.4 2014/02/20 13:00:40 sborrill Exp $	*/
 
 /*-
  * Copyright (c) 2001, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -46,7 +46,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: pthread_cond.c,v 1.56.8.3 2013/04/29 01:50:18 riz Exp $");
+__RCSID("$NetBSD: pthread_cond.c,v 1.56.8.4 2014/02/20 13:00:40 sborrill Exp $");
 
 #include 
 #include 
@@ -181,10 +181,12 @@ pthread_cond_timedwait(pthread_cond_t *c
 		pthread_mutex_unlock(mutex);
 		self->pt_willpark = 0;
 		self->pt_blocking++;
-		retval = _lwp_park(abstime, self->pt_unpark,
-		__UNVOLATILE(&mutex->ptm_waiters),
-		__UNVOLATILE(&mutex->ptm_waiters));
-		self->pt_unpark = 0;
+		do {
+			retval = _lwp_park(abstime, self->pt_unpark,
+			__UNVOLATILE(&mutex->ptm_waiters),
+			__UNVOLATILE(&mutex->ptm_waiters));
+			self->pt_unpark = 0;
+		} while (retval == -1 && errno == ESRCH);
 		self->pt_blocking--;
 		membar_sync();
 		pthread_mutex_lock(mutex);

Index: src/lib/libpthread/pthread_mutex.c
diff -u src/lib/libpthread/pthread_mutex.c:1.51.22.1 src/lib/libpthread/pthread_mutex.c:1.51.22.2
--- src/lib/libpthread/pthread_mutex.c:1.51.22.1	Mon Apr 29 01:50:18 2013
+++ src/lib/libpthread/pthread_mutex.c	Thu Feb 20 13:00:40 2014
@@ -1,4 +1,4 @@
-/*	$NetBSD: pthread_mutex.c,v 1.51.22.1 2013/04/29 01:50:18 riz Exp $	*/
+/*	$NetBSD: pthread_mutex.c,v 1.51.22.2 2014/02/20 13:00:40 sborrill Exp $	*/
 
 /*-
  * Copyright (c) 2001, 2003, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -47,7 +47,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: pthread_mutex.c,v 1.51.22.1 2013/04/29 01:50:18 riz Exp $");
+__RCSID("$NetBSD: pthread_mutex.c,v 1.51.22.2 2014/02/20 13:00:40 sborrill Exp $");
 
 #include 
 #include 
@@ -211,11 +211,61 @@ pthread__mutex_spin(pthread_mutex_t *ptm
 	return owner;
 }
 
+NOINLINE static void
+pthread__mutex_setwaiters(pthread_t self, pthread_mutex_t *ptm)
+{
+	void *new, *owner;
+
+	/*
+	 * Note that the mutex can become unlocked before we set
+	 * the waiters bit.  If that happens it's not safe to sleep
+	 * as we may never be awoken: we must remove the current
+	 * thread from the waiters list and try again.
+	 *
+	 * Because we are doing this atomically, we can't remove
+	 * one waiter: we must remove all waiters and awken them,
+	 * then sleep in _lwp_park() until we have been awoken. 
+	 *
+	 * Issue a memory barrier to ensure that we are reading
+	 * the value of ptm_owner/pt_mutexwait after we have entered
+	 * the waiters list (the CAS itself must be atomic).
+	 */
+again:
+	membar_consumer();
+	owner = ptm->ptm_owner;
+
+	if (MUTEX_OWNER(owner) == 0) {
+		pthread__mutex_wakeup(self, ptm);
+		return;
+	}
+	if (!MUTEX_HAS_WAITERS(owner)) {
+		new = (void *)((uintptr_t)owner | MUTEX_WAITERS_BIT);
+		if (atomic_cas_ptr(&ptm->ptm_owner, owner, new) != owner) {
+			goto again;
+		}
+	}
+
+	/*
+	 * Note that pthread_mutex_unlock() can do a non-interlocked CAS.
+	 * We cannot know if the presence of the waiters bit is stable
+	 * while the holding thread is running.  There are many assumptions;
+	 * see sys/kern/kern_mutex.c for details.  In short, we must spin if
+	 * we see that the holder is running again.
+	 */
+	membar_sync();
+	pthread__mutex_spin(ptm, owner);
+
+	if (membar_consumer(), !MUTEX_HAS_WAITERS(ptm->ptm_owner)) {
+		goto again;
+	}
+}
+
 NOINLINE static int
 pthread__mutex_lock_slow(pthread_mutex_t *ptm)
 {
 	void *waiters, *new, *owner, *next;
 	pthread_t self;
+	int serrno;
 
 	pthread__error(EINVAL, "Invalid mutex",
 	ptm->ptm_magic == _PT_MUTEX_MAGIC);
@@ -235,6 +285,7 @@ pthread__mutex_lock_slow(pthread_mutex_t
 			return EDEADLK;
 	}
 
+	serrno = errno;
 	for (;; owner = ptm->ptm_owner) {
 		/* Spin while the owner is running. */
 		owner = pthread__mutex_spin(ptm, owner);
@@ -247,6 +298,7 @@ pthread__mutex_lock_slow(pthread_mutex_t
 next = atomic_cas_ptr(&ptm->ptm_ow

CVS commit: [netbsd-6] src/lib/libpthread

2013-09-25 Thread Jeff Rizzo
Module Name:src
Committed By:   riz
Date:   Thu Sep 26 02:03:09 UTC 2013

Modified Files:
src/lib/libpthread [netbsd-6]: pthread.c

Log Message:
Pull up following revision(s) (requested by riastradh in ticket #959):
lib/libpthread/pthread.c: revision 1.137
return errno if pthread_create hits the system limit, not just -1
(this is not entirely correct because it can return ENOMEM which is
not mentioned in the spec, but there are other places in pthread_create
whete ENOMEM is returned -- it at all, this should be fixed everywhere)


To generate a diff of this commit:
cvs rdiff -u -r1.125.4.3 -r1.125.4.4 src/lib/libpthread/pthread.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/libpthread/pthread.c
diff -u src/lib/libpthread/pthread.c:1.125.4.3 src/lib/libpthread/pthread.c:1.125.4.4
--- src/lib/libpthread/pthread.c:1.125.4.3	Mon Apr 29 01:50:19 2013
+++ src/lib/libpthread/pthread.c	Thu Sep 26 02:03:09 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: pthread.c,v 1.125.4.3 2013/04/29 01:50:19 riz Exp $	*/
+/*	$NetBSD: pthread.c,v 1.125.4.4 2013/09/26 02:03:09 riz Exp $	*/
 
 /*-
  * Copyright (c) 2001, 2002, 2003, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: pthread.c,v 1.125.4.3 2013/04/29 01:50:19 riz Exp $");
+__RCSID("$NetBSD: pthread.c,v 1.125.4.4 2013/09/26 02:03:09 riz Exp $");
 
 #define	__EXPOSE_STACK	1
 
@@ -443,6 +443,7 @@ pthread_create(pthread_t *thread, const 
 		flag |= LWP_SUSPENDED;
 	ret = _lwp_create(&newthread->pt_uc, flag, &newthread->pt_lid);
 	if (ret != 0) {
+		ret = errno;
 		pthread_mutex_lock(&newthread->pt_lock);
 		/* Will unlock and free name. */
 		pthread__reap(newthread);



CVS commit: [netbsd-6] src/lib/libpthread

2013-04-28 Thread Jeff Rizzo
Module Name:src
Committed By:   riz
Date:   Mon Apr 29 02:27:29 UTC 2013

Modified Files:
src/lib/libpthread [netbsd-6]: pthread_condattr.3

Log Message:
Pull up following revision(s) (requested by enami in ticket #877):
lib/libpthread/pthread_condattr.3: revision 1.9
Fix pasto.


To generate a diff of this commit:
cvs rdiff -u -r1.7.8.1 -r1.7.8.2 src/lib/libpthread/pthread_condattr.3

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

Modified files:

Index: src/lib/libpthread/pthread_condattr.3
diff -u src/lib/libpthread/pthread_condattr.3:1.7.8.1 src/lib/libpthread/pthread_condattr.3:1.7.8.2
--- src/lib/libpthread/pthread_condattr.3:1.7.8.1	Wed Nov 28 23:47:38 2012
+++ src/lib/libpthread/pthread_condattr.3	Mon Apr 29 02:27:29 2013
@@ -1,4 +1,4 @@
-.\" $NetBSD: pthread_condattr.3,v 1.7.8.1 2012/11/28 23:47:38 riz Exp $
+.\" $NetBSD: pthread_condattr.3,v 1.7.8.2 2013/04/29 02:27:29 riz Exp $
 .\"
 .\" Copyright (c) 2002 The NetBSD Foundation, Inc.
 .\" All rights reserved.
@@ -63,7 +63,7 @@
 .Ft int
 .Fn pthread_condattr_init "pthread_condattr_t *attr"
 .Ft int
-.Fn pthread_condattr_init "pthread_condattr_t *attr" "clockid_t clock"
+.Fn pthread_condattr_setclock "pthread_condattr_t *attr" "clockid_t clock"
 .Ft int
 .Fn pthread_condattr_destroy "pthread_condattr_t *attr"
 .Sh DESCRIPTION



CVS commit: [netbsd-6] src/lib/libpthread

2013-04-20 Thread Manuel Bouyer
Module Name:src
Committed By:   bouyer
Date:   Sat Apr 20 15:14:08 UTC 2013

Modified Files:
src/lib/libpthread [netbsd-6]: pthread_cond.c

Log Message:
Pull up following revision(s) (requested by christos in ticket #862):
lib/libpthread/pthread_cond.c: revision 1.60
lib/libpthread/pthread_cond.c: revision 1.61
PR/47703: Yasushi Oshima: pthread_cond_timedwait() does not wait
after call pthread_condattr_setclock(CLOCK_MONOTONIC)
_lwp_park(2) expects a realtime clock, and it gets passed a monotonic
one.  Since monotonic < real, it never sleeps. This patch adjusts
the monotonic clock to be a real one before it passes is to
_lwp_park(2). This is the minimal hacky fix and it will be fixed
properly in _lwp_park(2) in the future.
XXX: pullup to 6.
for safety, declare mono on the outermost block it is used.


To generate a diff of this commit:
cvs rdiff -u -r1.56.8.1 -r1.56.8.2 src/lib/libpthread/pthread_cond.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/libpthread/pthread_cond.c
diff -u src/lib/libpthread/pthread_cond.c:1.56.8.1 src/lib/libpthread/pthread_cond.c:1.56.8.2
--- src/lib/libpthread/pthread_cond.c:1.56.8.1	Wed Nov 28 23:47:37 2012
+++ src/lib/libpthread/pthread_cond.c	Sat Apr 20 15:14:07 2013
@@ -1,4 +1,4 @@
-/*	$NetBSD: pthread_cond.c,v 1.56.8.1 2012/11/28 23:47:37 riz Exp $	*/
+/*	$NetBSD: pthread_cond.c,v 1.56.8.2 2013/04/20 15:14:07 bouyer Exp $	*/
 
 /*-
  * Copyright (c) 2001, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -46,7 +46,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: pthread_cond.c,v 1.56.8.1 2012/11/28 23:47:37 riz Exp $");
+__RCSID("$NetBSD: pthread_cond.c,v 1.56.8.2 2013/04/20 15:14:07 bouyer Exp $");
 
 #include 
 #include 
@@ -74,6 +74,13 @@ __strong_alias(__libc_cond_wait,pthread_
 __strong_alias(__libc_cond_timedwait,pthread_cond_timedwait)
 __strong_alias(__libc_cond_destroy,pthread_cond_destroy)
 
+static clockid_t
+pthread_cond_getclock(const pthread_cond_t *cond)
+{
+	return cond->ptc_private ? 
+	*(clockid_t *)cond->ptc_private : CLOCK_REALTIME;
+}
+
 int
 pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
 {
@@ -119,6 +126,7 @@ pthread_cond_timedwait(pthread_cond_t *c
 {
 	pthread_t self;
 	int retval;
+	struct timespec mono;
 
 	pthread__error(EINVAL, "Invalid condition variable",
 	cond->ptc_magic == _PT_COND_MAGIC);
@@ -127,6 +135,19 @@ pthread_cond_timedwait(pthread_cond_t *c
 	pthread__error(EPERM, "Mutex not locked in condition wait",
 	mutex->ptm_owner != NULL);
 	if (abstime != NULL) {
+		/*
+		 * XXX: This should be done in the kernel to avoid
+		 * extra system calls! 
+		 */
+		if (pthread_cond_getclock(cond) == CLOCK_MONOTONIC) {
+			struct timespec real;
+			if (clock_gettime(CLOCK_REALTIME, &real) == -1 ||
+			clock_gettime(CLOCK_MONOTONIC, &mono) == -1)
+return errno;
+			timespecsub(abstime, &mono, &mono);
+			timespecadd(&mono, &real, &mono);
+			abstime = &mono;
+		}
 		pthread__error(EINVAL, "Invalid wait time", 
 		(abstime->tv_sec >= 0) &&
 		(abstime->tv_nsec >= 0) &&
@@ -375,8 +396,7 @@ pthread_cond_wait_nothread(pthread_t sel
 		diff.tv_sec = ;
 		diff.tv_nsec = 0;
 	} else {
-		clockid_t clck = cond->ptc_private ?
-		*(clockid_t *)cond->ptc_private : CLOCK_REALTIME;
+		clockid_t clck = pthread_cond_getclock(cond);
 		clock_gettime(clck, &now);
 		if  (timespeccmp(abstime, &now, <))
 			timespecclear(&diff);



CVS commit: [netbsd-6] src/lib/libpthread

2012-12-03 Thread Julian Coleman
Module Name:src
Committed By:   jdc
Date:   Mon Dec  3 19:07:27 UTC 2012

Modified Files:
src/lib/libpthread [netbsd-6]: pthread.c

Log Message:
Apply patch (requested by riastradh in ticket #735) to fix the unchecked
assumption that sizeof(struct __pthread_st) <= pagesize, as observed in
PR 47271.


To generate a diff of this commit:
cvs rdiff -u -r1.125.4.1 -r1.125.4.2 src/lib/libpthread/pthread.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/libpthread/pthread.c
diff -u src/lib/libpthread/pthread.c:1.125.4.1 src/lib/libpthread/pthread.c:1.125.4.2
--- src/lib/libpthread/pthread.c:1.125.4.1	Mon May  7 03:12:33 2012
+++ src/lib/libpthread/pthread.c	Mon Dec  3 19:07:26 2012
@@ -1,4 +1,4 @@
-/*	$NetBSD: pthread.c,v 1.125.4.1 2012/05/07 03:12:33 riz Exp $	*/
+/*	$NetBSD: pthread.c,v 1.125.4.2 2012/12/03 19:07:26 jdc Exp $	*/
 
 /*-
  * Copyright (c) 2001, 2002, 2003, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: pthread.c,v 1.125.4.1 2012/05/07 03:12:33 riz Exp $");
+__RCSID("$NetBSD: pthread.c,v 1.125.4.2 2012/12/03 19:07:26 jdc Exp $");
 
 #define	__EXPOSE_STACK	1
 
@@ -1274,22 +1274,32 @@ pthread__stackid_setup(void *base, size_
 {
 	pthread_t t;
 	void *redaddr;
-	size_t pagesize;
+	size_t pagesize, bytes_needed;
 	int ret;
 
 	t = base;
 	pagesize = (size_t)sysconf(_SC_PAGESIZE);
+	bytes_needed = roundup(sizeof(*t), pagesize);
+
+	if (pagesize >= size)
+		return ENOMEM;
+	if (bytes_needed >= (size - pagesize))
+		return ENOMEM;
 
 	/*
 	 * Put a pointer to the pthread in the bottom (but
  * redzone-protected section) of the stack. 
+	 *
+	 * XXX If the stack grows up, the pthread is *not*
+	 * protected by the redzone.
 	 */
-	redaddr = STACK_SHRINK(STACK_MAX(base, size), pagesize);
-	t->pt_stack.ss_size = size - 2 * pagesize;
+	t->pt_stack.ss_size = size - bytes_needed - pagesize;
 #ifdef __MACHINE_STACK_GROWS_UP
-	t->pt_stack.ss_sp = (char *)(void *)base + pagesize;
+	redaddr = STACK_SHRINK(STACK_MAX(base, size), pagesize);
+	t->pt_stack.ss_sp = (char *)(void *)base + bytes_needed;
 #else
-	t->pt_stack.ss_sp = (char *)(void *)base + 2 * pagesize;
+	redaddr = STACK_SHRINK(STACK_MAX(base, size), bytes_needed);
+	t->pt_stack.ss_sp = (char *)(void *)base + bytes_needed + pagesize;
 #endif
 	/* Protect the next-to-bottom stack page as a red zone. */
 	ret = mprotect(redaddr, pagesize, PROT_NONE);



CVS commit: [netbsd-6] src/lib/libpthread

2012-11-28 Thread Jeff Rizzo
Module Name:src
Committed By:   riz
Date:   Wed Nov 28 23:58:36 UTC 2012

Modified Files:
src/lib/libpthread [netbsd-6]: pthread_int.h pthread_specific.c
pthread_tsd.c

Log Message:
Pull up following revision(s) (requested by christos in ticket #724):
lib/libpthread/pthread_specific.c: revision 1.24
lib/libpthread/pthread_tsd.c: revision 1.10
lib/libpthread/pthread_tsd.c: revision 1.9
lib/libpthread/pthread_int.h: revision 1.88
Replace the simple implementation of pthread_key_{create,destroy}
and pthread_{g,s}etspecific functions, to one that invalidates
values of keys in other threads when pthread_key_delete() is called.
This fixes chromium, which expects pthread_key_delete() to do
cleanup in all threads.
Don't call the destructor in pthread_key_delete() following the standard.


To generate a diff of this commit:
cvs rdiff -u -r1.82 -r1.82.2.1 src/lib/libpthread/pthread_int.h
cvs rdiff -u -r1.21 -r1.21.22.1 src/lib/libpthread/pthread_specific.c
cvs rdiff -u -r1.7 -r1.7.24.1 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/libpthread/pthread_int.h
diff -u src/lib/libpthread/pthread_int.h:1.82 src/lib/libpthread/pthread_int.h:1.82.2.1
--- src/lib/libpthread/pthread_int.h:1.82	Tue Jan 17 20:34:57 2012
+++ src/lib/libpthread/pthread_int.h	Wed Nov 28 23:58:35 2012
@@ -1,4 +1,4 @@
-/*	$NetBSD: pthread_int.h,v 1.82 2012/01/17 20:34:57 joerg Exp $	*/
+/*	$NetBSD: pthread_int.h,v 1.82.2.1 2012/11/28 23:58:35 riz Exp $	*/
 
 /*-
  * Copyright (c) 2001, 2002, 2003, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -145,7 +145,10 @@ struct	__pthread_st {
 
 	/* Thread-specific data.  Large so it sits close to the end. */
 	int		pt_havespecific;
-	void		*pt_specific[PTHREAD_KEYS_MAX];
+	struct pt_specific {
+		void *pts_value;
+		PTQ_ENTRY(pt_specific) pts_next;
+	} pt_specific[PTHREAD_KEYS_MAX];
 
 	/*
 	 * Context for thread creation.  At the end as it's cached
@@ -294,6 +297,7 @@ char	*pthread__getenv(const char *) PTHR
 __dead void	pthread__cancelled(void) PTHREAD_HIDE;
 void	pthread__mutex_deferwake(pthread_t, pthread_mutex_t *) PTHREAD_HIDE;
 int	pthread__checkpri(int) PTHREAD_HIDE;
+int	pthread__add_specific(pthread_t, pthread_key_t, const void *) PTHREAD_HIDE;
 
 #ifndef pthread__smt_pause
 #define	pthread__smt_pause()	/* nothing */

Index: src/lib/libpthread/pthread_specific.c
diff -u src/lib/libpthread/pthread_specific.c:1.21 src/lib/libpthread/pthread_specific.c:1.21.22.1
--- src/lib/libpthread/pthread_specific.c:1.21	Mon Jun 23 10:38:39 2008
+++ src/lib/libpthread/pthread_specific.c	Wed Nov 28 23:58:35 2012
@@ -1,4 +1,4 @@
-/*	$NetBSD: pthread_specific.c,v 1.21 2008/06/23 10:38:39 ad Exp $	*/
+/*	$NetBSD: pthread_specific.c,v 1.21.22.1 2012/11/28 23:58:35 riz Exp $	*/
 
 /*-
  * Copyright (c) 2001, 2007 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: pthread_specific.c,v 1.21 2008/06/23 10:38:39 ad Exp $");
+__RCSID("$NetBSD: pthread_specific.c,v 1.21.22.1 2012/11/28 23:58:35 riz Exp $");
 
 /* Functions and structures dealing with thread-specific data */
 
@@ -55,18 +55,14 @@ pthread_setspecific(pthread_key_t key, c
 	 * and return it from functions that are const void *, without
 	 * generating a warning. 
 	 */
-	/*LINTED const cast*/
-	self->pt_specific[key] = (void *) value;
-	self->pt_havespecific = 1;
-
-	return 0;
+	return pthread__add_specific(self, key, value);
 }
 
 void *
 pthread_getspecific(pthread_key_t key)
 {
 
-	return pthread__self()->pt_specific[key];
+	return pthread__self()->pt_specific[key].pts_value;
 }
 
 unsigned int

Index: src/lib/libpthread/pthread_tsd.c
diff -u src/lib/libpthread/pthread_tsd.c:1.7 src/lib/libpthread/pthread_tsd.c:1.7.24.1
--- src/lib/libpthread/pthread_tsd.c:1.7	Mon Apr 28 20:23:01 2008
+++ src/lib/libpthread/pthread_tsd.c	Wed Nov 28 23:58:35 2012
@@ -1,11 +1,11 @@
-/*	$NetBSD: pthread_tsd.c,v 1.7 2008/04/28 20:23:01 martin Exp $	*/
+/*	$NetBSD: pthread_tsd.c,v 1.7.24.1 2012/11/28 23:58:35 riz Exp $	*/
 
 /*-
  * Copyright (c) 2001, 2007 The NetBSD Foundation, Inc.
  * All rights reserved.
  *
  * This code is derived from software contributed to The NetBSD Foundation
- * by Nathan J. Williams, and by Andrew Doran.
+ * by Nathan J. Williams, by Andrew Doran, and by Christos Zoulas.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -30,7 +30,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: pthread_tsd.c,v 1.7 2008/04/28 20:23:01 martin Exp $");
+__RCSID("$NetBSD: pthread_tsd.c,v 1.7.24.1 2012/11/28 23:58:35 riz Exp $");
 
 /* Functions and structures dealing with thread-specific data */
 #include 
@@ -38,14 +38,23 @@ __RCSID("$NetBSD: pthread_tsd.c,v 1.7 20
 #include "pthread.h"
 #include "pthread_int.h"
 
+
 static pthread_mutex_t tsd_mutex = PTHR

CVS commit: [netbsd-6] src/lib/libpthread

2012-11-28 Thread Jeff Rizzo
Module Name:src
Committed By:   riz
Date:   Wed Nov 28 23:47:38 UTC 2012

Modified Files:
src/lib/libpthread [netbsd-6]: pthread.h pthread_cond.c
pthread_condattr.3

Log Message:
Pull up following revision(s) (requested by christos in ticket #722):
lib/libpthread/pthread_cond.c: revision 1.58
lib/libpthread/pthread_condattr.3: revision 1.8
lib/libpthread/pthread.h: revision 1.35
add pthread_condattr_setclock(3)


To generate a diff of this commit:
cvs rdiff -u -r1.34 -r1.34.8.1 src/lib/libpthread/pthread.h
cvs rdiff -u -r1.56 -r1.56.8.1 src/lib/libpthread/pthread_cond.c
cvs rdiff -u -r1.7 -r1.7.8.1 src/lib/libpthread/pthread_condattr.3

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

Modified files:

Index: src/lib/libpthread/pthread.h
diff -u src/lib/libpthread/pthread.h:1.34 src/lib/libpthread/pthread.h:1.34.8.1
--- src/lib/libpthread/pthread.h:1.34	Fri Aug  6 05:25:02 2010
+++ src/lib/libpthread/pthread.h	Wed Nov 28 23:47:38 2012
@@ -1,4 +1,4 @@
-/*	$NetBSD: pthread.h,v 1.34 2010/08/06 05:25:02 christos Exp $	*/
+/*	$NetBSD: pthread.h,v 1.34.8.1 2012/11/28 23:47:38 riz Exp $	*/
 
 /*-
  * Copyright (c) 2001 The NetBSD Foundation, Inc.
@@ -112,6 +112,9 @@ int	pthread_cond_timedwait(pthread_cond_
 int	pthread_cond_signal(pthread_cond_t *);
 int	pthread_cond_broadcast(pthread_cond_t *);
 int	pthread_condattr_init(pthread_condattr_t *);
+#if defined(_NETBSD_SOURCE)
+int pthread_condattr_setclock(pthread_condattr_t *, clockid_t);
+#endif
 int	pthread_condattr_destroy(pthread_condattr_t *);
 
 int	pthread_once(pthread_once_t *, void (*)(void));

Index: src/lib/libpthread/pthread_cond.c
diff -u src/lib/libpthread/pthread_cond.c:1.56 src/lib/libpthread/pthread_cond.c:1.56.8.1
--- src/lib/libpthread/pthread_cond.c:1.56	Tue Nov  2 20:49:47 2010
+++ src/lib/libpthread/pthread_cond.c	Wed Nov 28 23:47:37 2012
@@ -1,4 +1,4 @@
-/*	$NetBSD: pthread_cond.c,v 1.56 2010/11/02 20:49:47 skrll Exp $	*/
+/*	$NetBSD: pthread_cond.c,v 1.56.8.1 2012/11/28 23:47:37 riz Exp $	*/
 
 /*-
  * Copyright (c) 2001, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -46,11 +46,12 @@
  */
 
 #include 
-__RCSID("$NetBSD: pthread_cond.c,v 1.56 2010/11/02 20:49:47 skrll Exp $");
+__RCSID("$NetBSD: pthread_cond.c,v 1.56.8.1 2012/11/28 23:47:37 riz Exp $");
 
 #include 
 #include 
 #include 
+#include 
 
 #include "pthread.h"
 #include "pthread_int.h"
@@ -60,7 +61,7 @@ int	_sys___nanosleep50(const struct time
 extern int pthread__started;
 
 static int pthread_cond_wait_nothread(pthread_t, pthread_mutex_t *,
-const struct timespec *);
+pthread_cond_t *, const struct timespec *);
 
 int	_pthread_cond_has_waiters_np(pthread_cond_t *);
 
@@ -84,6 +85,14 @@ pthread_cond_init(pthread_cond_t *cond, 
 	pthread_lockinit(&cond->ptc_lock);
 	PTQ_INIT(&cond->ptc_waiters);
 	cond->ptc_mutex = NULL;
+	if (attr && attr->ptca_private) {
+		cond->ptc_private = malloc(sizeof(clockid_t));
+		if (cond->ptc_private == NULL)
+			return errno;
+		*(clockid_t *)cond->ptc_private =
+		*(clockid_t *)attr->ptca_private;
+	} else
+		cond->ptc_private = NULL;
 
 	return 0;
 }
@@ -99,6 +108,7 @@ pthread_cond_destroy(pthread_cond_t *con
 	cond->ptc_mutex == NULL);
 
 	cond->ptc_magic = _PT_COND_DEAD;
+	free(cond->ptc_private);
 
 	return 0;
 }
@@ -127,7 +137,7 @@ pthread_cond_timedwait(pthread_cond_t *c
 
 	/* Just hang out for a while if threads aren't running yet. */
 	if (__predict_false(pthread__started == 0)) {
-		return pthread_cond_wait_nothread(self, mutex, abstime);
+		return pthread_cond_wait_nothread(self, mutex, cond, abstime);
 	}
 	if (__predict_false(self->pt_cancel)) {
 		pthread__cancelled();
@@ -318,11 +328,29 @@ pthread_condattr_init(pthread_condattr_t
 {
 
 	attr->ptca_magic = _PT_CONDATTR_MAGIC;
+	attr->ptca_private = NULL;
 
 	return 0;
 }
 
 int
+pthread_condattr_setclock(pthread_condattr_t *attr, clockid_t clck)
+{
+	switch (clck) {
+	case CLOCK_MONOTONIC:
+	case CLOCK_REALTIME:
+		if (attr->ptca_private == NULL)
+			attr->ptca_private = malloc(sizeof(clockid_t));
+		if (attr->ptca_private == NULL)
+			return errno;
+		*(clockid_t *)attr->ptca_private = clck;
+		return 0;
+	default:
+		return EINVAL;
+	}
+}
+
+int
 pthread_condattr_destroy(pthread_condattr_t *attr)
 {
 
@@ -330,6 +358,7 @@ pthread_condattr_destroy(pthread_condatt
 	attr->ptca_magic == _PT_CONDATTR_MAGIC);
 
 	attr->ptca_magic = _PT_CONDATTR_DEAD;
+	free(attr->ptca_private);
 
 	return 0;
 }
@@ -337,7 +366,7 @@ pthread_condattr_destroy(pthread_condatt
 /* Utility routine to hang out for a while if threads haven't started yet. */
 static int
 pthread_cond_wait_nothread(pthread_t self, pthread_mutex_t *mutex,
-const struct timespec *abstime)
+pthread_cond_t *cond, const struct timespec *abstime)
 {
 	struct timespec now, diff;
 	int retval;
@@ -346,7 +375,9 @@ pthread_cond_wait_nothread(pthread_t sel
 		diff.tv_sec = ;
 		diff.tv_n

CVS commit: [netbsd-6] src/lib/libpthread

2012-05-06 Thread Jeff Rizzo
Module Name:src
Committed By:   riz
Date:   Mon May  7 03:12:33 UTC 2012

Modified Files:
src/lib/libpthread [netbsd-6]: pthread.c

Log Message:
Pull up following revision(s) (requested by enami in ticket #209):
lib/libpthread/pthread.c: revision 1.134
Store allocated lwpctl state in the thread actually forked,
which is the only thread lives in the child process.
The problem originally reported here:
  https://bugs.ruby-lang.org/issues/6341


To generate a diff of this commit:
cvs rdiff -u -r1.125 -r1.125.4.1 src/lib/libpthread/pthread.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/libpthread/pthread.c
diff -u src/lib/libpthread/pthread.c:1.125 src/lib/libpthread/pthread.c:1.125.4.1
--- src/lib/libpthread/pthread.c:1.125	Sun Oct  2 18:18:56 2011
+++ src/lib/libpthread/pthread.c	Mon May  7 03:12:33 2012
@@ -1,4 +1,4 @@
-/*	$NetBSD: pthread.c,v 1.125 2011/10/02 18:18:56 christos Exp $	*/
+/*	$NetBSD: pthread.c,v 1.125.4.1 2012/05/07 03:12:33 riz Exp $	*/
 
 /*-
  * Copyright (c) 2001, 2002, 2003, 2006, 2007, 2008 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include 
-__RCSID("$NetBSD: pthread.c,v 1.125 2011/10/02 18:18:56 christos Exp $");
+__RCSID("$NetBSD: pthread.c,v 1.125.4.1 2012/05/07 03:12:33 riz Exp $");
 
 #define	__EXPOSE_STACK	1
 
@@ -83,7 +83,6 @@ pthread_queue_t pthread__allqueue;
 
 static pthread_attr_t pthread_default_attr;
 static lwpctl_t pthread__dummy_lwpctl = { .lc_curcpu = LWPCTL_CPU_NONE };
-static pthread_t pthread__first;
 
 enum {
 	DIAGASSERT_ABORT =	1<<0,
@@ -231,7 +230,6 @@ pthread__init(void)
 	}
 
 	/* Tell libc that we're here and it should role-play accordingly. */
-	pthread__first = first;
 	pthread_atfork(NULL, NULL, pthread__fork_callback);
 	__isthreaded = 1;
 }
@@ -239,13 +237,12 @@ pthread__init(void)
 static void
 pthread__fork_callback(void)
 {
-	struct __pthread_st *self;
+	struct __pthread_st *self = pthread__self();
 
 	/* lwpctl state is not copied across fork. */
-	if (_lwp_ctl(LWPCTL_FEATURE_CURCPU, &pthread__first->pt_lwpctl)) {
+	if (_lwp_ctl(LWPCTL_FEATURE_CURCPU, &self->pt_lwpctl)) {
 		err(1, "_lwp_ctl");
 	}
-	self = pthread__self();
 	self->pt_lid = _lwp_self();
 }
 



CVS commit: [netbsd-6] src/lib/libpthread

2012-04-09 Thread Jeff Rizzo
Module Name:src
Committed By:   riz
Date:   Mon Apr  9 18:15:28 UTC 2012

Modified Files:
src/lib/libpthread [netbsd-6]: pthread_cancelstub.c

Log Message:
Pull up following revision(s) (requested by agc in ticket #174):
lib/libpthread/pthread_cancelstub.c: revision 1.36
Add a pthread cancel stub for sigwait, following Onno van der Linden's
analysis in PR 45131.  Kindly tested by Hisashi T Fujinaka (using csup
as the test case) with a successful outcome.
OK martin@


To generate a diff of this commit:
cvs rdiff -u -r1.35 -r1.35.6.1 src/lib/libpthread/pthread_cancelstub.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/libpthread/pthread_cancelstub.c
diff -u src/lib/libpthread/pthread_cancelstub.c:1.35 src/lib/libpthread/pthread_cancelstub.c:1.35.6.1
--- src/lib/libpthread/pthread_cancelstub.c:1.35	Fri Apr 22 14:18:34 2011
+++ src/lib/libpthread/pthread_cancelstub.c	Mon Apr  9 18:15:27 2012
@@ -1,4 +1,4 @@
-/*	$NetBSD: pthread_cancelstub.c,v 1.35 2011/04/22 14:18:34 joerg Exp $	*/
+/*	$NetBSD: pthread_cancelstub.c,v 1.35.6.1 2012/04/09 18:15:27 riz Exp $	*/
 
 /*-
  * Copyright (c) 2002, 2007 The NetBSD Foundation, Inc.
@@ -33,7 +33,7 @@
 #undef _FORTIFY_SOURCE
 
 #include 
-__RCSID("$NetBSD: pthread_cancelstub.c,v 1.35 2011/04/22 14:18:34 joerg Exp $");
+__RCSID("$NetBSD: pthread_cancelstub.c,v 1.35.6.1 2012/04/09 18:15:27 riz Exp $");
 
 #ifndef lint
 
@@ -58,6 +58,7 @@ __RCSID("$NetBSD: pthread_cancelstub.c,v
 #include 
 #include 
 #include 
+#include 
 #include 
 #include 
 #include 
@@ -577,6 +578,7 @@ __sigtimedwait50(const sigset_t * __rest
 	pthread_t self;
 	int retval;
 	struct timespec tout, *tp;
+
 	if (timeout) {
 		tout = *timeout;
 		tp = &tout;
@@ -591,6 +593,28 @@ __sigtimedwait50(const sigset_t * __rest
 	return retval;
 }
 
+int  
+sigwait(const sigset_t * __restrict set, int * __restrict sig)
+{
+	pthread_t	self;
+	int		saved_errno;
+	int		new_errno;
+	int		retval;
+
+	self = pthread__self();
+	saved_errno = errno;
+	TESTCANCEL(self);
+	retval = sigtimedwait50(set, NULL, NULL);
+	TESTCANCEL(self);
+	new_errno = errno;
+	errno = saved_errno;
+	if (retval < 0) {
+		return new_errno;
+	}
+	*sig = retval;
+	return 0;
+}
+
 __strong_alias(_close, close)
 __strong_alias(_fcntl, fcntl)
 __strong_alias(_fdatasync, fdatasync)
@@ -608,6 +632,7 @@ __strong_alias(_pread, pread)
 __strong_alias(_pwrite, pwrite)
 __strong_alias(_read, read)
 __strong_alias(_readv, readv)
+__strong_alias(_sigwait, sigwait)
 __strong_alias(_write, write)
 __strong_alias(_writev, writev)