From: Michael Johnson <michaelj@maine.rr.com>
To: pthreads-win32 discussion list <pthreads-win32@sources.redhat.com>
Subject: Deadlock interaction between pthread_cond_check_need_init.c and pthread_cond_destroy.c
Date: Thu, 19 Sep 2002 09:17:00 -0000 [thread overview]
Message-ID: <3D89F90A.2030805@maine.rr.com> (raw)
When two different threads exist, and one is attempting to destroy a
condition variable while the other is attempting to initialize a
condition variable that was created with PTHREAD_COND_INITIALIZER, a
deadlock can occur:
Thread A Thread B
Enters ptw32_cond_check_need_init
Enters pthread_cond_destroy
EnterCriticalSection(&ptw32_cond_test_init_lock)
(now holds ptw32_cond_test_init_lock)
EnterCriticalSection(&ptw32_cond_list_lock)
Enters pthread_cond_init
Determines that condvar is static initialized
EnterCriticalSection(&ptw32_cond_list_lock)
EnterCriticalSection(&ptw32_cond_test_init_lock)
(now waiting to enter ptw32_cond_list_lock)
(now waiting to enter ptw32_cond_test_init_lock)
deadlocked
It appears from reading the code and from a patch that I made that the
following change to pthread_cond_destroy appears to fix this:
|
|
|{|
| pthread_cond_t cv;|
| int result = 0, result1 = 0, result2 = 0;|
||
| /*|
| * Assuming any race condition here is harmless.|
| */|
| if (cond == NULL|
| || *cond == NULL)|
| {|
| return EINVAL;|
| }|
||
| if (*cond != PTHREAD_COND_INITIALIZER)|
| {|
| EnterCriticalSection(&ptw32_cond_list_lock);|
||
| cv = *cond;|
||
| /*|
| * Close the gate; this will synchronize this thread with|
| * all already signaled waiters to let them retract their|
| * waiter status - SEE NOTE 1 ABOVE!!!|
| */|
| if (sem_wait(&(cv->semBlockLock)) != 0)|
| {|
| return errno;|
| }|
||
| /*|
| * !TRY! lock mtxUnblockLock; try will detect busy condition|
| * and will not cause a deadlock with respect to concurrent|
| * signal/broadcast.|
| */|
| if ((result = pthread_mutex_trylock(&(cv->mtxUnblockLock)))
!= 0)|
| {|
| (void) sem_post(&(cv->semBlockLock));|
| return result;|
| }|
||
| /*|
| * Check whether cv is still busy (still has waiters)|
| */|
| if (cv->nWaitersBlocked > cv->nWaitersGone)|
| {|
| if (sem_post(&(cv->semBlockLock)) != 0)|
| {|
| result = errno;|
| }|
| result1 = pthread_mutex_unlock(&(cv->mtxUnblockLock));|
| result2 = EBUSY;|
| }|
| else|
| {|
| /*|
| * Now it is safe to destroy|
| */|
| *cond = NULL;|
||
| if (sem_destroy(&(cv->semBlockLock)) != 0)|
| {|
| result = errno;|
| }|
| if (sem_destroy(&(cv->semBlockQueue)) != 0)|
| {|
| result1 = errno;|
| }|
| if ((result2 =
pthread_mutex_unlock(&(cv->mtxUnblockLock))) == 0)|
| {|
| result2 = pthread_mutex_destroy(&(cv->mtxUnblockLock));|
| }|
||
| /* Unlink the CV from the list */|
||
| if (ptw32_cond_list_head == cv)|
| {|
| ptw32_cond_list_head = cv->next;|
| }|
| else|
| {|
| cv->prev->next = cv->next;|
| }|
||
| if (ptw32_cond_list_tail == cv)|
| {|
| ptw32_cond_list_tail = cv->prev;|
| }|
| else|
| {|
| cv->next->prev = cv->prev;|
| }|
||
| (void) free(cv);|
| }|
||
| LeaveCriticalSection(&ptw32_cond_list_lock);|
| }|
| else|
| {|
| /*|
| * See notes in ptw32_cond_check_need_init() above also.|
| */|
| EnterCriticalSection(&ptw32_cond_test_init_lock);|
||
| /*|
| * Check again.|
| */|
| if (*cond == PTHREAD_COND_INITIALIZER)|
| {|
| /*|
| * This is all we need to do to destroy a statically|
| * initialised cond that has not yet been used
(initialised).|
| * If we get to here, another thread waiting to initialise|
| * this cond will get an EINVAL. That's OK.|
| */|
| *cond = NULL;|
| }|
| else|
| {|
| /*|
| * The cv has been initialised while we were waiting|
| * so assume it's in use.|
| */|
| result = EBUSY;|
| }|
||
| LeaveCriticalSection(&ptw32_cond_test_init_lock);|
| }|
||
| return ((result != 0) ? result : ((result1 != 0) ? result1 :
result2));|
|}|
next reply other threads:[~2002-09-19 16:17 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-09-19 9:17 Michael Johnson [this message]
2002-09-19 19:40 ` Ross Johnson
2002-09-19 9:31 Michael Johnson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=3D89F90A.2030805@maine.rr.com \
--to=michaelj@maine.rr.com \
--cc=pthreads-win32@sources.redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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 read-only IMAP folder(s) and NNTP newsgroup(s).