[PATCH v2] tests: posix: use cpu clock for sleep

DJ Delorie dj@redhat.com
Tue Feb 17 18:09:56 GMT 2026


Yury Khrustalev <yury.khrustalev@arm.com> writes:
>  so I don't see what's wrong here. Please elaborate.

Perhaps I'm miscommunicating.

I don't care which clock you use to determine if 2 seconds has passed.

I'm looking at these lines:

+  while (ts.tv_sec < end || (ts.tv_sec == end && ts.tv_nsec < end_ns))
+    clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &ts);

This is a busy-wait loop.  It would be better on most platforms to do
*something* to pause the program, even if only a little.  Example:

+  while (ts.tv_sec < end || (ts.tv_sec == end && ts.tv_nsec < end_ns))
+    {
+      sleep(1);
+      clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &ts);
+    }

On your target, the sleep() would do nothing, and thus be the same as
what you want (and still be a busy loop).  On most targets, sleep() will
actually sleep the process for 1 second between each call to
clock_gettime(), so the busy loop is less busy, freeing up some CPU
resources for other tasks.

Alternately, use usleep(100000) to sleep in 0.1 sec increments, which
could shave a fraction of a second off the testing time on most targets
by exiting the busy loop sooner.



More information about the Libc-alpha mailing list