43 lines · cpp
1//===-- Darwin implementation of internal clock_gettime -------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "src/__support/time/clock_gettime.h"10#include "hdr/errno_macros.h" // For EINVAL11#include "hdr/time_macros.h"12#include "hdr/types/struct_timespec.h"13#include "hdr/types/struct_timeval.h"14#include "src/__support/OSUtil/syscall.h" // For syscall_impl15#include "src/__support/common.h"16#include "src/__support/error_or.h"17#include <sys/syscall.h> // For SYS_gettimeofday18#include <sys/time.h> // For struct timezone19 20namespace LIBC_NAMESPACE_DECL {21namespace internal {22 23ErrorOr<int> clock_gettime(clockid_t clockid, struct timespec *ts) {24 if (clockid != CLOCK_REALTIME)25 return Error(EINVAL);26 struct timeval tv;27 // The second argument to gettimeofday is a timezone pointer28 // The third argument is mach_absolute_time29 // Both of these, we don't need here, so they are 030 long ret = LIBC_NAMESPACE::syscall_impl<long>(31 SYS_gettimeofday, reinterpret_cast<long>(&tv), 0, 0);32 if (ret != 0)33 // The syscall returns -1 on error and sets errno.34 return Error(EINVAL);35 36 ts->tv_sec = tv.tv_sec;37 ts->tv_nsec = tv.tv_usec * 1000;38 return 0;39}40 41} // namespace internal42} // namespace LIBC_NAMESPACE_DECL43