Merge branch '3821_init_st_mtim'
[midnight-commander.git] / lib / timer.c
blobddcd5c95435e8d0be302c5f03b8e246e1e106911
1 /*
2 Simple timer for the Midnight Commander.
4 Copyright (C) 2013-2017
5 Free Software Foundation, Inc.
7 Written by:
8 Andrew Borodin 2013
10 This file is part of the Midnight Commander.
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.
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.
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/>.
26 /** \file
27 * \brief Source: simple timer
30 #include <config.h>
32 #include <sys/time.h>
34 #include "lib/global.h"
35 #include "lib/timer.h"
37 /*** global variables ****************************************************************************/
39 /**
40 * mc_timer_t:
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
48 guint64 start;
51 /*** file scope macro definitions ****************************************************************/
53 /*** file scope type declarations ****************************************************************/
55 /*** file scope variables ************************************************************************/
57 /*** file scope functions ************************************************************************/
59 /* --------------------------------------------------------------------------------------------- */
60 /*** public functions ****************************************************************************/
61 /* --------------------------------------------------------------------------------------------- */
63 /**
64 * Creates a new timer, and starts timing.
66 * @return: a new #mc_timer_t.
67 **/
68 mc_timer_t *
69 mc_timer_new (void)
71 mc_timer_t *timer;
72 struct timeval tv;
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;
78 return timer;
81 /* --------------------------------------------------------------------------------------------- */
83 /**
84 * Destroys a timer, freeing associated resources.
86 * @timer: an #mc_timer_t to destroy.
87 **/
88 void
89 mc_timer_destroy (mc_timer_t * timer)
91 g_free (timer);
94 /* --------------------------------------------------------------------------------------------- */
96 /**
97 * Obtains the time since the timer was started.
99 * @timer: an #mc_timer_t.
101 * @return: microseconds elapsed, the time since the timer was started
104 guint64
105 mc_timer_elapsed (const mc_timer_t * timer)
107 struct timeval tv;
109 gettimeofday (&tv, NULL);
111 return ((guint64) tv.tv_sec * G_USEC_PER_SEC + (guint64) tv.tv_usec - timer->start);
114 /* --------------------------------------------------------------------------------------------- */