refactor thrd_sleep and nanosleep in terms of clock_nanosleep

for namespace-safety with thrd_sleep, this requires an alias, which is
also added. this eliminates all but one direct call point for
nanosleep syscalls, and arranges that 64-bit time_t conversion logic
will only need to exist in one file rather than three.

as a bonus, clock_nanosleep with CLOCK_REALTIME and empty flags is now
implemented as SYS_nanosleep, thereby working on older kernels that
may lack POSIX clocks functionality.
This commit is contained in:
Rich Felker 2019-07-21 01:53:14 -04:00
parent 0ce49d0a30
commit 331993e3fc
4 changed files with 11 additions and 5 deletions

View File

@ -4,6 +4,7 @@
#include "../../include/time.h"
hidden int __clock_gettime(clockid_t, struct timespec *);
hidden int __clock_nanosleep(clockid_t, int, const struct timespec *, struct timespec *);
hidden char *__asctime_r(const struct tm *, char *);
hidden struct tm *__gmtime_r(const time_t *restrict, struct tm *restrict);

View File

@ -1,10 +1,11 @@
#include <threads.h>
#include <time.h>
#include <errno.h>
#include "syscall.h"
int thrd_sleep(const struct timespec *req, struct timespec *rem)
{
int ret = __syscall(SYS_nanosleep, req, rem);
int ret = -__clock_nanosleep(CLOCK_REALTIME, 0, req, rem);
switch (ret) {
case 0: return 0;
case -EINTR: return -1; /* value specified by C11 */

View File

@ -2,8 +2,12 @@
#include <errno.h>
#include "syscall.h"
int clock_nanosleep(clockid_t clk, int flags, const struct timespec *req, struct timespec *rem)
int __clock_nanosleep(clockid_t clk, int flags, const struct timespec *req, struct timespec *rem)
{
int r = -__syscall_cp(SYS_clock_nanosleep, clk, flags, req, rem);
return clk == CLOCK_THREAD_CPUTIME_ID ? EINVAL : r;
if (clk == CLOCK_THREAD_CPUTIME_ID) return EINVAL;
if (clk == CLOCK_REALTIME && !flags)
return -__syscall_cp(SYS_nanosleep, req, rem);
return -__syscall_cp(SYS_clock_nanosleep, clk, flags, req, rem);
}
weak_alias(__clock_nanosleep, clock_nanosleep);

View File

@ -3,5 +3,5 @@
int nanosleep(const struct timespec *req, struct timespec *rem)
{
return syscall_cp(SYS_nanosleep, req, rem);
return __syscall_ret(-__clock_nanosleep(CLOCK_REALTIME, 0, req, rem));
}