From 475a245e08bd55854e56e3d1183e8bf3245cfc90 Mon Sep 17 00:00:00 2001 From: Otto Moerbeek Date: Thu, 5 Jan 2006 22:17:18 +0000 Subject: In pthread_join(), check if we create a deadlock trying to join with ourself and only free thread after a succesful join. ok marc@ --- lib/librthread/rthread.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'lib/librthread/rthread.c') diff --git a/lib/librthread/rthread.c b/lib/librthread/rthread.c index 992c8a830de..3de0b1d79dd 100644 --- a/lib/librthread/rthread.c +++ b/lib/librthread/rthread.c @@ -1,4 +1,4 @@ -/* $OpenBSD: rthread.c,v 1.30 2006/01/05 04:24:30 tedu Exp $ */ +/* $OpenBSD: rthread.c,v 1.31 2006/01/05 22:17:17 otto Exp $ */ /* * Copyright (c) 2004,2005 Ted Unangst * All Rights Reserved. @@ -204,19 +204,21 @@ pthread_join(pthread_t thread, void **retval) { int e; - if (thread->flags & THREAD_DETACHED) + if (thread->tid == getthrid()) + e = EDEADLK; + else if (thread->flags & THREAD_DETACHED) e = EINVAL; else { _sem_wait(&thread->donesem, 0, 0); if (retval) *retval = thread->retval; e = 0; + /* We should be the last having a ref to this thread, but + * someone stupid or evil might haved detached it; + * in that case the thread will cleanup itself */ + if ((thread->flags & THREAD_DETACHED) == 0) + _rthread_free(thread); } - /* We should be the last having a ref to this thread, but - * someone stupid or evil might haved detached it; - * in that case the thread will cleanup itself */ - if ((thread->flags & THREAD_DETACHED) == 0) - _rthread_free(thread); _rthread_reaper(); return (e); -- cgit v1.2.3