root/lib/timer.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. mc_timer_new
  2. mc_timer_destroy
  3. mc_timer_elapsed

   1 /*
   2    Simple timer for the Midnight Commander.
   3 
   4    Copyright (C) 2013-2019
   5    Free Software Foundation, Inc.
   6 
   7    Written by:
   8    Andrew Borodin 2013
   9 
  10    This file is part of the Midnight Commander.
  11 
  12    The Midnight Commander is free software: you can redistribute it
  13    and/or modify it under the terms of the GNU General Public License as
  14    published by the Free Software Foundation, either version 3 of the License,
  15    or (at your option) any later version.
  16 
  17    The Midnight Commander is distributed in the hope that it will be useful,
  18    but WITHOUT ANY WARRANTY; without even the implied warranty of
  19    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20    GNU General Public License for more details.
  21 
  22    You should have received a copy of the GNU General Public License
  23    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  24  */
  25 
  26 /** \file
  27  *  \brief Source: simple timer
  28  */
  29 
  30 #include <config.h>
  31 
  32 #include <sys/time.h>
  33 
  34 #include "lib/global.h"
  35 #include "lib/timer.h"
  36 
  37 /*** global variables ****************************************************************************/
  38 
  39 /**
  40  * mc_timer_t:
  41  *
  42  * Opaque datatype that records a start time.
  43  * #mc_timer_t records a start time, and counts microseconds elapsed since
  44  * that time.
  45  **/
  46 struct mc_timer_t
  47 {
  48     guint64 start;
  49 };
  50 
  51 /*** file scope macro definitions ****************************************************************/
  52 
  53 /*** file scope type declarations ****************************************************************/
  54 
  55 /*** file scope variables ************************************************************************/
  56 
  57 /*** file scope functions ************************************************************************/
  58 
  59 /* --------------------------------------------------------------------------------------------- */
  60 /*** public functions ****************************************************************************/
  61 /* --------------------------------------------------------------------------------------------- */
  62 
  63 /**
  64  * Creates a new timer, and starts timing.
  65  *
  66  * @return: a new #mc_timer_t.
  67  **/
  68 mc_timer_t *
  69 mc_timer_new (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  70 {
  71     mc_timer_t *timer;
  72     struct timeval tv;
  73 
  74     timer = g_new (mc_timer_t, 1);
  75     gettimeofday (&tv, NULL);
  76     timer->start = (guint64) tv.tv_sec * G_USEC_PER_SEC + (guint64) tv.tv_usec;
  77 
  78     return timer;
  79 }
  80 
  81 /* --------------------------------------------------------------------------------------------- */
  82 
  83 /**
  84  * Destroys a timer, freeing associated resources.
  85  *
  86  * @timer: an #mc_timer_t to destroy.
  87  **/
  88 void
  89 mc_timer_destroy (mc_timer_t * timer)
     /* [previous][next][first][last][top][bottom][index][help]  */
  90 {
  91     g_free (timer);
  92 }
  93 
  94 /* --------------------------------------------------------------------------------------------- */
  95 
  96 /**
  97  * Obtains the time since the timer was started.
  98  *
  99  * @timer: an #mc_timer_t.
 100  *
 101  * @return: microseconds elapsed, the time since the timer was started
 102  *
 103  **/
 104 guint64
 105 mc_timer_elapsed (const mc_timer_t * timer)
     /* [previous][next][first][last][top][bottom][index][help]  */
 106 {
 107     struct timeval tv;
 108 
 109     gettimeofday (&tv, NULL);
 110 
 111     return ((guint64) tv.tv_sec * G_USEC_PER_SEC + (guint64) tv.tv_usec - timer->start);
 112 }
 113 
 114 /* --------------------------------------------------------------------------------------------- */

/* [previous][next][first][last][top][bottom][index][help]  */