Skip to content

Commit 95b9ab0

Browse files
fix: Trying to fix thread::sleep
1 parent 405c181 commit 95b9ab0

1 file changed

Lines changed: 20 additions & 19 deletions

File tree

src/vibrant/thread.h

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -907,36 +907,37 @@ inline void original::thread::sleep(const time::duration& d)
907907
if (d.value() < 0)
908908
return;
909909

910-
#if ORIGINAL_COMPILER_GCC || ORIGINAL_COMPILER_CLANG
910+
#if defined(ORIGINAL_PLATFORM_LINUX)
911911
const timespec ts = d.toTimespec();
912-
errno = 0;
913-
914-
#if ORIGINAL_PLATFORM_MACOS || ORIGINAL_PLATFORM_WINDOWS
915-
timespec rem = ts;
916-
int code;
917-
918-
do {
919-
code = nanosleep(&rem, &rem);
920-
} while (code == -1 && errno == EINTR);
921-
922-
if (code != 0)
923-
throw sysError("Failed to sleep thread (nanosleep returned " +
924-
formatString(code) + ", errno: " + formatString(errno) + ").");
925-
#endif
926-
927-
#elif ORIGINAL_PLATFORM_LINUX
928912
int code = clock_nanosleep(CLOCK_REALTIME, 0, &ts, nullptr);
913+
929914
if (code != 0 && (errno == EINVAL || errno == ENOSYS)) {
930915
code = clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, nullptr);
931916
}
917+
932918
if (code != 0)
933919
throw sysError("Failed to sleep thread (clock_nanosleep returned " +
934920
formatString(code) + ", errno: " + formatString(errno) + ").");
921+
#elif defined(ORIGINAL_PLATFORM_MACOS)
922+
const timespec ts = d.toTimespec();
923+
timespec rem = ts;
924+
int code;
925+
926+
do {
927+
errno = 0;
928+
code = nanosleep(&rem, &rem);
929+
} while (code == -1 && errno == EINTR);
930+
931+
if (code != 0)
932+
throw sysError("Failed to sleep thread (nanosleep returned " +
933+
formatString(code) + ", errno: " + formatString(errno) + ").");
934+
#elif defined(ORIGINAL_PLATFORM_WINDOWS)
935935

936-
#elif ORIGINAL_COMPILER_MSVC
937936
Sleep(d.toDWMilliseconds());
938-
#endif
939937

938+
#else
939+
#error Unsupported platform
940+
#endif
940941
}
941942

942943
inline original::thread::thread()

0 commit comments

Comments
 (0)