Debian Codegraph @

src/vendor/azure-uamqp-c/deps/azure-c-shared-utility/adapters/tickcounter_win32.c

raw
1 // Copyright (c) Microsoft. All rights reserved.
2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
4 #include <stdlib.h>
5 #include <windows.h>
6 #include <stdint.h>
7 #include <time.h>
8 #include "azure_c_shared_utility/gballoc.h"
9 #include "azure_c_shared_utility/tickcounter.h"
10 #include "azure_c_shared_utility/optimize_size.h"
11 #include "azure_c_shared_utility/xlogging.h"
12
13 #define INVALID_TIME_VALUE (time_t)(-1)
14 typedef struct TICK_COUNTER_INSTANCE_TAG
15 {
16 LARGE_INTEGER perf_freqency;
17 LARGE_INTEGER last_perf_counter;
18 time_t backup_time_value;
19 tickcounter_ms_t current_ms;
20 } TICK_COUNTER_INSTANCE;
21
22 TICK_COUNTER_HANDLE tickcounter_create(void)
23 {
24 TICK_COUNTER_INSTANCE* result = (TICK_COUNTER_INSTANCE*)malloc(sizeof(TICK_COUNTER_INSTANCE));
25 if (result != NULL)
26 {
27 if (!QueryPerformanceFrequency(&result->perf_freqency))
28 {
29 result->backup_time_value = time(NULL);
30 if (result->backup_time_value == INVALID_TIME_VALUE)
31 {
32 LogError("tickcounter failed: time return INVALID_TIME.");
33 free(result);
34 result = NULL;
35 }
36 else
37 {
38 result->current_ms = 0;
39 }
40 }
41 else
42 {
43 if (!QueryPerformanceCounter(&result->last_perf_counter))
44 {
45 LogError("tickcounter failed: QueryPerformanceCounter failed %d.", GetLastError());
46 free(result);
47 result = NULL;
48 }
49 else
50 {
51 result->backup_time_value = INVALID_TIME_VALUE;
52 result->current_ms = 0;
53 }
54 }
55 }
56 return result;
57 }
58
59 void tickcounter_destroy(TICK_COUNTER_HANDLE tick_counter)
60 {
61 if (tick_counter != NULL)
62 {
63 free(tick_counter);
64 }
65 }
66
67 int tickcounter_get_current_ms(TICK_COUNTER_HANDLE tick_counter, tickcounter_ms_t* current_ms)
68 {
69 int result;
70 if (tick_counter == NULL || current_ms == NULL)
71 {
72 LogError("tickcounter failed: Invalid Arguments.");
73 result = MU_FAILURE;
74 }
75 else
76 {
77 TICK_COUNTER_INSTANCE* tick_counter_instance = (TICK_COUNTER_INSTANCE*)tick_counter;
78 if (tick_counter_instance->backup_time_value == INVALID_TIME_VALUE)
79 {
80 // If the QueryPerformanceCounter is available use this
81 LARGE_INTEGER curr_perf_item;
82 if (!QueryPerformanceCounter(&curr_perf_item))
83 {
84 LogError("tickcounter failed: QueryPerformanceCounter failed %d.", GetLastError() );
85 result = MU_FAILURE;
86 }
87 else
88 {
89 LARGE_INTEGER perf_in_ms;
90 LONGLONG remainder;
91
92 perf_in_ms.QuadPart = (curr_perf_item.QuadPart - tick_counter_instance->last_perf_counter.QuadPart) * 1000000;
93 remainder = (perf_in_ms.QuadPart % tick_counter_instance->perf_freqency.QuadPart) / 1000000;
94 perf_in_ms.QuadPart /= tick_counter_instance->perf_freqency.QuadPart;
95 tick_counter_instance->current_ms += perf_in_ms.QuadPart;
96 tick_counter_instance->last_perf_counter = curr_perf_item;
97 tick_counter_instance->last_perf_counter.QuadPart -= remainder;
98
99 *current_ms = tick_counter_instance->current_ms / 1000;
100 result = 0;
101 }
102 }
103 else
104 {
105 time_t time_value = time(NULL);
106 if (time_value == INVALID_TIME_VALUE)
107 {
108 result = MU_FAILURE;
109 }
110 else
111 {
112 tick_counter_instance->current_ms = (tickcounter_ms_t)(difftime(time_value, tick_counter_instance->backup_time_value) * 1000);
113 *current_ms = tick_counter_instance->current_ms;
114 result = 0;
115 }
116 }
117 }
118 return result;
119 }