Module Name: src Committed By: joerg Date: Tue Apr 14 23:35:07 UTC 2020
Modified Files: src/lib/libpthread: pthread.c pthread_cond.c Log Message: Drop most of the logic associated with pthread__started. The pthread_cond logic is a questionable optimisation at best and the post-fork logic is plainly broken. To generate a diff of this commit: cvs rdiff -u -r1.167 -r1.168 src/lib/libpthread/pthread.c cvs rdiff -u -r1.67 -r1.68 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.c diff -u src/lib/libpthread/pthread.c:1.167 src/lib/libpthread/pthread.c:1.168 --- src/lib/libpthread/pthread.c:1.167 Sun Feb 16 17:45:11 2020 +++ src/lib/libpthread/pthread.c Tue Apr 14 23:35:07 2020 @@ -1,4 +1,4 @@ -/* $NetBSD: pthread.c,v 1.167 2020/02/16 17:45:11 kamil Exp $ */ +/* $NetBSD: pthread.c,v 1.168 2020/04/14 23:35:07 joerg Exp $ */ /*- * Copyright (c) 2001, 2002, 2003, 2006, 2007, 2008, 2020 @@ -31,7 +31,7 @@ */ #include <sys/cdefs.h> -__RCSID("$NetBSD: pthread.c,v 1.167 2020/02/16 17:45:11 kamil Exp $"); +__RCSID("$NetBSD: pthread.c,v 1.168 2020/04/14 23:35:07 joerg Exp $"); #define __EXPOSE_STACK 1 @@ -84,8 +84,6 @@ static void pthread__scrubthread(pthread static void pthread__initmain(pthread_t *); static void pthread__fork_callback(void); static void pthread__reap(pthread_t); -static void pthread__child_callback(void); -static void pthread__start(void); void pthread__init(void); @@ -274,36 +272,6 @@ pthread__fork_callback(void) self->pt_lid = _lwp_self(); } -static void -pthread__child_callback(void) -{ - - /* - * Clean up data structures that a forked child process might - * trip over. Note that if threads have been created (causing - * this handler to be registered) the standards say that the - * child will trigger undefined behavior if it makes any - * pthread_* calls (or any other calls that aren't - * async-signal-safe), so we don't really have to clean up - * much. Anything that permits some pthread_* calls to work is - * merely being polite. - */ - pthread__started = 0; -} - -static void -pthread__start(void) -{ - - /* - * Per-process timers are cleared by fork(); despite the - * various restrictions on fork() and threads, it's legal to - * fork() before creating any threads. - */ - pthread_atfork(NULL, NULL, pthread__child_callback); -} - - /* General-purpose thread data structure sanitization. */ /* ARGSUSED */ static void @@ -424,15 +392,6 @@ pthread_create(pthread_t *thread, const 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. - */ - if (pthread__started == 0) { - pthread__start(); - pthread__started = 1; - } - if (attr == NULL) nattr = pthread_default_attr; else if (attr->pta_magic == PT_ATTR_MAGIC) @@ -440,6 +399,8 @@ pthread_create(pthread_t *thread, const else return EINVAL; + pthread__started = 1; + /* Fetch misc. attributes from the attr structure. */ name = NULL; if ((p = nattr.pta_private) != NULL) Index: src/lib/libpthread/pthread_cond.c diff -u src/lib/libpthread/pthread_cond.c:1.67 src/lib/libpthread/pthread_cond.c:1.68 --- src/lib/libpthread/pthread_cond.c:1.67 Wed Jan 29 15:07:46 2020 +++ src/lib/libpthread/pthread_cond.c Tue Apr 14 23:35:07 2020 @@ -1,4 +1,4 @@ -/* $NetBSD: pthread_cond.c,v 1.67 2020/01/29 15:07:46 kamil Exp $ */ +/* $NetBSD: pthread_cond.c,v 1.68 2020/04/14 23:35:07 joerg 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.67 2020/01/29 15:07:46 kamil Exp $"); +__RCSID("$NetBSD: pthread_cond.c,v 1.68 2020/04/14 23:35:07 joerg Exp $"); #include <stdlib.h> #include <errno.h> @@ -59,11 +59,6 @@ __RCSID("$NetBSD: pthread_cond.c,v 1.67 int _sys___nanosleep50(const struct timespec *, struct timespec *); -extern int pthread__started; - -static int pthread_cond_wait_nothread(pthread_t, pthread_mutex_t *, - pthread_cond_t *, const struct timespec *); - int _pthread_cond_has_waiters_np(pthread_cond_t *); __weak_alias(pthread_cond_has_waiters_np,_pthread_cond_has_waiters_np) @@ -149,10 +144,6 @@ pthread_cond_timedwait(pthread_cond_t *c self = pthread__self(); - /* 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, cond, abstime); - } if (__predict_false(self->pt_cancel)) { pthread__cancelled(); } @@ -431,38 +422,3 @@ pthread_condattr_setpshared(pthread_cond return EINVAL; } #endif - -/* 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, - pthread_cond_t *cond, const struct timespec *abstime) -{ - struct timespec now, diff; - int retval; - - if (abstime == NULL) { - diff.tv_sec = 99999999; - diff.tv_nsec = 0; - } else { - clockid_t clck = pthread_cond_getclock(cond); - clock_gettime(clck, &now); - if (timespeccmp(abstime, &now, <)) - timespecclear(&diff); - else - timespecsub(abstime, &now, &diff); - } - - do { - pthread__testcancel(self); - pthread_mutex_unlock(mutex); - retval = _sys___nanosleep50(&diff, NULL); - pthread_mutex_lock(mutex); - } while (abstime == NULL && retval == 0); - pthread__testcancel(self); - - if (retval == 0) - return ETIMEDOUT; - else - /* spurious wakeup */ - return 0; -}