mailing list of musl libc
 help / color / mirror / code / Atom feed
* [PATCH] thread: do not attempt to join detached threads in pthread_join()
@ 2017-08-01 22:46 William Pitcock
  2017-08-01 22:56 ` Rich Felker
  0 siblings, 1 reply; 3+ messages in thread
From: William Pitcock @ 2017-08-01 22:46 UTC (permalink / raw)
  To: musl; +Cc: William Pitcock

A thread which is detached releases it's resources and TCB upon thread termination.
Therefore a thread which is detached is not joinable as any underlying futex will not
be incremented, resulting in a deadlock.  Accordingly, we return EINVAL instead of
attempting to wait on a detached thread.

Other pthread implementations such as glibc NPTL and FreeBSD also reject attempts
to join detached threads with EINVAL.
---
 src/thread/pthread_join.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/src/thread/pthread_join.c b/src/thread/pthread_join.c
index 52111489..7c4bde23 100644
--- a/src/thread/pthread_join.c
+++ b/src/thread/pthread_join.c
@@ -11,6 +11,7 @@ int __pthread_timedjoin_np(pthread_t t, void **res, const struct timespec *at)
 	__pthread_testcancel();
 	__pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
 	if (cs == PTHREAD_CANCEL_ENABLE) __pthread_setcancelstate(cs, 0);
+	if (t->detached) r = EINVAL;
 	while ((tmp = t->tid) && r != ETIMEDOUT && r != EINVAL)
 		r = __timedwait_cp(&t->tid, tmp, CLOCK_REALTIME, at, 0);
 	__pthread_setcancelstate(cs, 0);
-- 
2.13.3



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] thread: do not attempt to join detached threads in pthread_join()
  2017-08-01 22:46 [PATCH] thread: do not attempt to join detached threads in pthread_join() William Pitcock
@ 2017-08-01 22:56 ` Rich Felker
  0 siblings, 0 replies; 3+ messages in thread
From: Rich Felker @ 2017-08-01 22:56 UTC (permalink / raw)
  To: musl

On Tue, Aug 01, 2017 at 10:46:13PM +0000, William Pitcock wrote:
> A thread which is detached releases it's resources and TCB upon thread termination.
> Therefore a thread which is detached is not joinable as any underlying futex will not
> be incremented, resulting in a deadlock.  Accordingly, we return EINVAL instead of
> attempting to wait on a detached thread.
> 
> Other pthread implementations such as glibc NPTL and FreeBSD also reject attempts
> to join detached threads with EINVAL.

Since the behavior is undefined and *can't be defined* in the general
case (where the detached thread may already have exited and its id may
even have been reused for a new thread), if any change is to be made,
the function should be made to crash in this case. Returning EINVAL
hides the (serious) programming error.

Rich


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH] thread: do not attempt to join detached threads in pthread_join()
@ 2017-08-01 23:12 William Pitcock
  0 siblings, 0 replies; 3+ messages in thread
From: William Pitcock @ 2017-08-01 23:12 UTC (permalink / raw)
  To: musl; +Cc: William Pitcock

A thread which is detached releases it's resources and TCB upon thread termination.
Therefore a thread which is detached is not joinable as any underlying futex will not
be incremented, resulting in a deadlock.  Accordingly, POSIX defines calling
pthread_join() on a detached thread as undefined behaviour.

We attempt, where possible, to detect attempts to join detached threads and crash
instead, to bring the undefined behaviour to the programmer's attention.
---
 src/thread/pthread_join.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/src/thread/pthread_join.c b/src/thread/pthread_join.c
index 52111489..b7175c09 100644
--- a/src/thread/pthread_join.c
+++ b/src/thread/pthread_join.c
@@ -11,6 +11,7 @@ int __pthread_timedjoin_np(pthread_t t, void **res, const struct timespec *at)
 	__pthread_testcancel();
 	__pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
 	if (cs == PTHREAD_CANCEL_ENABLE) __pthread_setcancelstate(cs, 0);
+	if (t->detached) a_crash();
 	while ((tmp = t->tid) && r != ETIMEDOUT && r != EINVAL)
 		r = __timedwait_cp(&t->tid, tmp, CLOCK_REALTIME, at, 0);
 	__pthread_setcancelstate(cs, 0);
-- 
2.13.3



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2017-08-01 23:12 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-08-01 22:46 [PATCH] thread: do not attempt to join detached threads in pthread_join() William Pitcock
2017-08-01 22:56 ` Rich Felker
2017-08-01 23:12 William Pitcock

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).