blob: c6b25c26b2ea43b92b0c9ea899783a4e8cf1a77f [file] [log] [blame]
#pragma once
#define __NEED_struct_timespec
#include "threads_impl.h"
#include <bits/alltypes.h>
#include <zircon/time.h>
#include <zircon/types.h>
static inline zx_time_t __duration_timespec_to_deadline(const struct timespec timespec) {
zx_duration_t nanos = zx_duration_add_duration(
zx_duration_mul_int64(1000000000ll, timespec.tv_sec),
timespec.tv_nsec);
return _zx_deadline_after(nanos);
}
static inline int __timespec_to_deadline(const struct timespec* timespec,
clockid_t clk, zx_time_t* deadline) {
struct timespec to;
if (timespec->tv_nsec >= ZX_SEC(1))
return EINVAL;
if (__clock_gettime(clk, &to))
return EINVAL;
to.tv_sec = timespec->tv_sec - to.tv_sec;
if ((to.tv_nsec = timespec->tv_nsec - to.tv_nsec) < 0) {
to.tv_sec--;
to.tv_nsec += ZX_SEC(1);
}
if (to.tv_sec < 0)
return ETIMEDOUT;
*deadline = __duration_timespec_to_deadline(to);
return 0;
}