Skip to content
This repository was archived by the owner on Apr 1, 2026. It is now read-only.
This repository was archived by the owner on Apr 1, 2026. It is now read-only.

in some cases usleep doesn't accept more than 1.000.000 microseconds #174

@aniou

Description

@aniou

When SLEEP_DURATION is defined as 5000 in redshift.c and we call usleep(3) with 1000 multiplier in systemtime.c in systemtime_msleep() then, at least in NetBSD, usleep(3) doesn't work at all due to limit to 1.000.000 microseconds in this call. This leads to high CPU load due to lack of efficient sleep in redshift loop.

Converting to nanosleep(2) fixes this issue, small patch below (more like illustration rather than real solution but I tried to create a drop-in replacement).

--- systemtime.c.orig   2015-02-08 11:10:56.000000000 +0100
+++ systemtime.c        2015-02-08 11:32:16.000000000 +0100
@@ -74,7 +74,16 @@
 systemtime_msleep(unsigned int msecs)
 {
 #ifndef _WIN32
-       usleep(msecs*1000);
+        struct timespec wait;
+
+        if (msecs >= 1000) {
+            wait.tv_sec = (time_t)(msecs / 1000);
+            wait.tv_nsec = 0;
+        } else {
+            wait.tv_sec = 0;
+            wait.tv_nsec = msecs*1000*1000;
+        }
+        nanosleep(&wait, NULL);
 #else
        Sleep(msecs);
 #endif

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions