Module Name: src Committed By: kre Date: Sun Dec 11 10:02:53 UTC 2022
Modified Files: src/tests/lib/libpthread: t_condwait.c Log Message: This test makes (made) a false assumption about the way that process scheduling works. That a process (or in this case, a thread) is no longer blocked at time T does not mean that it will resume execution at time T. The OS is free to devote resources to other processes/threads instead - all we should normally be able to expect is that if it is not unblocked before time T, that it will not start running before then. In general though, the pthread_cond_*wait() functions don't guarantee even that - but for this test, the possibility of something else randomly signalling the condvar isn't believable, so don't worry about that possibility (but do fail without calling strerror(0) on the off chance it does happen). Once we cease testing that the process resumed running before some particular time, we can stop dealing with qemu timekeeping issues, it might (seem to) take qemu twice as long as was requested before the thread resumes, but that's OK - the same thing could happen on a loaded system for other reasons. Beyond that, the test also has (had) a race condition. When using CLOCK_REALTIME though that clock needed to have advanced to T before the ETIMEDOUT should happen, there is no guarantee that it will stay >T (CLOCK_REALTIME is allowed to be reset backwards). So, only test that the current time (after ETIMEDOUT) >= T when we're using CLOCK_MONOTONIC - for CLOCK_REALTIME the time might have stepped back between when the ETIMEDOUT happened and when the thread obtains the current clock reading. For that case, all we can test is that the ETIMEDOUT actually happens. With much of what was there now gone, the code can be simplified, we no longer need to do timespec arithmetic, just one comparison (simpler to test that Tend >= Tstart+period than Tend-Tstart > period as we need Tstart+period for the abstime value for the timeout anyway). Note that this still tests for the issue reported in PR lib/47703 which is where the test came from in the first place. ps: we seem to be missing pthread_cond_clockwait() which is the same as pthread_cond_timedwait() except that the clock to use is passed as a parameter, rather than as an attribute of the condition variable. To generate a diff of this commit: cvs rdiff -u -r1.9 -r1.10 src/tests/lib/libpthread/t_condwait.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/tests/lib/libpthread/t_condwait.c diff -u src/tests/lib/libpthread/t_condwait.c:1.9 src/tests/lib/libpthread/t_condwait.c:1.10 --- src/tests/lib/libpthread/t_condwait.c:1.9 Sat Apr 16 18:15:22 2022 +++ src/tests/lib/libpthread/t_condwait.c Sun Dec 11 10:02:53 2022 @@ -1,4 +1,4 @@ -/* $NetBSD: t_condwait.c,v 1.9 2022/04/16 18:15:22 andvar Exp $ */ +/* $NetBSD: t_condwait.c,v 1.10 2022/12/11 10:02:53 kre Exp $ */ /* * Copyright (c) 2013 The NetBSD Foundation, Inc. @@ -26,7 +26,7 @@ * POSSIBILITY OF SUCH DAMAGE. */ #include <sys/cdefs.h> -__RCSID("$NetBSD: t_condwait.c,v 1.9 2022/04/16 18:15:22 andvar Exp $"); +__RCSID("$NetBSD: t_condwait.c,v 1.10 2022/12/11 10:02:53 kre Exp $"); #include <sys/time.h> #include <errno.h> @@ -50,7 +50,7 @@ static const int debug = 1; static void * run(void *param) { - struct timespec ts, to, te, twmin, twmax; + struct timespec ts, to, te; clockid_t clck; pthread_condattr_t attr; pthread_cond_t cond; @@ -78,30 +78,18 @@ run(void *param) case ETIMEDOUT: /* Timeout */ ATF_REQUIRE_EQ(clock_gettime(clck, &te), 0); - timespecsub(&te, &to, &to); if (debug) { printf("timeout: %lld.%09ld sec\n", (long long)te.tv_sec, te.tv_nsec); + timespecsub(&te, &to, &to); printf("elapsed: %lld.%09ld sec\n", (long long)to.tv_sec, to.tv_nsec); } - twmin.tv_sec = WAITTIME; - twmin.tv_nsec = 0; - if (isQEMU()) { - struct timespec td, t; - // td.tv_sec = 0; - // td.tv_nsec = 900000000; - t = twmin; - // timespecsub(&t, &td, &twmin); - td.tv_sec = 2; - td.tv_nsec = 500000000; - timespecadd(&t, &td, &twmax); - } else { - twmax = twmin; - twmax.tv_sec++; - } - ATF_REQUIRE(timespeccmp(&to, &twmin, >=)); - ATF_REQUIRE(timespeccmp(&to, &twmax, <=)); + if (clck == CLOCK_MONOTONIC) + ATF_REQUIRE(timespeccmp(&te, &ts, >=)); + break; + case 0: + atf_tc_fail("pthread_cond_timedwait returned before timeout"); break; default: ATF_REQUIRE_MSG(0, "pthread_cond_timedwait: %s", strerror(ret));