| 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 "azure_c_shared_utility/gballoc.h" |
| 6 |
#include <stdint.h> |
| 7 |
#include "azure_c_shared_utility/tickcounter.h" |
| 8 |
#include "azure_c_shared_utility/optimize_size.h" |
| 9 |
#include "azure_c_shared_utility/xlogging.h" |
| 10 |
#include "linux_time.h" |
| 11 |
|
| 12 |
|
| 13 |
typedef struct TICK_COUNTER_INSTANCE_TAG |
| 14 |
{ |
| 15 |
int64_t init_time_value; |
| 16 |
tickcounter_ms_t current_ms; |
| 17 |
} TICK_COUNTER_INSTANCE; |
| 18 |
|
| 19 |
TICK_COUNTER_HANDLE tickcounter_create(void) |
| 20 |
{ |
| 21 |
TICK_COUNTER_INSTANCE* result = (TICK_COUNTER_INSTANCE*)malloc(sizeof(TICK_COUNTER_INSTANCE)); |
| 22 |
if (result != NULL) |
| 23 |
{ |
| 24 |
set_time_basis(); |
| 25 |
|
| 26 |
result->init_time_value = get_time_ms(); |
| 27 |
if (result->init_time_value == INVALID_TIME_VALUE) |
| 28 |
{ |
| 29 |
LogError("tickcounter failed: time return INVALID_TIME."); |
| 30 |
free(result); |
| 31 |
result = NULL; |
| 32 |
} |
| 33 |
else |
| 34 |
{ |
| 35 |
result->current_ms = 0; |
| 36 |
} |
| 37 |
} |
| 38 |
return result; |
| 39 |
} |
| 40 |
|
| 41 |
void tickcounter_destroy(TICK_COUNTER_HANDLE tick_counter) |
| 42 |
{ |
| 43 |
if (tick_counter != NULL) |
| 44 |
{ |
| 45 |
free(tick_counter); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
int tickcounter_get_current_ms(TICK_COUNTER_HANDLE tick_counter, tickcounter_ms_t * current_ms) |
| 50 |
{ |
| 51 |
int result; |
| 52 |
|
| 53 |
if (tick_counter == NULL || current_ms == NULL) |
| 54 |
{ |
| 55 |
LogError("tickcounter failed: Invalid Arguments."); |
| 56 |
result = MU_FAILURE; |
| 57 |
} |
| 58 |
else |
| 59 |
{ |
| 60 |
int64_t time_value = get_time_ms(); |
| 61 |
if (time_value == INVALID_TIME_VALUE) |
| 62 |
{ |
| 63 |
result = MU_FAILURE; |
| 64 |
} |
| 65 |
else |
| 66 |
{ |
| 67 |
TICK_COUNTER_INSTANCE* tick_counter_instance = (TICK_COUNTER_INSTANCE*)tick_counter; |
| 68 |
tick_counter_instance->current_ms = (tickcounter_ms_t) time_value - tick_counter_instance->init_time_value; |
| 69 |
*current_ms = tick_counter_instance->current_ms; |
| 70 |
result = 0; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
return result; |
| 75 |
} |