commit 58f3b44

shrub  ·  2026-08-05 12:52:32 +0000 UTC
parent 685599f
be portable
1 files changed,  +20, -4
+20, -4
 1@@ -351,11 +351,27 @@ timespec_add_ns(struct timespec *ts, long ns)
 2 static void
 3 sleep_until(const struct timespec *deadline)
 4 {
 5-	int rc;
 6+	struct timespec now;
 7+	struct timespec remaining;
 8 
 9-	while ((rc = clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, deadline,
10-	                             NULL)) != 0) {
11-		if (rc != EINTR) {
12+	for (;;) {
13+		if (clock_gettime(CLOCK_MONOTONIC, &now) != 0) {
14+			return;
15+		}
16+
17+		if (now.tv_sec > deadline->tv_sec ||
18+		    (now.tv_sec == deadline->tv_sec && now.tv_nsec >= deadline->tv_nsec)) {
19+			return;
20+		}
21+
22+		remaining.tv_sec = deadline->tv_sec - now.tv_sec;
23+		remaining.tv_nsec = deadline->tv_nsec - now.tv_nsec;
24+		if (remaining.tv_nsec < 0) {
25+			remaining.tv_nsec += 1000000000L;
26+			remaining.tv_sec--;
27+		}
28+
29+		if (nanosleep(&remaining, NULL) == 0 || errno != EINTR) {
30 			return;
31 		}
32 	}