1/*
2 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11
12#ifndef VPX_PORTS_VPX_TIMER_H_
13#define VPX_PORTS_VPX_TIMER_H_
14#include "vpx/vpx_integer.h"
15
16#if CONFIG_OS_SUPPORT
17
18#if defined(_WIN32)
19/*
20 * Win32 specific includes
21 */
22#ifndef WIN32_LEAN_AND_MEAN
23#define WIN32_LEAN_AND_MEAN
24#endif
25#include <windows.h>
26#else
27/*
28 * POSIX specific includes
29 */
30#include <sys/time.h>
31
32/* timersub is not provided by msys at this time. */
33#ifndef timersub
34#define timersub(a, b, result) \
35  do { \
36    (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
37    (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
38    if ((result)->tv_usec < 0) { \
39      --(result)->tv_sec; \
40      (result)->tv_usec += 1000000; \
41    } \
42  } while (0)
43#endif
44#endif
45
46
47struct vpx_usec_timer {
48#if defined(_WIN32)
49  LARGE_INTEGER  begin, end;
50#else
51  struct timeval begin, end;
52#endif
53};
54
55
56static void
57vpx_usec_timer_start(struct vpx_usec_timer *t) {
58#if defined(_WIN32)
59  QueryPerformanceCounter(&t->begin);
60#else
61  gettimeofday(&t->begin, NULL);
62#endif
63}
64
65
66static void
67vpx_usec_timer_mark(struct vpx_usec_timer *t) {
68#if defined(_WIN32)
69  QueryPerformanceCounter(&t->end);
70#else
71  gettimeofday(&t->end, NULL);
72#endif
73}
74
75
76static int64_t
77vpx_usec_timer_elapsed(struct vpx_usec_timer *t) {
78#if defined(_WIN32)
79  LARGE_INTEGER freq, diff;
80
81  diff.QuadPart = t->end.QuadPart - t->begin.QuadPart;
82
83  QueryPerformanceFrequency(&freq);
84  return diff.QuadPart * 1000000 / freq.QuadPart;
85#else
86  struct timeval diff;
87
88  timersub(&t->end, &t->begin, &diff);
89  return diff.tv_sec * 1000000 + diff.tv_usec;
90#endif
91}
92
93#else /* CONFIG_OS_SUPPORT = 0*/
94
95/* Empty timer functions if CONFIG_OS_SUPPORT = 0 */
96#ifndef timersub
97#define timersub(a, b, result)
98#endif
99
100struct vpx_usec_timer {
101  void *dummy;
102};
103
104static void
105vpx_usec_timer_start(struct vpx_usec_timer *t) { }
106
107static void
108vpx_usec_timer_mark(struct vpx_usec_timer *t) { }
109
110static long
111vpx_usec_timer_elapsed(struct vpx_usec_timer *t) {
112  return 0;
113}
114
115#endif /* CONFIG_OS_SUPPORT */
116
117#endif  // VPX_PORTS_VPX_TIMER_H_
118