-
Notifications
You must be signed in to change notification settings - Fork 2
/
OSTime.h
1769 lines (1473 loc) · 43.4 KB
/
OSTime.h
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************************************************************:')
OSTime.h
Functions related to the time.
Fabrice Le Bars
GetTickCount() from the file ue9.c provided by www.labjack.com
strtime_m() and strtime_fns() are based on a part of an example in the MSDN library
Created : 2009-01-28
***************************************************************************************************************:)*/
// Prevent Visual Studio Intellisense from defining _WIN32 and _MSC_VER when we use
// Visual Studio to edit Linux or Borland C++ code.
#ifdef __linux__
# undef _WIN32
#endif // __linux__
#if defined(__GNUC__) || defined(__BORLANDC__)
# undef _MSC_VER
#endif // defined(__GNUC__) || defined(__BORLANDC__)
#ifndef OSTIME_H
#define OSTIME_H
#include "OSCore.h"
/*
Debug macros specific to OSTime.
*/
#ifdef _DEBUG_MESSAGES_OSUTILS
# define _DEBUG_MESSAGES_OSTIME
#endif // _DEBUG_MESSAGES_OSUTILS
#ifdef _DEBUG_WARNINGS_OSUTILS
# define _DEBUG_WARNINGS_OSTIME
#endif // _DEBUG_WARNINGS_OSUTILS
#ifdef _DEBUG_ERRORS_OSUTILS
# define _DEBUG_ERRORS_OSTIME
#endif // _DEBUG_ERRORS_OSUTILS
#ifdef _DEBUG_MESSAGES_OSTIME
# define PRINT_DEBUG_MESSAGE_OSTIME(params) PRINT_DEBUG_MESSAGE(params)
#else
# define PRINT_DEBUG_MESSAGE_OSTIME(params)
#endif // _DEBUG_MESSAGES_OSTIME
#ifdef _DEBUG_WARNINGS_OSTIME
# define PRINT_DEBUG_WARNING_OSTIME(params) PRINT_DEBUG_WARNING(params)
#else
# define PRINT_DEBUG_WARNING_OSTIME(params)
#endif // _DEBUG_WARNINGS_OSTIME
#ifdef _DEBUG_ERRORS_OSTIME
# define PRINT_DEBUG_ERROR_OSTIME(params) PRINT_DEBUG_ERROR(params)
#else
# define PRINT_DEBUG_ERROR_OSTIME(params)
#endif // _DEBUG_ERRORS_OSTIME
#ifdef _WIN32
#ifndef ENABLE_SYS_TIME_H_WIN32
#ifdef ENABLE_GETTIMEOFDAY_WIN32
// To get struct timeval.
#include <winsock2.h>
// To get struct _timeb.
//#include <sys/timeb.h>
#endif // ENABLE_GETTIMEOFDAY_WIN32
#else
#include <sys/time.h>
#endif // !ENABLE_SYS_TIME_H_WIN32
#else
// CLOCK_MONOTONIC is a clock that cannot be set and represents monotonic time since
// some unspecified starting point.
// CLOCK_MONOTONIC_RAW is available since Linux 2.6.28 and provides access to a raw
// hardware-based time that is not subject to NTP adjustments.
#ifndef CLOCK_MONOTONIC_RAW
#define CLOCK_MONOTONIC_RAW CLOCK_MONOTONIC
#endif // !CLOCK_MONOTONIC_RAW
#endif // _WIN32
EXTERN_C char strftime_m_tmpbuf[64]; // Used to store the string returned by strtime_m().
EXTERN_C char strftimeex_m_tmpbuf[64]; // Used to store the string returned by strtimeex_m().
EXTERN_C char strftime_fns_tmpbuf[64]; // Used to store the string returned by strtime_fns().
EXTERN_C char strftimeex_fns_tmpbuf[64]; // Used to store the string returned by strtimeex_fns().
#ifndef USE_OLD_CHRONO
#ifdef _WIN32
struct CHRONO
{
LARGE_INTEGER Start;
LARGE_INTEGER Finish;
LARGE_INTEGER Duration;
LARGE_INTEGER Frequency;
BOOL Suspended; // Used to know if the chronometer is currently suspended.
};
typedef struct CHRONO CHRONO;
#else
struct CHRONO
{
struct timespec Start;
struct timespec Finish;
struct timespec Duration;
struct timespec Frequency;
BOOL Suspended; // Used to know if the chronometer is currently suspended.
};
typedef struct CHRONO CHRONO;
#endif // _WIN32
#else
// See https://stackoverflow.com/questions/43295845/is-the-clocks-per-sec-value-wrong-inside-a-virtual-machine
#if (defined(_WIN32) && (defined(ENABLE_GETTIMEOFDAY_WIN32) || defined(ENABLE_SYS_TIME_H_WIN32))) || (!defined(_WIN32))
//#if defined(ENABLE_GETTIMEOFDAY_WIN32) || defined(ENABLE_SYS_TIME_H_WIN32)
/*
Structure for a basic chronometer.
Might not work correctly if used during more than approximately 68 years.
The accuracy should be around 15 ms, but it is subject to system time changes
(automatic network time synchronization, user changing the system time...).
*/
struct CHRONO
{
struct timeval Start;
struct timeval Finish;
struct timeval Duration;
BOOL Suspended; // Used to know if the chronometer is currently suspended.
};
typedef struct CHRONO CHRONO;
#else
/*
Structure for a basic chronometer.
Might not work correctly if used during more than approximately 72 min for Linux
or 50 days for Windows.
*/
struct CHRONO
{
clock_t Start;
clock_t Finish;
double Duration;
BOOL Suspended; // Used to know if the chronometer is currently suspended.
};
typedef struct CHRONO CHRONO;
#endif // (defined(_WIN32) && (defined(ENABLE_GETTIMEOFDAY_WIN32) || defined(ENABLE_SYS_TIME_H_WIN32))) || (!defined(_WIN32))
//#endif // defined(ENABLE_GETTIMEOFDAY_WIN32) || defined(ENABLE_SYS_TIME_H_WIN32)
#endif // !USE_OLD_CHRONO
#ifdef _WIN32
#ifndef ENABLE_SYS_TIME_H_WIN32
#ifdef ENABLE_GETTIMEOFDAY_WIN32
//#define DELTA_EPOCH_IN_MICROSECS 11644473600000000ui64
//#define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
#define DELTA_EPOCH_IN_MICROSECS_LOW 1216757760
#define DELTA_EPOCH_IN_MICROSECS_HIGH 2711190
#ifdef DISABLE_TIMEZONE_STRUCT_REDEFINITION
struct timezone2
{
int tz_minuteswest; // Minutes W of Greenwich.
int tz_dsttime; // Type of DST correction.
};
#else
#ifdef _MSC_VER
// Disable Visual Studio warnings about timezone declaration.
#pragma warning(disable : 6244)
#endif // _MSC_VER
// Local declaration of timezone hides previous declaration in time.h.
struct timezone
{
int tz_minuteswest; // Minutes W of Greenwich.
int tz_dsttime; // Type of DST correction.
};
#ifdef _MSC_VER
// Restore the Visual Studio warnings previously disabled.
#pragma warning(default : 6244)
#endif // _MSC_VER
#endif // DISABLE_TIMEZONE_STRUCT_REDEFINITION
/*
Obtain the current time, expressed as seconds and microseconds since the Epoch,
and store it in the timeval structure pointed to by tv (the accuracy should be
around 15 ms).
struct timeval* tv : (INOUT) Valid pointer to the structure that will receive
the current time.
struct timezone* tz : (INOUT) Usually set to NULL.
Return : EXIT_SUCCESS or EXIT_FAILURE if there is an error.
*/
#ifdef DISABLE_TIMEZONE_STRUCT_REDEFINITION
inline int gettimeofday(struct timeval* tv, struct timezone2* tz)
#else
inline int gettimeofday(struct timeval* tv, struct timezone* tz)
#endif // DISABLE_TIMEZONE_STRUCT_REDEFINITION
{
FILETIME ft; // Will contain a 64-bit value representing the number of 100-nanosecond
// intervals since January 1, 1601 (UTC).
ULONGLONG tmpres = 0;
ULARGE_INTEGER li;
ULARGE_INTEGER epoch;
#ifdef USE__TZSET
static int tzflag;
#else
TIME_ZONE_INFORMATION tz_winapi;
int rez = 0;
#endif // USE__TZSET
if (tv)
{
GetSystemTimeAsFileTime(&ft);
li.LowPart = ft.dwLowDateTime;
li.HighPart = ft.dwHighDateTime;
// Converting file time to UNIX Epoch.
tmpres = li.QuadPart/(ULONGLONG)10; // Convert into microseconds.
//tmpres -= DELTA_EPOCH_IN_MICROSECS;
epoch.LowPart = DELTA_EPOCH_IN_MICROSECS_LOW;
epoch.HighPart = DELTA_EPOCH_IN_MICROSECS_HIGH;
tmpres -= epoch.QuadPart;
tv->tv_sec = (long)(tmpres/(ULONGLONG)1000000);
tv->tv_usec = (long)(tmpres%(ULONGLONG)1000000);
}
#ifdef USE__TZSET
if (tz)
{
if (!tzflag)
{
_tzset();
tzflag++;
}
tz->tz_minuteswest = _timezone/60;
tz->tz_dsttime = _daylight;
}
#else
if (tz)
{
// _tzset(), do not work properly, so we use GetTimeZoneInformation.
rez = GetTimeZoneInformation(&tz_winapi);
tz->tz_dsttime = (rez == 2)?TRUE:FALSE;
tz->tz_minuteswest = tz_winapi.Bias + ((rez == 2)?tz_winapi.DaylightBias:0);
}
#endif // USE__TZSET
return EXIT_SUCCESS;
}
//inline int gettimeofday(struct timeval* tp, void* tz)
//{
// struct _timeb timebuffer;
//
// UNREFERENCED_PARAMETER(tz);
//
// _ftime(&timebuffer);
// tp->tv_sec = (long)timebuffer.time;
// tp->tv_usec = timebuffer.millitm*1000;
// return 0;
//}
//// From olsrd...
//inline void gettimeofday(struct timeval *TVal, void *TZone __attribute__ ((unused)))
//{
// SYSTEMTIME SysTime;
// FILETIME FileTime;
// unsigned __int64 Ticks;
//
// GetSystemTime(&SysTime);
// SystemTimeToFileTime(&SysTime, &FileTime);
//
// Ticks = ((__int64) FileTime.dwHighDateTime << 32) | (__int64) FileTime.dwLowDateTime;
//
// Ticks -= 116444736000000000LL;
//
// TVal->tv_sec = (unsigned int)(Ticks / 10000000);
// TVal->tv_usec = (unsigned int)(Ticks % 10000000) / 10;
//}
#endif // ENABLE_GETTIMEOFDAY_WIN32
#endif // !ENABLE_SYS_TIME_H_WIN32
#endif // _WIN32
#ifndef _WIN32
/*
Retrieve the number of milliseconds that has elasped since the system was started (Windows)
or the time since the Epoch (Linux). The accuracy should be around 15 ms and the value will
wrap around to 0 after 50 days for Windows.
Return : This number.
*/
inline DWORD GetTickCount(void)
{
struct timeval tv;
gettimeofday(&tv, NULL);
return (DWORD)((tv.tv_sec*1000)+(tv.tv_usec/1000));
}
#endif // !_WIN32
// DWORD timeGetTime(void) is also similar to GetTickCount()...
#ifndef DISABLE_TIMEGM_MKGMTIME
#ifdef _WIN32
#ifdef __GNUC__
//#if !(((__GNUC__ == 4) && (__GNUC_MINOR__ >= 6)) || (__GNUC__ > 4))
// https://stackoverflow.com/questions/16647819/timegm-cross-platform
//
// Does not account for non-whole-hour offsets, for instance UTC?09:30, UTC+05:45, will fail
// for time zones outside of +/-12 hours, will fail during hour when time zone is switched
// from summer to winter, there is an ambiguity at those hour in local time because the same
// local hour is used twice per night...
//inline time_t _mkgmtime(struct tm * a_tm)
//{
// time_t ltime = mktime(a_tm);
// struct tm tm_val;
// gmtime_s(&tm_val, <ime);
// int offset = (tm_val.tm_hour - a_tm->tm_hour);
// if (offset > 12)
// {
// offset = 24 - offset;
// }
// time_t utc = mktime(a_tm) - offset * 3600;
// return utc;
//}
inline int32_t __is_leap(int32_t year)
{
if (year % 400 == 0)
return 1;
if (year % 100 == 0)
return 0;
if (year % 4 == 0)
return 1;
return 0;
}
inline int32_t __days_from_0(int32_t year)
{
year--;
return 365 * year + (year / 400) - (year/100) + (year / 4);
}
inline int32_t __days_from_1970(int32_t year)
{
int days_from_0_to_1970 = __days_from_0(1970);
return __days_from_0(year) - days_from_0_to_1970;
}
inline int32_t __days_from_1jan(int32_t year, int32_t month, int32_t day)
{
static const int32_t days[2][12] =
{
{ 0,31,59,90,120,151,181,212,243,273,304,334},
{ 0,31,60,91,121,152,182,213,244,274,305,335}
};
return days[__is_leap(year)][month-1] + day - 1;
}
inline time_t timegm(struct tm * t)
{
int year = t->tm_year + 1900;
int month = t->tm_mon;
if (month > 11)
{
year += month/12;
month %= 12;
}
else if (month < 0)
{
int years_diff = (-month + 11)/12;
year -= years_diff;
month += 12 * years_diff;
}
month++;
int day = t->tm_mday;
int day_of_year = __days_from_1jan(year, month, day);
int days_since_epoch = __days_from_1970(year) + day_of_year;
time_t seconds_in_day = 3600 * 24;
time_t result = seconds_in_day * days_since_epoch + 3600 * t->tm_hour + 60 * t->tm_min + t->tm_sec;
return result;
}
//#endif // !(((__GNUC__ == 4) && (__GNUC_MINOR__ >= 6)) || (__GNUC__ > 4))
#else
#ifndef WINCE
#ifndef timegm
#define timegm _mkgmtime
#endif // !timegm
// timelocal() is a deprecated interface that is equivalent to calling mktime() with a negative value for tm_isdst...
#ifndef timelocal
#define timelocal mktime
#endif // !timelocal
#endif // !WINCE
#endif // __GNUC__
#else
#ifndef _mkgmtime
#define _mkgmtime timegm
#endif // !_mkgmtime
#endif // _WIN32
#endif // !DISABLE_TIMEGM_MKGMTIME
/*
Return a string like ctime() but in this format :
2007-08-27 19:28:04\0
(without the "\n" of ctime()).
Should not be used directly in concurrent threads as the string value returned might
be changed by another thread.
Return : This string.
*/
EXTERN_C char* strtime_m(void);
#if (defined(_WIN32) && (defined(ENABLE_GETTIMEOFDAY_WIN32) || defined(ENABLE_SYS_TIME_H_WIN32))) || (!defined(_WIN32))
/*
Return a string like ctime() but in this format :
2007-08-27 19:28:04:00\0
(without the "\n" of ctime()).
Should not be used directly in concurrent threads as the string value returned might
be changed by another thread.
Return : This string.
*/
EXTERN_C char* strtimeex_m(void);
#endif // (defined(_WIN32) && (defined(ENABLE_GETTIMEOFDAY_WIN32) || defined(ENABLE_SYS_TIME_H_WIN32))) || (!defined(_WIN32))
/*
Return a string like ctime() but in this format :
2007-08-27_19h28min04s\0
(without the "\n", ":", " " of ctime() in order to be safely used in file names).
Should not be used directly in concurrent threads as the string value returned might
be changed by another thread.
Return : This string.
*/
EXTERN_C char* strtime_fns(void);
#if (defined(_WIN32) && (defined(ENABLE_GETTIMEOFDAY_WIN32) || defined(ENABLE_SYS_TIME_H_WIN32))) || (!defined(_WIN32))
/*
Return a string like ctime() but in this format :
2007-08-27_19h28min04s00\0
(without the "\n", ":", " " of ctime() in order to be safely used in file names).
Should not be used directly in concurrent threads as the string value returned might
be changed by another thread.
Return : This string.
*/
EXTERN_C char* strtimeex_fns(void);
#endif // (defined(_WIN32) && (defined(ENABLE_GETTIMEOFDAY_WIN32) || defined(ENABLE_SYS_TIME_H_WIN32))) || (!defined(_WIN32))
/*
Wait some time...
long Milliseconds : (IN) Time to wait in ms.
Return : Nothing.
*/
inline void mSleep(long Milliseconds)
{
#ifdef _WIN32
Sleep(Milliseconds);
#else
// usleep() is considered as obsolete.
//usleep(Milliseconds*1000);
struct timespec req;
req.tv_sec = Milliseconds/1000; // Seconds.
req.tv_nsec = (Milliseconds%1000)*1000000; // Additional nanoseconds.
nanosleep(&req, NULL);
#endif // _WIN32
}
/*
Wait some time...
long Microseconds : (IN) Time to wait in us.
Return : Nothing.
*/
inline void uSleep(long Microseconds)
{
#ifdef _WIN32
// From https://stackoverflow.com/questions/5801813/c-usleep-is-obsolete-workarounds-for-windows-mingw.
HANDLE timer;
LARGE_INTEGER ft;
ft.QuadPart = -(10*(LONGLONG)Microseconds); // Convert to 100 nanosecond interval, negative value indicates relative time.
timer = CreateWaitableTimer(NULL, TRUE, NULL);
SetWaitableTimer(timer, &ft, 0, NULL, NULL, FALSE);
//WaitForSingleObject(timer, INFINITE);
WaitForSingleObject(timer, (DWORD)(1+Microseconds/1000));
CloseHandle(timer);
#else
// usleep() is considered as obsolete.
//usleep(Microseconds);
struct timespec req;
req.tv_sec = Microseconds/1000000; // Seconds.
req.tv_nsec = (Microseconds%1000000)*1000; // Additional nanoseconds.
nanosleep(&req, NULL);
#endif // _WIN32
}
inline void DecSec2DaysHoursMinSec(double decsec, int* pDays, int* pHours, int* pMin, int* pSec, double* pDeccsec)
{
int hours = 0, minutes = 0, seconds = 0;
seconds = (int)decsec;
*pSec = seconds%60;
minutes = seconds/60;
*pMin = minutes%60;
hours = minutes/60;
*pHours = hours%24;
*pDays = hours/24;
*pDeccsec = (decsec-seconds)*100.0;
}
#ifndef USE_OLD_CHRONO
#ifdef _WIN32
/*
Start a chronometer.
CHRONO* pChrono : (INOUT) Valid pointer to the structure.
Return : EXIT_SUCCESS or EXIT_FAILURE if there is an error.
*/
inline int StartChrono(CHRONO* pChrono)
{
if (!QueryPerformanceFrequency(&pChrono->Frequency))
{
PRINT_DEBUG_ERROR_OSTIME(("StartChrono error (%s) : %s\n",
strtime_m(),
GetLastErrorMsg()));
return EXIT_FAILURE;
}
if (!QueryPerformanceCounter(&pChrono->Start))
{
PRINT_DEBUG_ERROR_OSTIME(("StartChrono error (%s) : %s\n",
strtime_m(),
GetLastErrorMsg()));
return EXIT_FAILURE;
}
pChrono->Finish.QuadPart = 0;
pChrono->Duration.QuadPart = 0;
pChrono->Suspended = FALSE;
return EXIT_SUCCESS;
}
/*
Suspend a chronometer.
CHRONO* pChrono : (INOUT) Valid pointer to the structure.
double* pDuration : (INOUT) Duration (in s).
Return : EXIT_SUCCESS or EXIT_FAILURE if there is an error.
*/
inline int SuspendChrono(CHRONO* pChrono, double* pDuration)
{
LARGE_INTEGER temp;
if (!pChrono->Suspended)
{
pChrono->Suspended = TRUE;
if (!QueryPerformanceCounter(&temp))
{
PRINT_DEBUG_ERROR_OSTIME(("SuspendChrono error (%s) : %s"
"(pChrono=%#x)\n",
strtime_m(),
GetLastErrorMsg(),
pChrono));
return EXIT_FAILURE;
}
//pChrono->Duration += pChrono->Finish-pChrono->Start
pChrono->Finish = temp;
temp.QuadPart += pChrono->Duration.QuadPart-pChrono->Start.QuadPart;
pChrono->Duration = temp;
*pDuration = (double)pChrono->Duration.QuadPart/(double)pChrono->Frequency.QuadPart;
}
else
{
PRINT_DEBUG_ERROR_OSTIME(("SuspendChrono error (%s) : %s"
"(pChrono=%#x)\n",
strtime_m(),
"Chrono already suspended. ",
pChrono));
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
/*
Suspend a chronometer.
CHRONO* pChrono : (INOUT) Valid pointer to the structure.
Return : Duration (in s).
*/
inline double SuspendChronoQuick(CHRONO* pChrono)
{
LARGE_INTEGER temp;
if (!pChrono->Suspended)
{
pChrono->Suspended = TRUE;
QueryPerformanceCounter(&temp);
//pChrono->Duration += pChrono->Finish-pChrono->Start
pChrono->Finish = temp;
temp.QuadPart += pChrono->Duration.QuadPart-pChrono->Start.QuadPart;
pChrono->Duration = temp;
return (double)pChrono->Duration.QuadPart/(double)pChrono->Frequency.QuadPart;
}
else
{
PRINT_DEBUG_ERROR_OSTIME(("SuspendChronoQuick error (%s) : %s"
"(pChrono=%#x)\n",
strtime_m(),
"Chrono already suspended. ",
pChrono));
return -1;
}
}
/*
Resume a chronometer.
CHRONO* pChrono : (INOUT) Valid pointer to the structure.
Return : EXIT_SUCCESS or EXIT_FAILURE if there is an error.
*/
inline int ResumeChrono(CHRONO* pChrono)
{
if (pChrono->Suspended)
{
pChrono->Suspended = FALSE;
if (!QueryPerformanceCounter(&pChrono->Start))
{
PRINT_DEBUG_ERROR_OSTIME(("ResumeChrono error (%s) : %s"
"(pChrono=%#x)\n",
strtime_m(),
GetLastErrorMsg(),
pChrono));
return EXIT_FAILURE;
}
}
else
{
PRINT_DEBUG_ERROR_OSTIME(("ResumeChrono error (%s) : %s"
"(pChrono=%#x)\n",
strtime_m(),
"Chrono not suspended. ",
pChrono));
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
/*
Get the time elapsed of a chronometer.
CHRONO* pChrono : (INOUT) Valid pointer to the structure.
double* pDuration : (INOUT) Duration (in s).
Return : EXIT_SUCCESS or EXIT_FAILURE if there is an error.
*/
inline int GetTimeElapsedChrono(CHRONO* pChrono, double* pDuration)
{
LARGE_INTEGER temp;
if (pChrono->Suspended)
{
*pDuration = (double)pChrono->Duration.QuadPart/(double)pChrono->Frequency.QuadPart;
}
else
{
if (!QueryPerformanceCounter(&temp))
{
PRINT_DEBUG_ERROR_OSTIME(("GetTimeElapsedChrono error (%s) : %s"
"(pChrono=%#x)\n",
strtime_m(),
GetLastErrorMsg(),
pChrono));
return EXIT_FAILURE;
}
//*pDuration = pChrono->Duration+temp-pChrono->Start
temp.QuadPart += pChrono->Duration.QuadPart-pChrono->Start.QuadPart;
*pDuration = (double)temp.QuadPart/(double)pChrono->Frequency.QuadPart;
}
return EXIT_SUCCESS;
}
/*
Get the time elapsed of a chronometer.
CHRONO* pChrono : (INOUT) Valid pointer to the structure.
Return : Duration (in s).
*/
inline double GetTimeElapsedChronoQuick(CHRONO* pChrono)
{
LARGE_INTEGER temp;
if (pChrono->Suspended)
{
return (double)pChrono->Duration.QuadPart/(double)pChrono->Frequency.QuadPart;
}
else
{
QueryPerformanceCounter(&temp);
//*pDuration = pChrono->Duration+temp-pChrono->Start
temp.QuadPart += pChrono->Duration.QuadPart-pChrono->Start.QuadPart;
return (double)temp.QuadPart/(double)pChrono->Frequency.QuadPart;
}
}
/*
Stop a chronometer.
CHRONO* pChrono : (INOUT) Valid pointer to the structure.
double* pDuration : (INOUT) Duration (in s).
Return : EXIT_SUCCESS or EXIT_FAILURE if there is an error.
*/
inline int StopChrono(CHRONO* pChrono, double* pDuration)
{
LARGE_INTEGER temp;
if (pChrono->Suspended)
{
*pDuration = (double)pChrono->Duration.QuadPart/(double)pChrono->Frequency.QuadPart;
}
else
{
if (!QueryPerformanceCounter(&temp))
{
PRINT_DEBUG_ERROR_OSTIME(("StopChrono error (%s) : %s"
"(pChrono=%#x)\n",
strtime_m(),
GetLastErrorMsg(),
pChrono));
return EXIT_FAILURE;
}
//pChrono->Duration += pChrono->Finish-pChrono->Start
pChrono->Finish = temp;
temp.QuadPart += pChrono->Duration.QuadPart-pChrono->Start.QuadPart;
pChrono->Duration = temp;
*pDuration = (double)pChrono->Duration.QuadPart/(double)pChrono->Frequency.QuadPart;
}
return EXIT_SUCCESS;
}
/*
Stop a chronometer.
CHRONO* pChrono : (INOUT) Valid pointer to the structure.
Return : Duration (in s).
*/
inline double StopChronoQuick(CHRONO* pChrono)
{
LARGE_INTEGER temp;
if (pChrono->Suspended)
{
return (double)pChrono->Duration.QuadPart/(double)pChrono->Frequency.QuadPart;
}
else
{
QueryPerformanceCounter(&temp);
//pChrono->Duration += pChrono->Finish-pChrono->Start
pChrono->Finish = temp;
temp.QuadPart += pChrono->Duration.QuadPart-pChrono->Start.QuadPart;
pChrono->Duration = temp;
return (double)pChrono->Duration.QuadPart/(double)pChrono->Frequency.QuadPart;
}
}
#else
/*
Start a chronometer.
CHRONO* pChrono : (INOUT) Valid pointer to the structure.
Return : EXIT_SUCCESS or EXIT_FAILURE if there is an error.
*/
inline int StartChrono(CHRONO* pChrono)
{
if (clock_getres(CLOCK_MONOTONIC_RAW, &pChrono->Frequency) != EXIT_SUCCESS)
{
PRINT_DEBUG_ERROR_OSTIME(("StartChrono error (%s) : %s\n",
strtime_m(),
GetLastErrorMsg()));
return EXIT_FAILURE;
}
if (clock_gettime(CLOCK_MONOTONIC_RAW, &pChrono->Start) != EXIT_SUCCESS)
{
PRINT_DEBUG_ERROR_OSTIME(("StartChrono error (%s) : %s\n",
strtime_m(),
GetLastErrorMsg()));
return EXIT_FAILURE;
}
pChrono->Finish.tv_sec = 0;
pChrono->Finish.tv_nsec = 0;
pChrono->Duration.tv_sec = 0;
pChrono->Duration.tv_nsec = 0;
pChrono->Suspended = FALSE;
return EXIT_SUCCESS;
}
/*
Suspend a chronometer.
CHRONO* pChrono : (INOUT) Valid pointer to the structure.
double* pDuration : (INOUT) Duration (in s).
Return : EXIT_SUCCESS or EXIT_FAILURE if there is an error.
*/
inline int SuspendChrono(CHRONO* pChrono, double* pDuration)
{
struct timespec temp;
if (!pChrono->Suspended)
{
pChrono->Suspended = TRUE;
if (clock_gettime(CLOCK_MONOTONIC_RAW, &temp) != EXIT_SUCCESS)
{
PRINT_DEBUG_ERROR_OSTIME(("SuspendChrono error (%s) : %s"
"(pChrono=%#x)\n",
strtime_m(),
GetLastErrorMsg(),
pChrono));
return EXIT_FAILURE;
}
//pChrono->Duration += pChrono->Finish-pChrono->Start
pChrono->Finish = temp;
temp.tv_sec += pChrono->Duration.tv_sec; // Seconds.
temp.tv_nsec += pChrono->Duration.tv_nsec; // Additional nanoseconds.
// tv_nsec must stay in [0,1000000000].
temp.tv_sec += temp.tv_nsec/1000000000;
temp.tv_nsec = temp.tv_nsec%1000000000;
temp.tv_sec -= pChrono->Start.tv_sec;
temp.tv_nsec -= pChrono->Start.tv_nsec;
// tv_nsec must stay in [0,1000000000].
if (temp.tv_nsec < 0)
{
temp.tv_sec += -1+temp.tv_nsec/1000000000;
temp.tv_nsec = 1000000000+temp.tv_nsec%1000000000;
}
pChrono->Duration = temp;
*pDuration = pChrono->Duration.tv_sec+(double)pChrono->Duration.tv_nsec/1000000000.0;
}
else
{
PRINT_DEBUG_ERROR_OSTIME(("SuspendChrono error (%s) : %s"
"(pChrono=%#x)\n",
strtime_m(),
"Chrono already suspended. ",
pChrono));
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
/*
Suspend a chronometer.
CHRONO* pChrono : (INOUT) Valid pointer to the structure.
Return : Duration (in s).
*/
inline double SuspendChronoQuick(CHRONO* pChrono)
{
struct timespec temp;
if (!pChrono->Suspended)
{
pChrono->Suspended = TRUE;
clock_gettime(CLOCK_MONOTONIC_RAW, &temp);
//pChrono->Duration += pChrono->Finish-pChrono->Start
pChrono->Finish = temp;
temp.tv_sec += pChrono->Duration.tv_sec; // Seconds.
temp.tv_nsec += pChrono->Duration.tv_nsec; // Additional nanoseconds.
// tv_nsec must stay in [0,1000000000].
temp.tv_sec += temp.tv_nsec/1000000000;
temp.tv_nsec = temp.tv_nsec%1000000000;
temp.tv_sec -= pChrono->Start.tv_sec;
temp.tv_nsec -= pChrono->Start.tv_nsec;
// tv_nsec must stay in [0,1000000000].
if (temp.tv_nsec < 0)
{
temp.tv_sec += -1+temp.tv_nsec/1000000000;
temp.tv_nsec = 1000000000+temp.tv_nsec%1000000000;
}
pChrono->Duration = temp;
return pChrono->Duration.tv_sec+(double)pChrono->Duration.tv_nsec/1000000000.0;
}
else
{
PRINT_DEBUG_ERROR_OSTIME(("SuspendChronoQuick error (%s) : %s"
"(pChrono=%#x)\n",
strtime_m(),
"Chrono already suspended. ",
pChrono));
return -1;
}
}
/*
Resume a chronometer.
CHRONO* pChrono : (INOUT) Valid pointer to the structure.
Return : EXIT_SUCCESS or EXIT_FAILURE if there is an error.
*/
inline int ResumeChrono(CHRONO* pChrono)
{
if (pChrono->Suspended)
{
pChrono->Suspended = FALSE;
if (clock_gettime(CLOCK_MONOTONIC_RAW, &pChrono->Start) != EXIT_SUCCESS)
{
PRINT_DEBUG_ERROR_OSTIME(("ResumeChrono error (%s) : %s"
"(pChrono=%#x)\n",
strtime_m(),
GetLastErrorMsg(),
pChrono));
return EXIT_FAILURE;
}
}
else
{
PRINT_DEBUG_ERROR_OSTIME(("ResumeChrono error (%s) : %s"
"(pChrono=%#x)\n",
strtime_m(),
"Chrono not suspended. ",
pChrono));
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
/*
Get the time elapsed of a chronometer.
CHRONO* pChrono : (INOUT) Valid pointer to the structure.
double* pDuration : (INOUT) Duration (in s).
Return : EXIT_SUCCESS or EXIT_FAILURE if there is an error.
*/
inline int GetTimeElapsedChrono(CHRONO* pChrono, double* pDuration)
{
struct timespec temp;
if (pChrono->Suspended)
{
*pDuration = pChrono->Duration.tv_sec+(double)pChrono->Duration.tv_nsec/1000000000.0;
}
else
{
if (clock_gettime(CLOCK_MONOTONIC_RAW, &temp) != EXIT_SUCCESS)
{
PRINT_DEBUG_ERROR_OSTIME(("GetTimeElapsedChrono error (%s) : %s"