* [patch] testsuite: Fix a race by me - watchthreads-reorder.exp
@ 2009-12-17 19:50 Jan Kratochvil
2009-12-17 20:27 ` Tom Tromey
0 siblings, 1 reply; 5+ messages in thread
From: Jan Kratochvil @ 2009-12-17 19:50 UTC (permalink / raw)
To: gdb-patches
Hi,
there is a bug explainable by man pthread_cond_signal:
The [...] pthread_cond_signal() functions shall have no effect if
there are no threads currently blocked on cond.
meaning a race for the testcase.
+FAIL: gdb.threads/watchthreads-reorder.exp: reorder1: continue a (timeout)
One can reproduce the race on CVS HEAD by:
# --- a/gdb/testsuite/gdb.threads/watchthreads-reorder.c
# +++ b/gdb/testsuite/gdb.threads/watchthreads-reorder.c
# @@ -89,6 +89,7 @@ thread1_func (void *unused)
# int i;
# volatile int rwatch_store;
#
# +sleep(1);
# thread1_tid = gettid ();
# i = pthread_cond_signal (&thread1_tid_cond);
# assert (i == 0);
# @@ -317,6 +318,7 @@ main (int argc, char **argv)
#
# if (thread1_tid == 0)
# {
# +sleep(2);
# i = pthread_cond_wait (&thread1_tid_cond, &thread1_tid_mutex);
# assert (i == 0);
#
Fixed; gdbstop_mutex got removed as it became redundant there.
Going to check it in as obvious in several days (code is by me + it is just
a testcase).
Sorry,
Jan
gdb/testsuite/
2009-12-17 Jan Kratochvil <jan.kratochvil@redhat.com>
* gdb.threads/watchthreads-reorder.c (gdbstop_mutex): Remove.
(thread1_func): Protect thread1_tid_cond by thread1_tid_mutex. Remove
gdbstop_mutex handling.
(thread2_func): Protect thread2_tid_cond by thread2_tid_mutex. Remove
gdbstop_mutex handling.
(main): Move thread1_tid_mutex and thread2_tid_mutex locks before
pthread_create. Remove gdbstop_mutex handling. New comment. Remove
pthread_cond_wait conditionalizations.
--- a/gdb/testsuite/gdb.threads/watchthreads-reorder.c
+++ b/gdb/testsuite/gdb.threads/watchthreads-reorder.c
@@ -34,8 +34,6 @@
otherwise. */
#define TIMEOUT (gettid () == getpid() ? 10 : 15)
-static pthread_mutex_t gdbstop_mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
-
static pid_t thread1_tid;
static pthread_cond_t thread1_tid_cond = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t thread1_tid_mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
@@ -90,12 +88,11 @@ thread1_func (void *unused)
volatile int rwatch_store;
thread1_tid = gettid ();
+
+ timed_mutex_lock (&thread1_tid_mutex);
i = pthread_cond_signal (&thread1_tid_cond);
assert (i == 0);
-
- /* Be sure GDB is already stopped before continuing. */
- timed_mutex_lock (&gdbstop_mutex);
- i = pthread_mutex_unlock (&gdbstop_mutex);
+ i = pthread_mutex_unlock (&thread1_tid_mutex);
assert (i == 0);
rwatch_store = thread1_rwatch;
@@ -115,12 +112,11 @@ thread2_func (void *unused)
volatile int rwatch_store;
thread2_tid = gettid ();
+
+ timed_mutex_lock (&thread2_tid_mutex);
i = pthread_cond_signal (&thread2_tid_cond);
assert (i == 0);
-
- /* Be sure GDB is already stopped before continuing. */
- timed_mutex_lock (&gdbstop_mutex);
- i = pthread_mutex_unlock (&gdbstop_mutex);
+ i = pthread_mutex_unlock (&thread2_tid_mutex);
assert (i == 0);
rwatch_store = thread2_rwatch;
@@ -267,7 +263,8 @@ main (int argc, char **argv)
setbuf (stdout, NULL);
- timed_mutex_lock (&gdbstop_mutex);
+ timed_mutex_lock (&thread1_tid_mutex);
+ timed_mutex_lock (&thread2_tid_mutex);
timed_mutex_lock (&terminate_mutex);
@@ -306,30 +303,21 @@ main (int argc, char **argv)
state_wait (tracer, "T (stopped)");
}
- timed_mutex_lock (&thread1_tid_mutex);
- timed_mutex_lock (&thread2_tid_mutex);
-
- /* Let the threads start. */
- i = pthread_mutex_unlock (&gdbstop_mutex);
- assert (i == 0);
+ /* Threads are now waiting at timed_mutex_lock (thread1_tid_mutex) and so
+ they could not trigger the watchpoints before GDB gets unstopped later.
+ Threads get resumed at pthread_cond_wait below. */
printf ("Waiting till the threads initialize their TIDs.\n");
- if (thread1_tid == 0)
- {
- i = pthread_cond_wait (&thread1_tid_cond, &thread1_tid_mutex);
- assert (i == 0);
+ i = pthread_cond_wait (&thread1_tid_cond, &thread1_tid_mutex);
+ assert (i == 0);
- assert (thread1_tid > 0);
- }
+ assert (thread1_tid > 0);
- if (thread2_tid == 0)
- {
- i = pthread_cond_wait (&thread2_tid_cond, &thread2_tid_mutex);
- assert (i == 0);
+ i = pthread_cond_wait (&thread2_tid_cond, &thread2_tid_mutex);
+ assert (i == 0);
- assert (thread2_tid > 0);
- }
+ assert (thread2_tid > 0);
printf ("Thread 1 TID = %lu, thread 2 TID = %lu, PID = %lu.\n",
(unsigned long) thread1_tid, (unsigned long) thread2_tid,
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [patch] testsuite: Fix a race by me - watchthreads-reorder.exp
2009-12-17 19:50 [patch] testsuite: Fix a race by me - watchthreads-reorder.exp Jan Kratochvil
@ 2009-12-17 20:27 ` Tom Tromey
2010-01-19 22:12 ` Jan Kratochvil
0 siblings, 1 reply; 5+ messages in thread
From: Tom Tromey @ 2009-12-17 20:27 UTC (permalink / raw)
To: Jan Kratochvil; +Cc: gdb-patches
>>>>> "Jan" == Jan Kratochvil <jan.kratochvil@redhat.com> writes:
Jan> there is a bug explainable by man pthread_cond_signal:
Jan> The [...] pthread_cond_signal() functions shall have no effect if
Jan> there are no threads currently blocked on cond.
Jan> + i = pthread_cond_wait (&thread1_tid_cond, &thread1_tid_mutex);
Jan> + assert (i == 0);
pthread_cond_wait can also spuriously wake up. The usual thing to do is
call it in a loop that checks some condition. Then, have the signalling
thread set the condition before calling pthread_cond_signal. Something
like:
while (thread1_tid == 0)
pthread_cond_wait (...);
This is race-free as long as the signalling thread also acquires the
mutex associated with the condition.
Is there some reason not to do this in this test case?
Tom
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [patch] testsuite: Fix a race by me - watchthreads-reorder.exp
2009-12-17 20:27 ` Tom Tromey
@ 2010-01-19 22:12 ` Jan Kratochvil
2010-01-20 21:01 ` Tom Tromey
0 siblings, 1 reply; 5+ messages in thread
From: Jan Kratochvil @ 2010-01-19 22:12 UTC (permalink / raw)
To: Tom Tromey; +Cc: gdb-patches
On Thu, 17 Dec 2009 21:27:24 +0100, Tom Tromey wrote:
> >>>>> "Jan" == Jan Kratochvil <jan.kratochvil@redhat.com> writes:
> Jan> + i = pthread_cond_wait (&thread1_tid_cond, &thread1_tid_mutex);
> Jan> + assert (i == 0);
>
> pthread_cond_wait can also spuriously wake up. The usual thing to do is
> call it in a loop that checks some condition. Then, have the signalling
> thread set the condition before calling pthread_cond_signal. Something
> like:
>
> while (thread1_tid == 0)
> pthread_cond_wait (...);
>
> This is race-free as long as the signalling thread also acquires the
> mutex associated with the condition.
>
> Is there some reason not to do this in this test case?
Thanks, I was not proficient in these functions (and have not read the whole
manual).
OK to check-in?
Thanks,
Jan
gdb/testsuite/
2010-01-19 Jan Kratochvil <jan.kratochvil@redhat.com>
* gdb.threads/watchthreads-reorder.c (gdbstop_mutex): Remove.
(thread1_func): Protect thread1_tid with thread1_tid_cond by
thread1_tid_mutex. Remove gdbstop_mutex handling.
(thread2_func): Protect thread2_tid with thread2_tid_cond by
thread2_tid_mutex. Remove gdbstop_mutex handling.
(main): Move thread1_tid_mutex and thread2_tid_mutex locks before
pthread_create. Remove gdbstop_mutex handling. New comment. Replace
pthread_cond_wait conditionalizations by while loops.
--- a/gdb/testsuite/gdb.threads/watchthreads-reorder.c
+++ b/gdb/testsuite/gdb.threads/watchthreads-reorder.c
@@ -34,8 +34,6 @@
otherwise. */
#define TIMEOUT (gettid () == getpid() ? 10 : 15)
-static pthread_mutex_t gdbstop_mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
-
static pid_t thread1_tid;
static pthread_cond_t thread1_tid_cond = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t thread1_tid_mutex = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
@@ -89,13 +87,14 @@ thread1_func (void *unused)
int i;
volatile int rwatch_store;
+ timed_mutex_lock (&thread1_tid_mutex);
+
+ /* THREAD1_TID_MUTEX must be already locked to avoid race. */
thread1_tid = gettid ();
+
i = pthread_cond_signal (&thread1_tid_cond);
assert (i == 0);
-
- /* Be sure GDB is already stopped before continuing. */
- timed_mutex_lock (&gdbstop_mutex);
- i = pthread_mutex_unlock (&gdbstop_mutex);
+ i = pthread_mutex_unlock (&thread1_tid_mutex);
assert (i == 0);
rwatch_store = thread1_rwatch;
@@ -114,13 +113,14 @@ thread2_func (void *unused)
int i;
volatile int rwatch_store;
+ timed_mutex_lock (&thread2_tid_mutex);
+
+ /* THREAD2_TID_MUTEX must be already locked to avoid race. */
thread2_tid = gettid ();
+
i = pthread_cond_signal (&thread2_tid_cond);
assert (i == 0);
-
- /* Be sure GDB is already stopped before continuing. */
- timed_mutex_lock (&gdbstop_mutex);
- i = pthread_mutex_unlock (&gdbstop_mutex);
+ i = pthread_mutex_unlock (&thread2_tid_mutex);
assert (i == 0);
rwatch_store = thread2_rwatch;
@@ -267,7 +267,8 @@ main (int argc, char **argv)
setbuf (stdout, NULL);
- timed_mutex_lock (&gdbstop_mutex);
+ timed_mutex_lock (&thread1_tid_mutex);
+ timed_mutex_lock (&thread2_tid_mutex);
timed_mutex_lock (&terminate_mutex);
@@ -306,29 +307,23 @@ main (int argc, char **argv)
state_wait (tracer, "T (stopped)");
}
- timed_mutex_lock (&thread1_tid_mutex);
- timed_mutex_lock (&thread2_tid_mutex);
-
- /* Let the threads start. */
- i = pthread_mutex_unlock (&gdbstop_mutex);
- assert (i == 0);
+ /* Threads are now waiting at timed_mutex_lock (thread1_tid_mutex) and so
+ they could not trigger the watchpoints before GDB gets unstopped later.
+ Threads get resumed at pthread_cond_wait below. Use `while' loops for
+ protection against spurious pthread_cond_wait wakeups. */
printf ("Waiting till the threads initialize their TIDs.\n");
- if (thread1_tid == 0)
+ while (thread1_tid == 0)
{
i = pthread_cond_wait (&thread1_tid_cond, &thread1_tid_mutex);
assert (i == 0);
-
- assert (thread1_tid > 0);
}
- if (thread2_tid == 0)
+ while (thread2_tid == 0)
{
i = pthread_cond_wait (&thread2_tid_cond, &thread2_tid_mutex);
assert (i == 0);
-
- assert (thread2_tid > 0);
}
printf ("Thread 1 TID = %lu, thread 2 TID = %lu, PID = %lu.\n",
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [patch] testsuite: Fix a race by me - watchthreads-reorder.exp
2010-01-19 22:12 ` Jan Kratochvil
@ 2010-01-20 21:01 ` Tom Tromey
2010-01-20 21:10 ` Jan Kratochvil
0 siblings, 1 reply; 5+ messages in thread
From: Tom Tromey @ 2010-01-20 21:01 UTC (permalink / raw)
To: Jan Kratochvil; +Cc: gdb-patches
>>>>> "Jan" == Jan Kratochvil <jan.kratochvil@redhat.com> writes:
Jan> 2010-01-19 Jan Kratochvil <jan.kratochvil@redhat.com>
Jan> * gdb.threads/watchthreads-reorder.c (gdbstop_mutex): Remove.
Jan> (thread1_func): Protect thread1_tid with thread1_tid_cond by
Jan> thread1_tid_mutex. Remove gdbstop_mutex handling.
Jan> (thread2_func): Protect thread2_tid with thread2_tid_cond by
Jan> thread2_tid_mutex. Remove gdbstop_mutex handling.
Jan> (main): Move thread1_tid_mutex and thread2_tid_mutex locks before
Jan> pthread_create. Remove gdbstop_mutex handling. New comment. Replace
Jan> pthread_cond_wait conditionalizations by while loops.
Ok. Thanks.
Tom
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [patch] testsuite: Fix a race by me - watchthreads-reorder.exp
2010-01-20 21:01 ` Tom Tromey
@ 2010-01-20 21:10 ` Jan Kratochvil
0 siblings, 0 replies; 5+ messages in thread
From: Jan Kratochvil @ 2010-01-20 21:10 UTC (permalink / raw)
To: Tom Tromey; +Cc: gdb-patches
On Wed, 20 Jan 2010 22:01:22 +0100, Tom Tromey wrote:
> >>>>> "Jan" == Jan Kratochvil <jan.kratochvil@redhat.com> writes:
>
> Jan> 2010-01-19 Jan Kratochvil <jan.kratochvil@redhat.com>
> Jan> * gdb.threads/watchthreads-reorder.c (gdbstop_mutex): Remove.
> Jan> (thread1_func): Protect thread1_tid with thread1_tid_cond by
> Jan> thread1_tid_mutex. Remove gdbstop_mutex handling.
> Jan> (thread2_func): Protect thread2_tid with thread2_tid_cond by
> Jan> thread2_tid_mutex. Remove gdbstop_mutex handling.
> Jan> (main): Move thread1_tid_mutex and thread2_tid_mutex locks before
> Jan> pthread_create. Remove gdbstop_mutex handling. New comment. Replace
> Jan> pthread_cond_wait conditionalizations by while loops.
>
> Ok. Thanks.
Checked-in:
http://sourceware.org/ml/gdb-cvs/2010-01/msg00182.html
Thanks,
Jan
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2010-01-20 21:10 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-12-17 19:50 [patch] testsuite: Fix a race by me - watchthreads-reorder.exp Jan Kratochvil
2009-12-17 20:27 ` Tom Tromey
2010-01-19 22:12 ` Jan Kratochvil
2010-01-20 21:01 ` Tom Tromey
2010-01-20 21:10 ` Jan Kratochvil
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox