amqp_time.c

filter: Statementstmt 36.5% 34 / 93 Branchbran 28.5% 16 / 56 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 33.5% 50 / 149 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« perl_math_int64.c ↑ Summary amqp_tcp_socket.c »
1 /* vim:set ft=c ts=2 sw=2 sts=2 et cindent: */
2 /*
3 * Portions created by Alan Antonuk are Copyright (c) 2013-2014 Alan Antonuk.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 */
24 #include "amqp.h"
25 #include "amqp_time.h"
26 #include <assert.h>
27 #include <limits.h>
28 #include <string.h>
29  
30 #if (defined(_WIN32) || defined(__WIN32__) || defined(WIN32))
31 # define AMQP_WIN_TIMER_API
32 #elif (defined(machintosh) || defined(__APPLE__) || defined(__APPLE_CC__))
33 # define AMQP_MAC_TIMER_API
34 #else
35 # define AMQP_POSIX_TIMER_API
36 #endif
37  
38  
39 #ifdef AMQP_WIN_TIMER_API
40 #define WIN32_LEAN_AND_MEAN
41 #include <Windows.h>
42  
43 uint64_t
44 amqp_get_monotonic_timestamp(void)
45 {
46 static double NS_PER_COUNT = 0;
47 LARGE_INTEGER perf_count;
48  
49 if (0 == NS_PER_COUNT) {
50 LARGE_INTEGER perf_frequency;
51 if (!QueryPerformanceFrequency(&perf_frequency)) {
52 return 0;
53 }
54 NS_PER_COUNT = (double)AMQP_NS_PER_S / perf_frequency.QuadPart;
55 }
56  
57 if (!QueryPerformanceCounter(&perf_count)) {
58 return 0;
59 }
60  
61 return (uint64_t)(perf_count.QuadPart * NS_PER_COUNT);
62 }
63 #endif /* AMQP_WIN_TIMER_API */
64  
65 #ifdef AMQP_MAC_TIMER_API
66 # include <mach/mach_time.h>
67  
68 uint64_t
69 amqp_get_monotonic_timestamp(void)
70 {
71 static mach_timebase_info_data_t s_timebase = {0, 0};
72 uint64_t timestamp;
73  
74 timestamp = mach_absolute_time();
75  
76 if (s_timebase.denom == 0) {
77 mach_timebase_info(&s_timebase);
78 if (0 == s_timebase.denom) {
79 return 0;
80 }
81 }
82  
83 timestamp *= (uint64_t)s_timebase.numer;
84 timestamp /= (uint64_t)s_timebase.denom;
85  
86 return timestamp;
87 }
88 #endif /* AMQP_MAC_TIMER_API */
89  
90 #ifdef AMQP_POSIX_TIMER_API
91 #include <time.h>
92  
93 uint64_t
94 2 amqp_get_monotonic_timestamp(void)
95 {
96 #ifdef __hpux
97 return (uint64_t)gethrtime();
98 #else
99 struct timespec tp;
100 2 ▶ if (-1 == clock_gettime(CLOCK_MONOTONIC, &tp)) {
Branch: 1 branch
BranchTrueFalseTotal
if (-1 == clock_gettime(CLOCK_MONOTONIC, &tp)) { 0 2 2
101 0 return 0;
102 }
103  
104 2 return ((uint64_t)tp.tv_sec * AMQP_NS_PER_S + (uint64_t)tp.tv_nsec);
105 #endif
106 }
107 #endif /* AMQP_POSIX_TIMER_API */
108  
109 30 int amqp_time_from_now(amqp_time_t *time, struct timeval *timeout) {
110 uint64_t now_ns;
111 uint64_t delta_ns;
112  
113 30 ▶ assert(NULL != time);
Branch: 1 branch
BranchTrueFalseTotal
assert(NULL != time); 0 30 30
114  
115 30 ▶ if (NULL == timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if (NULL == timeout) { 29 1 30
116 29 *time = amqp_time_infinite();
117 29 return AMQP_STATUS_OK;
118 }
119 1 ▶ if (0 == timeout->tv_sec && 0 == timeout->tv_usec) {
Branch: 2 branches
BranchTrueFalseTotal
if (0 == timeout->tv_sec && 0 == timeout->tv_usec) { 0 1 1
if (0 == timeout->tv_sec && 0 == timeout->tv_usec) { 0 0 0
120 0 *time = amqp_time_immediate();
121 0 return AMQP_STATUS_OK;
122 }
123  
124 1 ▶ if (timeout->tv_sec < 0 || timeout->tv_usec < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (timeout->tv_sec < 0 || timeout->tv_usec < 0) { 1 0 1
if (timeout->tv_sec < 0 || timeout->tv_usec < 0) { 0 1 1
125 0 return AMQP_STATUS_INVALID_PARAMETER;
126 }
127  
128 1 delta_ns = (uint64_t)timeout->tv_sec * AMQP_NS_PER_S +
129 1 (uint64_t)timeout->tv_usec * AMQP_NS_PER_US;
130  
131 1 now_ns = amqp_get_monotonic_timestamp();
132 1 ▶ if (0 == now_ns) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == now_ns) { 0 1 1
133 0 return AMQP_STATUS_TIMER_FAILURE;
134 }
135  
136 1 time->time_point_ns = now_ns + delta_ns;
137 1 ▶ if (now_ns > time->time_point_ns || delta_ns > time->time_point_ns) {
Branch: 2 branches
BranchTrueFalseTotal
if (now_ns > time->time_point_ns || delta_ns > time->time_point_ns) { 1 0 1
if (now_ns > time->time_point_ns || delta_ns > time->time_point_ns) { 0 1 1
138 0 return AMQP_STATUS_INVALID_PARAMETER;
139 }
140  
141 1 return AMQP_STATUS_OK;
142 }
143  
144 54 int amqp_time_s_from_now(amqp_time_t *time, int seconds) {
145 uint64_t now_ns;
146 uint64_t delta_ns;
147 54 ▶ assert(NULL != time);
Branch: 1 branch
BranchTrueFalseTotal
assert(NULL != time); 0 54 54
148  
149 54 ▶ if (0 >= seconds) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 >= seconds) { 54 0 54
150 54 *time = amqp_time_infinite();
151 54 return AMQP_STATUS_OK;
152 }
153  
154 0 now_ns = amqp_get_monotonic_timestamp();
155 0 ▶ if (0 == now_ns) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == now_ns) { 0 0 0
156 0 return AMQP_STATUS_TIMER_FAILURE;
157 }
158  
159 0 delta_ns = (uint64_t)seconds * AMQP_NS_PER_S;
160 0 time->time_point_ns = now_ns + delta_ns;
161 0 ▶ if (now_ns > time->time_point_ns || delta_ns > time->time_point_ns) {
Branch: 2 branches
BranchTrueFalseTotal
if (now_ns > time->time_point_ns || delta_ns > time->time_point_ns) { 0 0 0
if (now_ns > time->time_point_ns || delta_ns > time->time_point_ns) { 0 0 0
162 0 return AMQP_STATUS_INVALID_PARAMETER;
163 }
164  
165 0 return AMQP_STATUS_OK;
166 }
167  
168 0 amqp_time_t amqp_time_immediate(void) {
169 amqp_time_t time;
170 0 time.time_point_ns = 0;
171 0 return time;
172 }
173  
174 83 amqp_time_t amqp_time_infinite(void) {
175 amqp_time_t time;
176 83 time.time_point_ns = UINT64_MAX;
177 83 return time;
178 }
179  
180 1 int amqp_time_ms_until(amqp_time_t time) {
181 uint64_t now_ns;
182 uint64_t delta_ns;
183 int left_ms;
184  
185 1 ▶ if (UINT64_MAX == time.time_point_ns) {
Branch: 1 branch
BranchTrueFalseTotal
if (UINT64_MAX == time.time_point_ns) { 0 1 1
186 0 return -1;
187 }
188 1 ▶ if (0 == time.time_point_ns) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == time.time_point_ns) { 0 1 1
189 0 return 0;
190 }
191  
192 1 now_ns = amqp_get_monotonic_timestamp();
193 1 ▶ if (0 == now_ns) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == now_ns) { 0 1 1
194 0 return AMQP_STATUS_TIMER_FAILURE;
195 }
196  
197 1 ▶ if (now_ns >= time.time_point_ns) {
Branch: 1 branch
BranchTrueFalseTotal
if (now_ns >= time.time_point_ns) { 0 1 1
198 0 return 0;
199 }
200  
201 1 delta_ns = time.time_point_ns - now_ns;
202 1 left_ms = (int)(delta_ns / AMQP_NS_PER_MS);
203  
204 1 return left_ms;
205 }
206  
207 0 int amqp_time_tv_until(amqp_time_t time, struct timeval *in,
208 struct timeval **out) {
209 uint64_t now_ns;
210 uint64_t delta_ns;
211  
212 0 ▶ assert(in != NULL);
Branch: 1 branch
BranchTrueFalseTotal
assert(in != NULL); 0 0 0
213 0 ▶ if (UINT64_MAX == time.time_point_ns) {
Branch: 1 branch
BranchTrueFalseTotal
if (UINT64_MAX == time.time_point_ns) { 0 0 0
214 0 *out = NULL;
215 0 return AMQP_STATUS_OK;
216 }
217 0 ▶ if (0 == time.time_point_ns) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == time.time_point_ns) { 0 0 0
218 0 in->tv_sec = 0;
219 0 in->tv_usec = 0;
220 0 *out = in;
221 0 return AMQP_STATUS_OK;
222 }
223  
224 0 now_ns = amqp_get_monotonic_timestamp();
225 0 ▶ if (0 == now_ns) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == now_ns) { 0 0 0
226 0 return AMQP_STATUS_TIMER_FAILURE;
227 }
228  
229 0 ▶ if (now_ns >= time.time_point_ns) {
Branch: 1 branch
BranchTrueFalseTotal
if (now_ns >= time.time_point_ns) { 0 0 0
230 0 in->tv_sec = 0;
231 0 in->tv_usec = 0;
232 0 *out = in;
233 0 return AMQP_STATUS_OK;
234 }
235  
236 0 delta_ns = time.time_point_ns - now_ns;
237 0 in->tv_sec = (int)(delta_ns / AMQP_NS_PER_S);
238 0 in->tv_usec = (int)((delta_ns % AMQP_NS_PER_S) / AMQP_NS_PER_US);
239 0 *out = in;
240  
241 0 return AMQP_STATUS_OK;
242 }
243  
244 0 int amqp_time_has_past(amqp_time_t time) {
245 uint64_t now_ns;
246 0 ▶ if (UINT64_MAX == time.time_point_ns) {
Branch: 1 branch
BranchTrueFalseTotal
if (UINT64_MAX == time.time_point_ns) { 0 0 0
247 0 return AMQP_STATUS_OK;
248 }
249  
250 0 now_ns = amqp_get_monotonic_timestamp();
251 0 ▶ if (0 == now_ns) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == now_ns) { 0 0 0
252 0 return AMQP_STATUS_TIMER_FAILURE;
253 }
254  
255 0 ▶ if (now_ns > time.time_point_ns) {
Branch: 1 branch
BranchTrueFalseTotal
if (now_ns > time.time_point_ns) { 0 0 0
256 0 return AMQP_STATUS_TIMEOUT;
257 }
258 0 return AMQP_STATUS_OK;
259 }
260  
261 0 amqp_time_t amqp_time_first(amqp_time_t l, amqp_time_t r) {
262 0 ▶ if (l.time_point_ns < r.time_point_ns) {
Branch: 1 branch
BranchTrueFalseTotal
if (l.time_point_ns < r.time_point_ns) { 0 0 0
263 0 return l;
264 }
265 0 return r;
266 }
267  
268 0 int amqp_time_equal(amqp_time_t l, amqp_time_t r) {
269 0 return l.time_point_ns == r.time_point_ns;
270 }
« perl_math_int64.c ↑ Summary amqp_tcp_socket.c »