diff --git a/lib/Makefile.am b/lib/Makefile.am index 18bf8362b..db231fc85 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -40,7 +40,8 @@ libmc_la_SOURCES = \ keybind.c keybind.h \ lock.c lock.h \ serialize.c serialize.h \ - timefmt.c timefmt.h + timefmt.c timefmt.h \ + timer.c timer.h if USE_MAINTAINER_MODE libmc_la_SOURCES += logging.c logging.h diff --git a/lib/timer.c b/lib/timer.c new file mode 100644 index 000000000..53a2f00a0 --- /dev/null +++ b/lib/timer.c @@ -0,0 +1,114 @@ +/* + Simple timer for the Midnight Commander. + + Copyright (C) 2013-2014 + Free Software Foundation, Inc. + + Written by: + Andrew Borodin 2013 + + This file is part of the Midnight Commander. + + The Midnight Commander is free software: you can redistribute it + and/or modify it under the terms of the GNU General Public License as + published by the Free Software Foundation, either version 3 of the License, + or (at your option) any later version. + + The Midnight Commander 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 General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + */ + +/** \file + * \brief Source: simple timer + */ + +#include + +#include + +#include "lib/global.h" +#include "lib/timer.h" + +/*** global variables ****************************************************************************/ + +/** + * mc_timer_t: + * + * Opaque datatype that records a start time. + * #mc_timer_t records a start time, and counts microseconds elapsed since + * that time. + **/ +struct mc_timer_t +{ + guint64 start; +}; + +/*** file scope macro definitions ****************************************************************/ + +/*** file scope type declarations ****************************************************************/ + +/*** file scope variables ************************************************************************/ + +/*** file scope functions ************************************************************************/ + +/* --------------------------------------------------------------------------------------------- */ +/*** public functions ****************************************************************************/ +/* --------------------------------------------------------------------------------------------- */ + +/** + * Creates a new timer, and starts timing. + * + * @return: a new #mc_timer_t. + **/ +mc_timer_t * +mc_timer_new (void) +{ + mc_timer_t *timer; + struct timeval tv; + + timer = g_new (mc_timer_t, 1); + gettimeofday (&tv, NULL); + timer->start = (guint64) tv.tv_sec * G_USEC_PER_SEC + (guint64) tv.tv_usec; + + return timer; +} + +/* --------------------------------------------------------------------------------------------- */ + +/** + * Destroys a timer, freeing associated resources. + * + * @timer: an #mc_timer_t to destroy. + **/ +void +mc_timer_destroy (mc_timer_t * timer) +{ + g_free (timer); +} + +/* --------------------------------------------------------------------------------------------- */ + +/** + * Obtains the time since the timer was started. + * + * @timer: an #mc_timer_t. + * + * @return: microseconds elapsed, the time since the timer was started + * + **/ +guint64 +mc_timer_elapsed (const mc_timer_t * timer) +{ + struct timeval tv; + + gettimeofday (&tv, NULL); + + return ((guint64) tv.tv_sec * G_USEC_PER_SEC + (guint64) tv.tv_usec - timer->start); +} + +/* --------------------------------------------------------------------------------------------- */ diff --git a/lib/timer.h b/lib/timer.h new file mode 100644 index 000000000..4cc823260 --- /dev/null +++ b/lib/timer.h @@ -0,0 +1,27 @@ +/** \file timer.h + * \brief Header: simple timer + */ + +#ifndef MC_TIMER_H +#define MC_TIMER_H + +/*** typedefs(not structures) and defined constants **********************************************/ + +/*** enums ***************************************************************************************/ + +/*** structures declarations (and typedefs of structures)*****************************************/ + +struct mc_timer_t; +typedef struct mc_timer_t mc_timer_t; + +/*** global variables defined in .c file *********************************************************/ + +/*** declarations of public functions ************************************************************/ + +mc_timer_t *mc_timer_new (void); +void mc_timer_destroy (mc_timer_t * timer); +guint64 mc_timer_elapsed (const mc_timer_t * timer); + +/*** inline functions **************************************************/ + +#endif /* MC_TIMER_H */