timer.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*------------------------------------------------------------------------
  2. * Copyright 2007-2009 (c) Jeff Brown <spadix@users.sourceforge.net>
  3. *
  4. * This file is part of the ZBar Bar Code Reader.
  5. *
  6. * The ZBar Bar Code Reader is free software; you can redistribute it
  7. * and/or modify it under the terms of the GNU Lesser Public License as
  8. * published by the Free Software Foundation; either version 2.1 of
  9. * the License, or (at your option) any later version.
  10. *
  11. * The ZBar Bar Code Reader is distributed in the hope that it will be
  12. * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  13. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser Public License
  17. * along with the ZBar Bar Code Reader; if not, write to the Free
  18. * Software Foundation, Inc., 51 Franklin St, Fifth Floor,
  19. * Boston, MA 02110-1301 USA
  20. *
  21. * http://sourceforge.net/projects/zbar
  22. *------------------------------------------------------------------------*/
  23. #ifndef _ZBAR_TIMER_H_
  24. #define _ZBAR_TIMER_H_
  25. #include <time.h>
  26. //#include <sys/time.h> /* gettimeofday */
  27. /* platform timer abstraction
  28. *
  29. * zbar_timer_t stores the absolute expiration of a delay from
  30. * when the timer was initialized.
  31. *
  32. * _zbar_timer_init() initialized timer with specified ms delay.
  33. * returns timer or NULL if timeout < 0 (no/infinite timeout)
  34. * _zbar_timer_check() returns ms remaining until expiration.
  35. * will be <= 0 if timer has expired
  36. */
  37. #if _POSIX_TIMERS > 0
  38. typedef struct timespec zbar_timer_t;
  39. static inline zbar_timer_t *_zbar_timer_init (zbar_timer_t *timer,
  40. int delay)
  41. {
  42. if(delay < 0)
  43. return(NULL);
  44. clock_gettime(CLOCK_REALTIME, timer);
  45. timer->tv_nsec += (delay % 1000) * 1000000;
  46. timer->tv_sec += (delay / 1000) + (timer->tv_nsec / 1000000000);
  47. timer->tv_nsec %= 1000000000;
  48. return(timer);
  49. }
  50. static inline int _zbar_timer_check (zbar_timer_t *timer)
  51. {
  52. if(!timer)
  53. return(-1);
  54. struct timespec now;
  55. clock_gettime(CLOCK_REALTIME, &now);
  56. int delay = ((timer->tv_sec - now.tv_sec) * 1000 +
  57. (timer->tv_nsec - now.tv_nsec) / 1000000);
  58. return((delay >= 0) ? delay : 0);
  59. }
  60. #elif defined(_WIN32)
  61. # include <windows.h>
  62. typedef DWORD zbar_timer_t;
  63. static inline zbar_timer_t *_zbar_timer_init (zbar_timer_t *timer,
  64. int delay)
  65. {
  66. if(delay < 0)
  67. return(NULL);
  68. *timer = timeGetTime() + delay;
  69. return(timer);
  70. }
  71. static inline int _zbar_timer_check (zbar_timer_t *timer)
  72. {
  73. if(!timer)
  74. return(INFINITE);
  75. int delay = *timer - timeGetTime();
  76. return((delay >= 0) ? delay : 0);
  77. }
  78. #else
  79. typedef struct timeval zbar_timer_t;
  80. static inline zbar_timer_t *_zbar_timer_init (zbar_timer_t *timer,
  81. int delay)
  82. {
  83. #if 0
  84. if(delay < 0)
  85. return(NULL);
  86. gettimeofday(timer, NULL);
  87. timer->tv_usec += (delay % 1000) * 1000;
  88. timer->tv_sec += (delay / 1000) + (timer->tv_usec / 1000000);
  89. timer->tv_usec %= 1000000;
  90. #endif
  91. return(timer);
  92. }
  93. static inline int _zbar_timer_check (zbar_timer_t *timer)
  94. {
  95. #if 0
  96. if(!timer)
  97. return(-1);
  98. struct timeval now;
  99. gettimeofday(&now, NULL);
  100. return((timer->tv_sec - now.tv_sec) * 1000 +
  101. (timer->tv_usec - now.tv_usec) / 1000);
  102. #else
  103. return 0;
  104. #endif
  105. }
  106. #endif
  107. #endif