Commit c9ab343d authored by Chris Friedt's avatar Chris Friedt Committed by Johan Hedberg
Browse files

tests: posix: semaphores: use timespec_add() to add to abstime



Use `timespec_add()` to add to an absolute timespec. This is required
because the test was failing occasionally on some qemu platforms in CI
due to this assertion in `timespec_normalize()`, which could happen if
the number of fractional seconds was already close to `NSEC_PER_SEC`
before an offset was added.

```cpp
if (!overflow) {
	__ASSERT_NO_MSG(timespec_is_valid(ts));
}
```

Signed-off-by: default avatarChris Friedt <cfriedt@tenstorrent.com>
parent 82460c8a
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@
#include <pthread.h>
#include <semaphore.h>

#include <zephyr/sys/timeutil.h>
#include <zephyr/sys/util.h>
#include <zephyr/ztest.h>

@@ -57,8 +58,8 @@ static void semaphore_test(sem_t *sem)

	zassert_equal(clock_gettime(CLOCK_REALTIME, &abstime), 0, "clock_gettime failed");

	abstime.tv_sec += WAIT_TIME_MS / MSEC_PER_SEC;
	abstime.tv_nsec += (WAIT_TIME_MS % MSEC_PER_SEC) * NSEC_PER_MSEC;
	timespec_add(&abstime, &(struct timespec){.tv_sec = WAIT_TIME_MS / MSEC_PER_SEC,
						  (WAIT_TIME_MS % MSEC_PER_SEC) * NSEC_PER_MSEC});

	/* TESPOINT: Wait to acquire sem given by thread1 */
	zassert_equal(sem_timedwait(sem, &abstime), 0);