* tests/test-cnd.c: Improve comments.
(cond_value): Remove initializer.
(cond_timed_out): Renamed from cond_timeout.
* tests/test-cond.c: Improve comments.
(cond_value): Remove initializer.
(cond_timed_out): Renamed from cond_timeout.
* tests/test-pthread-cond.c: Improve comments.
(cond_value): Remove initializer.
(cond_timed_out): Renamed from cond_timeout.
+2024-06-28 Bruno Haible <bruno@clisp.org>
+
+ cond tests: Improve comments.
+ * tests/test-cnd.c: Improve comments.
+ (cond_value): Remove initializer.
+ (cond_timed_out): Renamed from cond_timeout.
+ * tests/test-cond.c: Improve comments.
+ (cond_value): Remove initializer.
+ (cond_timed_out): Renamed from cond_timeout.
+ * tests/test-pthread-cond.c: Improve comments.
+ (cond_value): Remove initializer.
+ (cond_timed_out): Renamed from cond_timeout.
+
2024-06-28 Bruno Haible <bruno@clisp.org>
time: Fix test failure on FreeBSD.
/*
* Condition check
*/
-static int cond_value = 0;
+static int cond_value;
static cnd_t condtest;
static mtx_t lockcond;
static void
test_cnd_wait ()
{
- struct timespec remain;
thrd_t thread;
int ret;
- remain.tv_sec = 2;
- remain.tv_nsec = 0;
-
cond_value = 0;
+ /* Create a separate thread. */
ASSERT (thrd_create (&thread, cnd_wait_routine, NULL) == thrd_success);
- do
- {
- yield ();
- ret = thrd_sleep (&remain, &remain);
- ASSERT (ret >= -1);
- }
- while (ret == -1 && (remain.tv_sec != 0 || remain.tv_nsec != 0));
- /* signal condition */
+ /* Sleep for 2 seconds. */
+ {
+ struct timespec remaining;
+
+ remaining.tv_sec = 2;
+ remaining.tv_nsec = 0;
+
+ do
+ {
+ yield ();
+ ret = thrd_sleep (&remaining, &remaining);
+ ASSERT (ret >= -1);
+ }
+ while (ret == -1 && (remaining.tv_sec != 0 || remaining.tv_nsec != 0));
+ }
+
+ /* Tell one of the waiting threads (if any) to continue. */
ASSERT (mtx_lock (&lockcond) == thrd_success);
cond_value = 1;
ASSERT (cnd_signal (&condtest) == thrd_success);
/*
* Timed Condition check
*/
-static int cond_timeout;
+static int cond_timed_out;
+/* Stores in *TS the current time plus 1 second. */
static void
get_ts (struct timespec *ts)
{
get_ts (&ts);
ret = cnd_timedwait (&condtest, &lockcond, &ts);
if (ret == thrd_timedout)
- cond_timeout = 1;
+ cond_timed_out = 1;
}
ASSERT (mtx_unlock (&lockcond) == thrd_success);
static void
test_cnd_timedwait (void)
{
- struct timespec remain;
thrd_t thread;
int ret;
- remain.tv_sec = 2;
- remain.tv_nsec = 0;
-
- cond_value = cond_timeout = 0;
+ cond_value = cond_timed_out = 0;
+ /* Create a separate thread. */
ASSERT (thrd_create (&thread, cnd_timedwait_routine, NULL) == thrd_success);
- do
- {
- yield ();
- ret = thrd_sleep (&remain, &remain);
- ASSERT (ret >= -1);
- }
- while (ret == -1 && (remain.tv_sec != 0 || remain.tv_nsec != 0));
- /* signal condition */
+ /* Sleep for 2 seconds. */
+ {
+ struct timespec remaining;
+
+ remaining.tv_sec = 2;
+ remaining.tv_nsec = 0;
+
+ do
+ {
+ yield ();
+ ret = thrd_sleep (&remaining, &remaining);
+ ASSERT (ret >= -1);
+ }
+ while (ret == -1 && (remaining.tv_sec != 0 || remaining.tv_nsec != 0));
+ }
+
+ /* Tell one of the waiting threads (if any) to continue. */
ASSERT (mtx_lock (&lockcond) == thrd_success);
cond_value = 1;
ASSERT (cnd_signal (&condtest) == thrd_success);
ASSERT (thrd_join (thread, NULL) == thrd_success);
- if (!cond_timeout)
+ if (!cond_timed_out)
abort ();
}
/*
* Condition check
*/
-static int cond_value = 0;
+static int cond_value;
gl_cond_define_initialized(static, condtest)
gl_lock_define_initialized(static, lockcond)
static void
test_cond ()
{
- int remain = 2;
gl_thread_t thread;
cond_value = 0;
+ /* Create a separate thread. */
thread = gl_thread_create (cond_routine, NULL);
- do
- {
- yield ();
- remain = sleep (remain);
- }
- while (remain);
- /* signal condition */
+ /* Sleep for 2 seconds. */
+ {
+ int remaining = 2;
+
+ do
+ {
+ yield ();
+ remaining = sleep (remaining);
+ }
+ while (remaining);
+ }
+
+ /* Tell one of the waiting threads (if any) to continue. */
gl_lock_lock (lockcond);
cond_value = 1;
gl_cond_signal (condtest);
/*
* Timed Condition check
*/
-static int cond_timeout;
+static int cond_timed_out;
+/* Stores in *TS the current time plus 1 second. */
static void
get_ts (struct timespec *ts)
{
get_ts (&ts);
ret = glthread_cond_timedwait (&condtest, &lockcond, &ts);
if (ret == ETIMEDOUT)
- cond_timeout = 1;
+ cond_timed_out = 1;
}
gl_lock_unlock (lockcond);
static void
test_timedcond (void)
{
- int remain = 2;
gl_thread_t thread;
- cond_value = cond_timeout = 0;
+ cond_value = cond_timed_out = 0;
+ /* Create a separate thread. */
thread = gl_thread_create (timedcond_routine, NULL);
- do
- {
- yield ();
- remain = sleep (remain);
- }
- while (remain);
- /* signal condition */
+ /* Sleep for 2 seconds. */
+ {
+ int remaining = 2;
+
+ do
+ {
+ yield ();
+ remaining = sleep (remaining);
+ }
+ while (remaining);
+ }
+
+ /* Tell one of the waiting threads (if any) to continue. */
gl_lock_lock (lockcond);
cond_value = 1;
gl_cond_signal (condtest);
gl_thread_join (thread, NULL);
- if (!cond_timeout)
+ if (!cond_timed_out)
abort ();
}
/*
* Condition check
*/
-static int cond_value = 0;
+static int cond_value;
static pthread_cond_t condtest;
static pthread_mutex_t lockcond;
static void
test_pthread_cond_wait ()
{
- struct timespec remain;
pthread_t thread;
int ret;
- remain.tv_sec = 2;
- remain.tv_nsec = 0;
-
cond_value = 0;
+ /* Create a separate thread. */
ASSERT (pthread_create (&thread, NULL, pthread_cond_wait_routine, NULL) == 0);
- do
- {
- yield ();
- ret = nanosleep (&remain, &remain);
- ASSERT (ret >= -1);
- }
- while (ret == -1 && (remain.tv_sec != 0 || remain.tv_nsec != 0));
- /* signal condition */
+ /* Sleep for 2 seconds. */
+ {
+ struct timespec remaining;
+
+ remaining.tv_sec = 2;
+ remaining.tv_nsec = 0;
+
+ do
+ {
+ yield ();
+ ret = nanosleep (&remaining, &remaining);
+ ASSERT (ret >= -1);
+ }
+ while (ret == -1 && (remaining.tv_sec != 0 || remaining.tv_nsec != 0));
+ }
+
+ /* Tell one of the waiting threads (if any) to continue. */
ASSERT (pthread_mutex_lock (&lockcond) == 0);
cond_value = 1;
ASSERT (pthread_cond_signal (&condtest) == 0);
/*
* Timed Condition check
*/
-static int cond_timeout;
+static int cond_timed_out;
+/* Stores in *TS the current time plus 1 second. */
static void
get_ts (struct timespec *ts)
{
get_ts (&ts);
ret = pthread_cond_timedwait (&condtest, &lockcond, &ts);
if (ret == ETIMEDOUT)
- cond_timeout = 1;
+ cond_timed_out = 1;
}
ASSERT (pthread_mutex_unlock (&lockcond) == 0);
static void
test_pthread_cond_timedwait (void)
{
- struct timespec remain;
pthread_t thread;
int ret;
- remain.tv_sec = 2;
- remain.tv_nsec = 0;
-
- cond_value = cond_timeout = 0;
+ cond_value = cond_timed_out = 0;
+ /* Create a separate thread. */
ASSERT (pthread_create (&thread, NULL, pthread_cond_timedwait_routine, NULL)
== 0);
- do
- {
- yield ();
- ret = nanosleep (&remain, &remain);
- ASSERT (ret >= -1);
- }
- while (ret == -1 && (remain.tv_sec != 0 || remain.tv_nsec != 0));
- /* signal condition */
+ /* Sleep for 2 seconds. */
+ {
+ struct timespec remaining;
+
+ remaining.tv_sec = 2;
+ remaining.tv_nsec = 0;
+
+ do
+ {
+ yield ();
+ ret = nanosleep (&remaining, &remaining);
+ ASSERT (ret >= -1);
+ }
+ while (ret == -1 && (remaining.tv_sec != 0 || remaining.tv_nsec != 0));
+ }
+
+ /* Tell one of the waiting threads (if any) to continue. */
ASSERT (pthread_mutex_lock (&lockcond) == 0);
cond_value = 1;
ASSERT (pthread_cond_signal (&condtest) == 0);
ASSERT (pthread_join (thread, NULL) == 0);
- if (!cond_timeout)
+ if (!cond_timed_out)
abort ();
}