Skip to content

Instantly share code, notes, and snippets.

@ChrisTX
Created March 13, 2020 17:15
Show Gist options
  • Save ChrisTX/391fe201b3d72d3b3dac17066100347d to your computer and use it in GitHub Desktop.
Save ChrisTX/391fe201b3d72d3b3dac17066100347d to your computer and use it in GitHub Desktop.
WSL clock_nanosleep patch
diff -Naur glibc-2.31/sysdeps/unix/sysv/linux/clock_nanosleep.c glibc-2.31-wsl/sysdeps/unix/sysv/linux/clock_nanosleep.c
--- glibc-2.31/sysdeps/unix/sysv/linux/clock_nanosleep.c 2020-02-01 12:52:50.000000000 +0100
+++ glibc-2.31-wsl/sysdeps/unix/sysv/linux/clock_nanosleep.c 2020-03-13 12:30:30.737587300 +0100
@@ -31,7 +32,25 @@
struct __timespec64 *rem)
{
int r;
-
+ struct __timespec64 current_realtime, actual_req;
+ actual_req = *req;
+ if (clock_id == CLOCK_REALTIME)
+ {
+ clock_id = CLOCK_MONOTONIC;
+ if(flags & TIMER_ABSTIME)
+ {
+ if(__clock_gettime64(CLOCK_REALTIME, &current_realtime))
+ return EINVAL;
+ actual_req.tv_nsec -= current_realtime.tv_nsec;
+ actual_req.tv_sec -= current_realtime.tv_sec;
+ if(actual_req.tv_nsec < 0)
+ {
+ --actual_req.tv_sec;
+ actual_req.tv_nsec = 1000000000L - actual_req.tv_nsec;
+ }
+ flags = flags & ~TIMER_ABSTIME;
+ }
+ }
if (clock_id == CLOCK_THREAD_CPUTIME_ID)
return EINVAL;
if (clock_id == CLOCK_PROCESS_CPUTIME_ID)
@@ -46,11 +65,11 @@
# define __NR_clock_nanosleep_time64 __NR_clock_nanosleep
# endif
r = INTERNAL_SYSCALL_CANCEL (clock_nanosleep_time64, err, clock_id,
- flags, req, rem);
+ flags, &actual_req, rem);
#else
# ifdef __NR_clock_nanosleep_time64
r = INTERNAL_SYSCALL_CANCEL (clock_nanosleep_time64, err, clock_id,
- flags, req, rem);
+ flags, &actual_req, rem);
if (! INTERNAL_SYSCALL_ERROR_P (r, err))
return 0;
@@ -58,14 +77,14 @@
return INTERNAL_SYSCALL_ERRNO (r, err);
# endif /* __NR_clock_nanosleep_time64 */
- if (! in_time_t_range (req->tv_sec))
+ if (! in_time_t_range (actual_req.tv_sec))
{
__set_errno (EOVERFLOW);
return -1;
}
struct timespec tr32;
- struct timespec ts32 = valid_timespec64_to_timespec (*req);
+ struct timespec ts32 = valid_timespec64_to_timespec (actual_req);
r = INTERNAL_SYSCALL_CANCEL (clock_nanosleep, err, clock_id, flags,
&ts32, &tr32);
if (INTERNAL_SYSCALL_ERROR_P (r, err))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment