Blame lib/clock_gettime.c

Packit 0b5880
/*
Packit 0b5880
 * Check: a unit test framework for C
Packit 0b5880
 * Copyright (C) 2001, 2002 Arien Malec
Packit 0b5880
 *
Packit 0b5880
 * This library is free software; you can redistribute it and/or
Packit 0b5880
 * modify it under the terms of the GNU Lesser General Public
Packit 0b5880
 * License as published by the Free Software Foundation; either
Packit 0b5880
 * version 2.1 of the License, or (at your option) any later version.
Packit 0b5880
 *
Packit 0b5880
 * This library is distributed in the hope that it will be useful,
Packit 0b5880
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 0b5880
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 0b5880
 * Lesser General Public License for more details.
Packit 0b5880
 *
Packit 0b5880
 * You should have received a copy of the GNU Lesser General Public
Packit 0b5880
 * License along with this library; if not, write to the
Packit 0b5880
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
Packit 0b5880
 * MA 02110-1301, USA.
Packit 0b5880
 */
Packit 0b5880
Packit 0b5880
#include "libcompat.h"
Packit 0b5880
Packit 0b5880
#ifdef __APPLE__
Packit 0b5880
#include <mach/clock.h>
Packit 0b5880
#include <mach/mach.h>
Packit 0b5880
#include <mach/mach_time.h>
Packit 0b5880
#include <CoreServices/CoreServices.h>
Packit 0b5880
#include <unistd.h>
Packit 0b5880
#endif
Packit 0b5880
Packit 0b5880
#define NANOSECONDS_PER_SECOND 1000000000
Packit 0b5880
Packit 0b5880
Packit 0b5880
Packit 0b5880
int clock_gettime(clockid_t clk_id CK_ATTRIBUTE_UNUSED, struct timespec *ts)
Packit 0b5880
{
Packit 0b5880
Packit 0b5880
#ifdef __APPLE__
Packit 0b5880
    /* Some versions of macOS and iOS do not have clock_gettime, use
Packit 0b5880
     * mach_absolute_time */
Packit 0b5880
Packit 0b5880
    static mach_timebase_info_data_t sTimebaseInfo;
Packit 0b5880
    uint64_t rawTime;
Packit 0b5880
    uint64_t nanos;
Packit 0b5880
Packit 0b5880
    rawTime = mach_absolute_time();
Packit 0b5880
Packit 0b5880
    /*
Packit 0b5880
     * OS X has a function to convert abs time to nano seconds: AbsoluteToNanoseconds
Packit 0b5880
     * However, the function may not be available as we may not have
Packit 0b5880
     * access to CoreServices. Because of this, we convert the abs time
Packit 0b5880
     * to nano seconds manually.
Packit 0b5880
     */
Packit 0b5880
Packit 0b5880
    /*
Packit 0b5880
     * First grab the time base used on the system, if this is the first
Packit 0b5880
     * time we are being called. We can check if the value is uninitialized,
Packit 0b5880
     * as the denominator will be zero. 
Packit 0b5880
     */
Packit 0b5880
    if(sTimebaseInfo.denom == 0)
Packit 0b5880
    {
Packit 0b5880
        (void)mach_timebase_info(&sTimebaseInfo);
Packit 0b5880
    }
Packit 0b5880
Packit 0b5880
    /* 
Packit 0b5880
     * Do the conversion. We hope that the multiplication doesn't 
Packit 0b5880
     * overflow; the price you pay for working in fixed point.
Packit 0b5880
     */
Packit 0b5880
    nanos = rawTime * sTimebaseInfo.numer / sTimebaseInfo.denom;
Packit 0b5880
Packit 0b5880
    /* 
Packit 0b5880
     * Fill in the timespec container 
Packit 0b5880
     */
Packit 0b5880
    ts->tv_sec = nanos / NANOSECONDS_PER_SECOND;
Packit 0b5880
    ts->tv_nsec = nanos - (ts->tv_sec * NANOSECONDS_PER_SECOND);
Packit 0b5880
#else
Packit 0b5880
    /* 
Packit 0b5880
     * As there is no function to fall back onto to get the current
Packit 0b5880
     * time, zero out the time so the caller will have a sane value. 
Packit 0b5880
     */
Packit 0b5880
    ts->tv_sec = 0;
Packit 0b5880
    ts->tv_nsec = 0;
Packit 0b5880
#endif
Packit 0b5880
Packit 0b5880
    return 0;
Packit 0b5880
}