From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 30926 invoked by alias); 2 Sep 2004 21:50:56 -0000 Mailing-List: contact libc-hacker-help@sources.redhat.com; run by ezmlm Precedence: bulk List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-hacker-owner@sources.redhat.com Received: (qmail 30908 invoked from network); 2 Sep 2004 21:50:55 -0000 Received: from unknown (HELO sunsite.ms.mff.cuni.cz) (195.113.15.26) by sourceware.org with SMTP; 2 Sep 2004 21:50:55 -0000 Received: from sunsite.ms.mff.cuni.cz (sunsite.mff.cuni.cz [127.0.0.1]) by sunsite.ms.mff.cuni.cz (8.12.8/8.12.8) with ESMTP id i82JX03j018693; Thu, 2 Sep 2004 21:33:00 +0200 Received: (from jakub@localhost) by sunsite.ms.mff.cuni.cz (8.12.8/8.12.8/Submit) id i82JWxeU018691; Thu, 2 Sep 2004 21:32:59 +0200 Date: Thu, 02 Sep 2004 21:50:00 -0000 From: Jakub Jelinek To: Ulrich Drepper Cc: Glibc hackers Subject: [PATCH] Avoid pthread_cond_destroy blocking for too long Message-ID: <20040902193259.GN30497@sunsite.ms.mff.cuni.cz> Reply-To: Jakub Jelinek Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.4.1i X-SW-Source: 2004-09/txt/msg00011.txt.bz2 Hi! This patch adds a mutex wakeup if there are still waiters at pthread_cond_destroy time. The reason is to make sure pthread_cond_destroy won't block for too long. If some threads are blocked on the pthread_mutex_t's __lock, it is under application control when (if ever) they will be woken up and pthread_cond_destroy would block for that whole time. By waking all mutex waiters we are just waiting until the scheduler gives all threads enough timeslice to acquire the condvar internal lock, hop through the short critical section and release the lock (last thread after waking up pthread_cond_destroy). In most programs nwaiters will be < (1 << COND_CLOCK_BITS) at pthread_cond_destroy time and thus this patch shouldn't cause performance regressions. 2004-09-02 Jakub Jelinek * pthread_cond_destroy.c (__pthread_cond_destroy): If there are waiters, awake all waiters on the associated mutex. --- libc/nptl/pthread_cond_destroy.c.jj 2004-09-02 22:27:56.000000000 +0200 +++ libc/nptl/pthread_cond_destroy.c 2004-09-02 23:33:43.278763736 +0200 @@ -44,15 +44,35 @@ __pthread_cond_destroy (cond) broadcasted, but still are using the pthread_cond_t structure, pthread_cond_destroy needs to wait for them. */ unsigned int nwaiters = cond->__data.__nwaiters; - while (nwaiters >= (1 << COND_CLOCK_BITS)) + + if (nwaiters >= (1 << COND_CLOCK_BITS)) { - lll_mutex_unlock (cond->__data.__lock); + /* Wake everybody on the associated mutex in case there are + threads that have been requeued to it. + Without this, pthread_cond_destroy could block potentially + for a long time or forever, as it would depend on other + thread's using the mutex. + When all threads waiting on the mutex are woken up, pthread_cond_wait + only waits for threads to acquire and release the internal + condvar lock. */ + if (cond->__data.__mutex != NULL + && cond->__data.__mutex != (void *) ~0l) + { + pthread_mutex_t *mut = (pthread_mutex_t *) cond->__data.__mutex; + lll_futex_wake (&mut->__data.__lock, INT_MAX); + } + + do + { + lll_mutex_unlock (cond->__data.__lock); - lll_futex_wait (&cond->__data.__nwaiters, nwaiters); + lll_futex_wait (&cond->__data.__nwaiters, nwaiters); - lll_mutex_lock (cond->__data.__lock); + lll_mutex_lock (cond->__data.__lock); - nwaiters = cond->__data.__nwaiters; + nwaiters = cond->__data.__nwaiters; + } + while (nwaiters >= (1 << COND_CLOCK_BITS)); } return 0; Jakub