116 lines
2.8 KiB
C
116 lines
2.8 KiB
C
/* ****************************************************************************
|
|
* utils/timer.c -- timer internals.
|
|
* Copyright (C) 2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
|
|
*
|
|
* This file is part of libcasio.
|
|
* libcasio is free software; you can redistribute it and/or modify it
|
|
* under the terms of the GNU Lesser General Public License as published by
|
|
* the Free Software Foundation; either version 3.0 of the License,
|
|
* or (at your option) any later version.
|
|
*
|
|
* libcasio is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
* See the GNU Lesser General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
* along with libcasio; if not, see <http://www.gnu.org/licenses/>.
|
|
* ************************************************************************* */
|
|
#include "../internals.h"
|
|
|
|
/* ---
|
|
* Default functions for UNIX.
|
|
* --- */
|
|
|
|
#if _POSIX_C_SOURCE >= 199309L
|
|
# include <time.h>
|
|
|
|
struct casio_timer_s {
|
|
clockid_t clk_id;
|
|
struct timespec initial;
|
|
};
|
|
|
|
CASIO_LOCAL int default_get_timer(casio_timer_t **timerp)
|
|
{
|
|
casio_timer_t *timer;
|
|
|
|
if (!(timer = casio_alloc(sizeof(*timer), 1)))
|
|
return (casio_error_alloc);
|
|
|
|
#ifdef __linux__
|
|
timer->clk_id = CLOCK_MONOTONIC_COARSE;
|
|
#else
|
|
timer->clk_id = CLOCK_MONOTONIC;
|
|
#endif
|
|
|
|
if (clock_gettime(timer->clk_id, &timer->initial) < 0) {
|
|
casio_free(timer);
|
|
return (casio_error_unknown);
|
|
}
|
|
|
|
*timerp = timer;
|
|
return (0);
|
|
}
|
|
|
|
CASIO_LOCAL void default_free_timer(casio_timer_t *timer)
|
|
{
|
|
casio_free(timer);
|
|
}
|
|
|
|
CASIO_LOCAL int default_get_spent_time(casio_timer_t *timer,
|
|
unsigned long *spent)
|
|
{
|
|
struct timespec ts;
|
|
|
|
if (clock_gettime(timer->clk_id, &ts) < 0)
|
|
return (casio_error_unknown);
|
|
|
|
*spent = (ts.tv_sec - timer->initial.tv_sec) * 1000
|
|
+ (ts.tv_nsec - timer->initial.tv_nsec) / 1000000;
|
|
return (0);
|
|
}
|
|
|
|
#else
|
|
# define NO_DEFAULTS 1
|
|
#endif
|
|
|
|
/* ---
|
|
* Public API functions.
|
|
* --- */
|
|
|
|
#if NO_DEFAULTS
|
|
CASIO_LOCAL casio_get_timer_t *gettimer = NULL;
|
|
CASIO_LOCAL casio_get_spent_time_t *getspenttime = NULL;
|
|
#else
|
|
CASIO_LOCAL casio_get_timer_t *gettimer = &default_get_timer;
|
|
CASIO_LOCAL casio_get_spent_time_t *getspenttime = &default_get_spent_time;
|
|
#endif
|
|
|
|
#if NO_DEFAULTS || NO_TIMERFREE
|
|
CASIO_LOCAL casio_free_timer_t *freetimer = NULL;
|
|
#else
|
|
CASIO_LOCAL casio_free_timer_t *freetimer = &default_free_timer;
|
|
#endif
|
|
|
|
int CASIO_EXPORT casio_get_timer(casio_timer_t **timerp)
|
|
{
|
|
if (!gettimer)
|
|
return (casio_error_op);
|
|
return ((*gettimer)(timerp));
|
|
}
|
|
|
|
void CASIO_EXPORT casio_free_timer(casio_timer_t *timer)
|
|
{
|
|
if (!freetimer)
|
|
return ;
|
|
(*freetimer)(timer);
|
|
}
|
|
|
|
int CASIO_EXPORT casio_get_spent_time(casio_timer_t *timer,
|
|
unsigned long *spent)
|
|
{
|
|
if (!getspenttime)
|
|
return (casio_error_op);
|
|
return ((*getspenttime)(timer, spent));
|
|
}
|