Path: blob/master/Utilities/cmlibuv/src/unix/posix-hrtime.c
3156 views
/* Copyright libuv project contributors. All rights reserved.1*2* Permission is hereby granted, free of charge, to any person obtaining a copy3* of this software and associated documentation files (the "Software"), to4* deal in the Software without restriction, including without limitation the5* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or6* sell copies of the Software, and to permit persons to whom the Software is7* furnished to do so, subject to the following conditions:8*9* The above copyright notice and this permission notice shall be included in10* all copies or substantial portions of the Software.11*12* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR13* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,14* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE15* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER16* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING17* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS18* IN THE SOFTWARE.19*/2021#include "uv.h"22#include "internal.h"2324#if defined(__APPLE__)25/* Special case for CMake bootstrap: no clock_gettime on macOS < 10.12 */2627#ifndef CMAKE_BOOTSTRAP28#error "This code path meant only for use during CMake bootstrap."29#endif3031#include <mach/mach.h>32#include <mach/mach_time.h>3334uint64_t uv__hrtime(uv_clocktype_t type) {35static mach_timebase_info_data_t info;3637if ((ACCESS_ONCE(uint32_t, info.numer) == 0 ||38ACCESS_ONCE(uint32_t, info.denom) == 0) &&39mach_timebase_info(&info) != KERN_SUCCESS)40abort();4142return mach_absolute_time() * info.numer / info.denom;43}4445#elif defined(__hpux)46/* Special case for CMake bootstrap: no CLOCK_MONOTONIC on HP-UX */4748#ifndef CMAKE_BOOTSTRAP49#error "This code path meant only for use during CMake bootstrap."50#endif5152#include <stdint.h>53#include <time.h>5455uint64_t uv__hrtime(uv_clocktype_t type) {56return (uint64_t) gethrtime();57}5859#else6061#include <stdint.h>62#include <time.h>6364#undef NANOSEC65#define NANOSEC ((uint64_t) 1e9)6667uint64_t uv__hrtime(uv_clocktype_t type) {68struct timespec ts;69clock_gettime(CLOCK_MONOTONIC, &ts);70return (((uint64_t) ts.tv_sec) * NANOSEC + ts.tv_nsec);71}7273#endif747576