From owner-svn-src-stable@freebsd.org Sat Feb 29 21:42:37 2020 Return-Path: Delivered-To: svn-src-stable@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 2AE13241DE7; Sat, 29 Feb 2020 21:42:37 +0000 (UTC) (envelope-from kib@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) server-signature RSA-PSS (4096 bits) client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 48VKd82PVKz3Kp0; Sat, 29 Feb 2020 21:42:36 +0000 (UTC) (envelope-from kib@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 2B97121320; Sat, 29 Feb 2020 21:42:36 +0000 (UTC) (envelope-from kib@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 01TLgZZg060852; Sat, 29 Feb 2020 21:42:35 GMT (envelope-from kib@FreeBSD.org) Received: (from kib@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 01TLgZKs060847; Sat, 29 Feb 2020 21:42:35 GMT (envelope-from kib@FreeBSD.org) Message-Id: <202002292142.01TLgZKs060847@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: kib set sender to kib@FreeBSD.org using -f From: Konstantin Belousov Date: Sat, 29 Feb 2020 21:42:35 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-12@freebsd.org Subject: svn commit: r358473 - in stable/12: include lib/libthr lib/libthr/thread share/man/man3 X-SVN-Group: stable-12 X-SVN-Commit-Author: kib X-SVN-Commit-Paths: in stable/12: include lib/libthr lib/libthr/thread share/man/man3 X-SVN-Commit-Revision: 358473 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-stable@freebsd.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: SVN commit messages for all the -stable branches of the src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 29 Feb 2020 21:42:37 -0000 Author: kib Date: Sat Feb 29 21:42:34 2020 New Revision: 358473 URL: https://svnweb.freebsd.org/changeset/base/358473 Log: MFC r357985: Add pthread_peekjoin_np(3). Modified: stable/12/include/pthread_np.h stable/12/lib/libthr/pthread.map stable/12/lib/libthr/thread/thr_join.c stable/12/share/man/man3/Makefile stable/12/share/man/man3/pthread_join.3 Directory Properties: stable/12/ (props changed) Modified: stable/12/include/pthread_np.h ============================================================================== --- stable/12/include/pthread_np.h Sat Feb 29 19:59:21 2020 (r358472) +++ stable/12/include/pthread_np.h Sat Feb 29 21:42:34 2020 (r358473) @@ -63,6 +63,7 @@ int pthread_mutex_setyieldloops_np(pthread_mutex_t *mu int pthread_mutex_isowned_np(pthread_mutex_t *mutex); void pthread_resume_all_np(void); int pthread_resume_np(pthread_t); +int pthread_peekjoin_np(pthread_t, void **); void pthread_set_name_np(pthread_t, const char *); int pthread_setaffinity_np(pthread_t, size_t, const cpuset_t *); int pthread_single_np(void); Modified: stable/12/lib/libthr/pthread.map ============================================================================== --- stable/12/lib/libthr/pthread.map Sat Feb 29 19:59:21 2020 (r358472) +++ stable/12/lib/libthr/pthread.map Sat Feb 29 21:42:34 2020 (r358473) @@ -326,3 +326,7 @@ FBSD_1.4 { FBSD_1.5 { pthread_get_name_np; }; + +FBSD_1.6 { + pthread_peekjoin_np; +}; Modified: stable/12/lib/libthr/thread/thr_join.c ============================================================================== --- stable/12/lib/libthr/thread/thr_join.c Sat Feb 29 19:59:21 2020 (r358472) +++ stable/12/lib/libthr/thread/thr_join.c Sat Feb 29 21:42:34 2020 (r358473) @@ -36,13 +36,15 @@ __FBSDID("$FreeBSD$"); #include "thr_private.h" +int _pthread_peekjoin_np(pthread_t pthread, void **thread_return); int _pthread_timedjoin_np(pthread_t pthread, void **thread_return, - const struct timespec *abstime); -static int join_common(pthread_t, void **, const struct timespec *); + const struct timespec *abstime); +static int join_common(pthread_t, void **, const struct timespec *, bool peek); __weak_reference(_thr_join, pthread_join); __weak_reference(_thr_join, _pthread_join); __weak_reference(_pthread_timedjoin_np, pthread_timedjoin_np); +__weak_reference(_pthread_peekjoin_np, pthread_peekjoin_np); static void backout_join(void *arg) { @@ -57,7 +59,7 @@ static void backout_join(void *arg) int _thr_join(pthread_t pthread, void **thread_return) { - return (join_common(pthread, thread_return, NULL)); + return (join_common(pthread, thread_return, NULL, false)); } int @@ -68,22 +70,28 @@ _pthread_timedjoin_np(pthread_t pthread, void **thread abstime->tv_nsec >= 1000000000) return (EINVAL); - return (join_common(pthread, thread_return, abstime)); + return (join_common(pthread, thread_return, abstime, false)); } +int +_pthread_peekjoin_np(pthread_t pthread, void **thread_return) +{ + return (join_common(pthread, thread_return, NULL, true)); +} + /* * Cancellation behavior: * if the thread is canceled, joinee is not recycled. */ static int join_common(pthread_t pthread, void **thread_return, - const struct timespec *abstime) + const struct timespec *abstime, bool peek) { struct pthread *curthread = _get_curthread(); struct timespec ts, ts2, *tsp; void *tmp; long tid; - int ret = 0; + int ret; if (pthread == NULL) return (EINVAL); @@ -100,10 +108,21 @@ join_common(pthread_t pthread, void **thread_return, /* Multiple joiners are not supported. */ ret = ENOTSUP; } - if (ret) { + if (ret != 0) { THR_THREAD_UNLOCK(curthread, pthread); return (ret); } + + /* Only peek into status, do not gc the thread. */ + if (peek) { + if (pthread->tid != TID_TERMINATED) + ret = EBUSY; + else if (thread_return != NULL) + *thread_return = pthread->ret; + THR_THREAD_UNLOCK(curthread, pthread); + return (ret); + } + /* Set the running thread to be the joiner: */ pthread->joiner = curthread; Modified: stable/12/share/man/man3/Makefile ============================================================================== --- stable/12/share/man/man3/Makefile Sat Feb 29 19:59:21 2020 (r358472) +++ stable/12/share/man/man3/Makefile Sat Feb 29 21:42:34 2020 (r358473) @@ -368,7 +368,8 @@ PTHREAD_MLINKS+=pthread_spin_init.3 pthread_spin_destr PTHREAD_MLINKS+=pthread_switch_add_np.3 pthread_switch_delete_np.3 PTHREAD_MLINKS+=pthread_testcancel.3 pthread_setcancelstate.3 \ pthread_testcancel.3 pthread_setcanceltype.3 -PTHREAD_MLINKS+=pthread_join.3 pthread_timedjoin_np.3 +PTHREAD_MLINKS+=pthread_join.3 pthread_peekjoin_np.3 \ + pthread_join.3 pthread_timedjoin_np.3 .endif .include Modified: stable/12/share/man/man3/pthread_join.3 ============================================================================== --- stable/12/share/man/man3/pthread_join.3 Sat Feb 29 19:59:21 2020 (r358472) +++ stable/12/share/man/man3/pthread_join.3 Sat Feb 29 21:42:34 2020 (r358473) @@ -30,13 +30,14 @@ .\" .\" $FreeBSD$ .\" -.Dd February 3, 2018 +.Dd February 13, 2019 .Dt PTHREAD_JOIN 3 .Os .Sh NAME .Nm pthread_join , +.Nm pthread_peekjoin_np , .Nm pthread_timedjoin_np -.Nd wait for thread termination +.Nd inspect thread termination state .Sh LIBRARY .Lb libpthread .Sh SYNOPSIS @@ -45,7 +46,16 @@ .Fn pthread_join "pthread_t thread" "void **value_ptr" .In pthread_np.h .Ft int -.Fn pthread_timedjoin_np "pthread_t thread" "void **value_ptr" "const struct timespec *abstime" +.Fo pthread_peekjoin_np +.Fa "pthread_t thread" +.Fa "void **value_ptr" +.Fc +.Ft int +.Fo pthread_timedjoin_np +.Fa "pthread_t thread" +.Fa "void **value_ptr" +.Fa "const struct timespec *abstime" +.Fc .Sh DESCRIPTION The .Fn pthread_join @@ -82,19 +92,30 @@ function except it will return .Er ETIMEDOUT if target thread does not exit before specified absolute time passes. .Pp +The +.Fn pthread_peekjoin_np +only peeks into the exit status of the specified thread. +If the thread has not exited, the +.Er EBUSY +error is returned. +Otherwise, zero is returned and the thread exit value is optionally stored +into the location of +.Fa *value_ptr . +The target thread is left unjoined and can be used as an argument for +the +.Fn pthread_join +family of functions again. +.Pp A thread that has exited but remains unjoined counts against [_POSIX_THREAD_THREADS_MAX]. .Sh RETURN VALUES -If successful, the -.Fn pthread_join -and -.Fn pthread_timedjoin_np -functions will return zero. -Otherwise an error number will be returned to -indicate the error. +If successful, the described functions return zero. +Otherwise an error number is returned to indicate the error or +special condition. .Sh ERRORS The -.Fn pthread_join +.Fn pthread_join , +.Fn pthread_peekjoin_np , and .Fn pthread_timedjoin_np functions will fail if: @@ -125,6 +146,14 @@ The specified absolute time passed while .Fn pthread_timedjoin_np waited for thread exit. .El +.Pp +The +.Fn pthread_peekjoin_np +function will also fail if: +.Bl -tag -width Er +.It Bq Er EBUSY +The specified thread has not yet exited. +.El .Sh SEE ALSO .Xr wait 2 , .Xr pthread_create 3 @@ -139,3 +168,7 @@ function is a .Fx extension which first appeared in .Fx 6.1 . +Another extension, the +.Fn pthread_peekjoin_np +function, first appearead in +.Fx 13.0 .