* Re: Deadlock interaction between pthread_cond_check_need_init.c and pthread_cond_destroy.c
@ 2002-09-19 9:31 Michael Johnson
0 siblings, 0 replies; 3+ messages in thread
From: Michael Johnson @ 2002-09-19 9:31 UTC (permalink / raw)
To: pthreads-win32 discussion list
I see that Mozilla mangled my table that illustrates the deadlock when it
converted it to plain text, instead of handling it intelligently. Here's
the table with the spacing corrected:
Thread A Thread B
Enters ptw32_cond_check_need_init Enters pthread_cond_destroy
EnterCriticalSection(&ptw32_cond_test_init_lock) EnterCriticalSection(&ptw32_cond_list_lock)
(now holds ptw32_cond_test_init_lock) (now holds 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
Michael Johnson
DeskNet Inc.
66 Pearl Street, Suite 300
Portland, ME 04101
Harness the Power of Your Content
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Deadlock interaction between pthread_cond_check_need_init.c and pthread_cond_destroy.c
2002-09-19 9:17 Michael Johnson
@ 2002-09-19 19:40 ` Ross Johnson
0 siblings, 0 replies; 3+ messages in thread
From: Ross Johnson @ 2002-09-19 19:40 UTC (permalink / raw)
To: Michael Johnson; +Cc: pthreads-win32 discussion list
Michael,
I've applied your patch and committed it to CVS. I'll write a test
for it later.
Many thanks for this.
Ross
Michael Johnson wrote:
> 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));|
> |}|
>
^ permalink raw reply [flat|nested] 3+ messages in thread
* Deadlock interaction between pthread_cond_check_need_init.c and pthread_cond_destroy.c
@ 2002-09-19 9:17 Michael Johnson
2002-09-19 19:40 ` Ross Johnson
0 siblings, 1 reply; 3+ messages in thread
From: Michael Johnson @ 2002-09-19 9:17 UTC (permalink / raw)
To: pthreads-win32 discussion list
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));|
|}|
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2002-09-20 2:40 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-09-19 9:31 Deadlock interaction between pthread_cond_check_need_init.c and pthread_cond_destroy.c Michael Johnson
-- strict thread matches above, loose matches on Subject: below --
2002-09-19 9:17 Michael Johnson
2002-09-19 19:40 ` Ross Johnson
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).