timer.c 3.1 KB

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