From: malteskarupke@fastmail.fm
To: libc-alpha@sourceware.org
Cc: Malte Skarupke <malteskarupke@fastmail.fm>
Subject: [PATCH v3 4/6] nptl: Make test-cond-printers check the number of waiters
Date: Thu, 6 Oct 2022 17:43:27 -0400 [thread overview]
Message-ID: <20221006214329.1084244-5-malteskarupke@fastmail.fm> (raw)
In-Reply-To: <20221006214329.1084244-1-malteskarupke@fastmail.fm>
From: Malte Skarupke <malteskarupke@fastmail.fm>
In my last change I changed the semantics of how to determine the
number of waiters on a condition variable. The existing test only
tested that the printers print something. They didn't cover the case
when there is a thread sleeping on the condition variable. In this
patch I changed the test to ensure that the correct number is printed.
---
nptl/test-cond-printers.c | 56 +++++++++++++++++++++++++++++++++-----
nptl/test-cond-printers.py | 5 ++++
2 files changed, 54 insertions(+), 7 deletions(-)
diff --git a/nptl/test-cond-printers.c b/nptl/test-cond-printers.c
index 51d7f920b3..615fd5a681 100644
--- a/nptl/test-cond-printers.c
+++ b/nptl/test-cond-printers.c
@@ -26,7 +26,14 @@
#define PASS 0
#define FAIL 1
-static int test_status_destroyed (pthread_cond_t *condvar);
+static int test_status (pthread_cond_t *condvar);
+
+typedef struct
+{
+ pthread_mutex_t *mutex;
+ pthread_cond_t *condvar;
+ int *wait_thread_asleep;
+} test_state;
int
main (void)
@@ -36,22 +43,57 @@ main (void)
int result = FAIL;
if (pthread_condattr_init (&attr) == 0
- && test_status_destroyed (&condvar) == PASS)
+ && test_status (&condvar) == PASS)
result = PASS;
/* Else, one of the pthread_cond* functions failed. */
return result;
}
+static void *
+wait (void *arg)
+{
+ test_state *state = (test_state *)arg;
+ void *result = PASS;
+ if (pthread_mutex_lock (state->mutex) != 0)
+ result = (void *)FAIL;
+ *state->wait_thread_asleep = 1;
+ if (pthread_cond_signal (state->condvar) != 0)
+ result = (void *)FAIL;
+ if (pthread_cond_wait (state->condvar, state->mutex) != 0)
+ result = (void *)FAIL;
+ if (pthread_mutex_unlock (state->mutex) != 0)
+ result = (void *)FAIL;
+ return result;
+}
+
/* Initializes CONDVAR, then destroys it. */
static int
-test_status_destroyed (pthread_cond_t *condvar)
+test_status (pthread_cond_t *condvar)
{
- int result = FAIL;
+ int result = PASS;
- if (pthread_cond_init (condvar, NULL) == 0
- && pthread_cond_destroy (condvar) == 0)
- result = PASS; /* Test status (destroyed). */
+ pthread_mutex_t mutex;
+ result |= pthread_mutex_init (&mutex, NULL);
+ result |= pthread_cond_init (condvar, NULL);
+ int wait_thread_asleep = 0;
+ test_state state = { &mutex, condvar, &wait_thread_asleep };
+ result |= pthread_mutex_lock (&mutex);
+ pthread_t thread;
+ result |= pthread_create (&thread, NULL, wait, &state);
+ while (!wait_thread_asleep)
+ {
+ result |= pthread_cond_wait (condvar, &mutex);
+ }
+ result |= pthread_cond_signal (condvar); /* Test about to signal */
+ result |= pthread_mutex_unlock (&mutex);
+ result |= pthread_cond_destroy (condvar);
+ void *retval = NULL;
+ result |= pthread_join (thread, &retval); /* Test status (destroyed). */
+ result |= pthread_mutex_destroy (&mutex);
+ result = result ? FAIL : PASS;
+ if (retval != NULL)
+ result = FAIL;
return result;
}
diff --git a/nptl/test-cond-printers.py b/nptl/test-cond-printers.py
index 42329c1691..7945c7a0d5 100644
--- a/nptl/test-cond-printers.py
+++ b/nptl/test-cond-printers.py
@@ -33,6 +33,11 @@ try:
var = 'condvar'
to_string = 'pthread_cond_t'
+ break_at(test_source, 'Test about to signal')
+ continue_cmd() # Go to test_status_destroyed
+ test_printer(var, to_string, {'Threads known to still execute a wait function': '1'})
+
+
break_at(test_source, 'Test status (destroyed)')
continue_cmd() # Go to test_status_destroyed
test_printer(var, to_string, {'Threads known to still execute a wait function': '0'})
--
2.25.1
next prev parent reply other threads:[~2022-10-06 21:43 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-10-06 21:43 [PATCH v3 0/6] nptl: Fix pthread_cond_signal missing a sleeper malteskarupke
2022-10-06 21:43 ` [PATCH v3 1/6] nptl: Fix pthread_cond_signal missing a sleeper (#BZ 25847) malteskarupke
2022-10-06 21:43 ` [PATCH v3 2/6] nptl: Remove the signal-stealing code. It is no longer needed malteskarupke
2022-10-06 21:43 ` [PATCH v3 3/6] nptl: Optimization by not incrementing wrefs in pthread_cond_wait malteskarupke
2022-10-06 21:43 ` malteskarupke [this message]
2022-10-06 21:43 ` [PATCH v3 5/6] nptl: Rename __wrefs to __crefs because its meaning has changed malteskarupke
2022-10-06 21:43 ` [PATCH v3 6/6] nptl: Cleaning up __g1_start and related code in pthread_cond_wait malteskarupke
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=20221006214329.1084244-5-malteskarupke@fastmail.fm \
--to=malteskarupke@fastmail.fm \
--cc=libc-alpha@sourceware.org \
/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).