timer.c (3743B)
1 /* 2 * libdivecomputer 3 * 4 * Copyright (C) 2018 Jef Driesen 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License as published by the Free Software Foundation; either 9 * version 2.1 of the License, or (at your option) any later version. 10 * 11 * This library is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with this library; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 19 * MA 02110-1301 USA 20 */ 21 22 #ifdef HAVE_CONFIG_H 23 #include "config.h" 24 #endif 25 26 #include <stdlib.h> 27 28 #ifdef _WIN32 29 #define WIN32_LEAN_AND_MEAN 30 #define NOGDI 31 #include <windows.h> 32 #else 33 #include <time.h> 34 #include <sys/time.h> 35 #ifdef HAVE_MACH_MACH_TIME_H 36 #include <mach/mach_time.h> 37 #endif 38 #endif 39 40 #include "timer.h" 41 42 struct dc_timer_t { 43 #if defined (_WIN32) 44 LARGE_INTEGER timestamp; 45 LARGE_INTEGER frequency; 46 #elif defined (HAVE_CLOCK_GETTIME) 47 struct timespec timestamp; 48 #elif defined (HAVE_MACH_ABSOLUTE_TIME) 49 uint64_t timestamp; 50 mach_timebase_info_data_t info; 51 #else 52 struct timeval timestamp; 53 #endif 54 }; 55 56 dc_status_t 57 dc_timer_new (dc_timer_t **out) 58 { 59 dc_timer_t *timer = NULL; 60 61 if (out == NULL) 62 return DC_STATUS_INVALIDARGS; 63 64 timer = (dc_timer_t *) malloc (sizeof (dc_timer_t)); 65 if (timer == NULL) { 66 return DC_STATUS_NOMEMORY; 67 } 68 69 #if defined (_WIN32) 70 if (!QueryPerformanceFrequency(&timer->frequency) || 71 !QueryPerformanceCounter(&timer->timestamp)) { 72 free(timer); 73 return DC_STATUS_IO; 74 } 75 #elif defined (HAVE_CLOCK_GETTIME) 76 if (clock_gettime(CLOCK_MONOTONIC, &timer->timestamp) != 0) { 77 free(timer); 78 return DC_STATUS_IO; 79 } 80 #elif defined (HAVE_MACH_ABSOLUTE_TIME) 81 if (mach_timebase_info(&timer->info) != KERN_SUCCESS) { 82 free(timer); 83 return DC_STATUS_IO; 84 } 85 86 timer->timestamp = mach_absolute_time(); 87 #else 88 if (gettimeofday (&timer->timestamp, NULL) != 0) { 89 free(timer); 90 return DC_STATUS_IO; 91 } 92 #endif 93 94 *out = timer; 95 96 return DC_STATUS_SUCCESS; 97 } 98 99 dc_status_t 100 dc_timer_now (dc_timer_t *timer, dc_usecs_t *usecs) 101 { 102 dc_status_t status = DC_STATUS_SUCCESS; 103 dc_usecs_t value = 0; 104 105 if (timer == NULL) { 106 status = DC_STATUS_INVALIDARGS; 107 goto out; 108 } 109 110 #if defined (_WIN32) 111 LARGE_INTEGER now; 112 if (!QueryPerformanceCounter(&now)) { 113 status = DC_STATUS_IO; 114 goto out; 115 } 116 117 value = (now.QuadPart - timer->timestamp.QuadPart) * 1000000 / timer->frequency.QuadPart; 118 #elif defined (HAVE_CLOCK_GETTIME) 119 struct timespec now, delta; 120 if (clock_gettime(CLOCK_MONOTONIC, &now) != 0) { 121 status = DC_STATUS_IO; 122 goto out; 123 } 124 125 if (now.tv_nsec < timer->timestamp.tv_nsec) { 126 delta.tv_nsec = 1000000000 + now.tv_nsec - timer->timestamp.tv_nsec; 127 delta.tv_sec = now.tv_sec - timer->timestamp.tv_sec - 1; 128 } else { 129 delta.tv_nsec = now.tv_nsec - timer->timestamp.tv_nsec; 130 delta.tv_sec = now.tv_sec - timer->timestamp.tv_sec; 131 } 132 133 value = (dc_usecs_t) delta.tv_sec * 1000000 + delta.tv_nsec / 1000; 134 #elif defined (HAVE_MACH_ABSOLUTE_TIME) 135 uint64_t now = mach_absolute_time(); 136 value = (now - timer->timestamp) * timer->info.numer / (timer->info.denom * 1000); 137 #else 138 struct timeval now, delta; 139 if (gettimeofday (&now, NULL) != 0) { 140 status = DC_STATUS_IO; 141 goto out; 142 } 143 144 timersub (&now, &timer->timestamp, &delta); 145 146 value = (dc_usecs_t) delta.tv_sec * 1000000 + delta.tv_usec; 147 #endif 148 149 out: 150 if (usecs) 151 *usecs = value; 152 153 return status; 154 } 155 156 dc_status_t 157 dc_timer_free (dc_timer_t *timer) 158 { 159 free (timer); 160 161 return DC_STATUS_SUCCESS; 162 }