user/lib/libc/src/time/clock.c
/**
 * Reads out the current time according to the specified clock id.
 */
Reads out the current time according to the specified clock id.
Reads out the current time according to the specified clock id .
int clock_gettime(clockid_t clockId, struct timespec *tp) {
    if(!tp) {
        errno = EFAULT;
        return -1;
    }
    switch(clockId) {
        case CLOCK_UPTIME_RAW: {
            uint32_t sec1, sec2, nsec;
            __atomic_load(&gTimeInfo->timeSecs, &sec1, __ATOMIC_ACQUIRE);
            __atomic_load(&gTimeInfo->timeNsec, &nsec, __ATOMIC_ACQUIRE);
            __atomic_load(&gTimeInfo->timeSecs2, &sec2, __ATOMIC_ACQUIRE);
            if(sec1 != sec2) {
                nsec = 0;
                sec1 = sec2;
            }
            tp->tv_sec = sec1;
            tp->tv_nsec = nsec;
            return 0;
        }
        default:
            fprintf(stderr, "%s (clock %u) unimplemented\n", __PRETTY_FUNCTION__, clockId);
            errno = EINVAL;
            return -1;
    }
    errno = EINVAL;
    return -1;
}
int clock_gettime ( clockid_t clockId , struct timespec * tp ) { if ( ! tp ) { errno = EFAULT ; return -1 ; } switch ( clockId ) { case CLOCK_UPTIME_RAW : { uint32_t sec1 , sec2 , nsec ; __atomic_load ( & gTimeInfo -> timeSecs , & sec1 , __ATOMIC_ACQUIRE ) ; __atomic_load ( & gTimeInfo -> timeNsec , & nsec , __ATOMIC_ACQUIRE ) ; __atomic_load ( & gTimeInfo -> timeSecs2 , & sec2 , __ATOMIC_ACQUIRE ) ; if ( sec1 != sec2 ) { nsec = 0 ; sec1 = sec2 ; } tp -> tv_sec = sec1 ; tp -> tv_nsec = nsec ; return 0 ; } default : fprintf ( stderr , " \n " , __PRETTY_FUNCTION__ , clockId ) ; errno = EINVAL ; return -1 ; } errno = EINVAL ; return -1 ; }
Reads out the current time according to the specified clock id.
Reads out the current time according to the specified clock id .
// read system uptime from time page // if sec2 is different than sec1, it will be newer, so use it, and reset nsec // TODO: apply an offset via RDTSC // write to output // unsupported clock