My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/**
* @file BTime.h
* @author Ambroz Bizjak <ambrop7@gmail.com>
*
* @section LICENSE
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the author nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @section DESCRIPTION
*
* System time abstraction used by {@link BReactor}.
*/

#ifndef BADVPN_SYSTEM_BTIME_H
#define BADVPN_SYSTEM_BTIME_H

#if defined(BADVPN_USE_WINAPI)
#include <windows.h>
#elif defined(BADVPN_EMSCRIPTEN)
#include <emscripten/emscripten.h>
#else
#include <time.h>
#include <sys/time.h>
#endif

#include <stdint.h>

#include <misc/debug.h>
#include <misc/overflow.h>
#include <base/BLog.h>

#include <generated/blog_channel_BTime.h>

typedef int64_t btime_t;

#define BTIME_MIN INT64_MIN

struct _BTime_global {
#ifndef NDEBUG
int initialized; // initialized statically
#endif
#if defined(BADVPN_USE_WINAPI)
LARGE_INTEGER start_time;
#elif defined(BADVPN_EMSCRIPTEN)
btime_t start_time;
#else
btime_t start_time;
int use_gettimeofday;
#endif
};

extern struct _BTime_global btime_global;

static void BTime_Init (void)
{
ASSERT(!btime_global.initialized)

#if defined(BADVPN_USE_WINAPI)

ASSERT_FORCE(QueryPerformanceCounter(&btime_global.start_time))

#elif defined(BADVPN_EMSCRIPTEN)

btime_global.start_time = emscripten_get_now();

#else

struct timespec ts;
if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0) {
BLog(BLOG_WARNING, "CLOCK_MONOTONIC is not available. Timers will be confused by clock changes.");

struct timeval tv;
ASSERT_FORCE(gettimeofday(&tv, NULL) == 0)

btime_global.start_time = (int64_t)tv.tv_sec * 1000 + (int64_t)tv.tv_usec/1000;
btime_global.use_gettimeofday = 1;
} else {
btime_global.start_time = (int64_t)ts.tv_sec * 1000 + (int64_t)ts.tv_nsec/1000000;
btime_global.use_gettimeofday = 0;
}

#endif

#ifndef NDEBUG
btime_global.initialized = 1;
#endif
}

static btime_t btime_gettime (void)
{
ASSERT(btime_global.initialized)

#if defined(BADVPN_USE_WINAPI)

LARGE_INTEGER count;
LARGE_INTEGER freq;
ASSERT_FORCE(QueryPerformanceCounter(&count))
ASSERT_FORCE(QueryPerformanceFrequency(&freq))
return (((count.QuadPart - btime_global.start_time.QuadPart) * 1000) / freq.QuadPart);

#elif defined(BADVPN_EMSCRIPTEN)

return (btime_t)emscripten_get_now() - btime_global.start_time;

#else

if (btime_global.use_gettimeofday) {
struct timeval tv;
ASSERT_FORCE(gettimeofday(&tv, NULL) == 0)
return ((int64_t)tv.tv_sec * 1000 + (int64_t)tv.tv_usec/1000);
} else {
struct timespec ts;
ASSERT_FORCE(clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
return (((int64_t)ts.tv_sec * 1000 + (int64_t)ts.tv_nsec/1000000) - btime_global.start_time);
}

#endif
}

static btime_t btime_add (btime_t t1, btime_t t2)
{
// handle overflow
int overflows = add_int64_overflows(t1, t2);
btime_t sum;
if (overflows != 0) {
if (overflows > 0) {
sum = INT64_MAX;
} else {
sum = INT64_MIN;
}
} else {
sum = t1 + t2;
}

return sum;
}

static btime_t btime_getpast (void)
{
return INT64_MIN;
}

#endif

Change log

r1778 by ambrop7 on Oct 22, 2012   Diff
port NCD to Emscripten
Go to: 
Project members, sign in to write a code review

Older revisions

r1672 by ambrop7 on Oct 6, 2012   Diff
system: BTime: add BTIME_MIN
r1061 by ambrop7 on Jan 27, 2012   Diff
switch to using the New BSD License
r810 by ambrop7 on Sep 4, 2011   Diff
system: BTime: add btime_getpast()
All revisions of this file

File info

Size: 4710 bytes, 163 lines
Powered by Google Project Hosting