From 1e13d57c9f6a5ee6f86a9e1c93cb77cd67678294 Mon Sep 17 00:00:00 2001 From: "tanqinyun.tqy" Date: Fri, 10 Nov 2023 14:52:12 +0800 Subject: [PATCH] open_posix_testsuite: fix pthread_cond_destroy test error. According to the description of the pthread_cond_destroy function in glibc 2.32, it is advised to ensure that all threads blocked on the condition variable have been successfully awakened before calling this function, otherwise unpredictable situations may occur. In the glibc 2.32 version, this testcase will hang. Therefore, it is necessary to ensure that all threads have been successfully awakened before calling pthread_cond_destroy to destroy the condition variable. Signed-off-by: tanqinyun.tqy --- .../interfaces/pthread_cond_destroy/2-1.c | 67 ++++++++++--------- 1 file changed, 37 insertions(+), 30 deletions(-) diff --git a/testcases/open_posix_testsuite/conformance/interfaces/pthread_cond_destroy/2-1.c b/testcases/open_posix_testsuite/conformance/interfaces/pthread_cond_destroy/2-1.c index 63363bbeacf..b56b91e0d2a 100644 --- a/testcases/open_posix_testsuite/conformance/interfaces/pthread_cond_destroy/2-1.c +++ b/testcases/open_posix_testsuite/conformance/interfaces/pthread_cond_destroy/2-1.c @@ -298,7 +298,9 @@ static void *child(void *arg PTS_ATTRIBUTE_UNUSED) UNRESOLVED(ret, "Failed to wait for the cond"); } - /* unlock the mutex */ + td->count2--; + + /* unlock the mutex */ ret = pthread_mutex_unlock(&td->mtx2); if (ret != 0) { UNRESOLVED(ret, "Failed to unlock the mutex."); @@ -603,6 +605,23 @@ int main(void) p_child); } + /* Make sure all children have exited the first wait */ + ch = td->count1; + while (ch > 0) { + ret = pthread_mutex_unlock(&td->mtx1); + if (ret != 0) { + UNRESOLVED_KILLALL( + ret, "Failed to unlock mutex", p_child); + } + sched_yield(); + ret = pthread_mutex_lock(&td->mtx1); + if (ret != 0) { + UNRESOLVED_KILLALL(ret, "Failed to lock mutex", + p_child); + } + ch = td->count1; + } + ret = pthread_mutex_unlock(&td->mtx1); if (ret != 0) { UNRESOLVED_KILLALL(ret, "Failed to unlock mutex", @@ -625,35 +644,6 @@ int main(void) ("[parent] Condition was broadcasted, and condvar destroyed.\n"); #endif - /* Make sure all children have exited the first wait */ - ret = pthread_mutex_lock(&td->mtx1); - if (ret != 0) { - UNRESOLVED_KILLALL(ret, "Failed to lock mutex", - p_child); - } - ch = td->count1; - while (ch > 0) { - ret = pthread_mutex_unlock(&td->mtx1); - if (ret != 0) { - UNRESOLVED_KILLALL(ret, - "Failed to unlock mutex", - p_child); - } - sched_yield(); - ret = pthread_mutex_lock(&td->mtx1); - if (ret != 0) { - UNRESOLVED_KILLALL(ret, "Failed to lock mutex", - p_child); - } - ch = td->count1; - } - - ret = pthread_mutex_unlock(&td->mtx1); - if (ret != 0) { - UNRESOLVED_KILLALL(ret, "Failed to unlock mutex", - p_child); - } - /* Go toward the 2nd pass */ /* Now, all children are waiting to lock the 2nd mutex, which we own here. */ /* reinitialize the condvar */ @@ -697,6 +687,23 @@ int main(void) p_child); } + /* Make sure all children have exited the second wait */ + ch = td->count2; + while (ch > 0) { + ret = pthread_mutex_unlock(&td->mtx2); + if (ret != 0) { + UNRESOLVED_KILLALL( + ret, "Failed to unlock mutex", p_child); + } + sched_yield(); + ret = pthread_mutex_lock(&td->mtx2); + if (ret != 0) { + UNRESOLVED_KILLALL(ret, "Failed to lock mutex", + p_child); + } + ch = td->count2; + } + /* Allow the children to terminate */ ret = pthread_mutex_unlock(&td->mtx2); if (ret != 0) {