MQTTTime.c
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright (c) 2020 IBM Corp.
3  *
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v2.0
6  * and Eclipse Distribution License v1.0 which accompany this distribution.
7  *
8  * The Eclipse Public License is available at
9  * https://www.eclipse.org/legal/epl-2.0/
10  * and the Eclipse Distribution License is available at
11  * http://www.eclipse.org/org/documents/edl-v10.php.
12  *
13  * Contributors:
14  * Ian Craggs - initial implementation
15  *******************************************************************************/
16 
17 #include "MQTTTime.h"
18 #include "StackTrace.h"
19 
20 #if defined(_WIN32) || defined(_WIN64)
21 #include <windows.h>
22 #else
23 #include <unistd.h>
24 #include <sys/time.h>
25 #endif
26 
28 {
29  FUNC_ENTRY;
30 #if defined(_WIN32) || defined(_WIN64)
31  Sleep((DWORD)milliseconds);
32 #else
33  usleep((useconds_t)(milliseconds*1000));
34 #endif
35  FUNC_EXIT;
36 }
37 
38 #if defined(_WIN32) || defined(_WIN64)
40 {
41 #if WINVER >= _WIN32_WINNT_VISTA
42  return GetTickCount64();
43 #else
44  return GetTickCount();
45 #endif
46 }
47 #elif defined(AIX)
49 {
50  static struct timespec start;
51  clock_gettime(CLOCK_MONOTONIC, &start);
52  return start;
53 }
54 #else
56 {
57  static struct timeval start;
58  static struct timespec start_ts;
59 
60  clock_gettime(CLOCK_MONOTONIC, &start_ts);
61  start.tv_sec = start_ts.tv_sec;
62  start.tv_usec = start_ts.tv_nsec / 1000;
63  return start;
64 }
65 #endif
67 {
68  return MQTTTime_start_clock();
69 }
70 
71 
72 #if defined(_WIN32) || defined(_WIN64)
73 /*
74  * @param new most recent time in milliseconds from GetTickCount()
75  * @param old older time in milliseconds from GetTickCount()
76  * @return difference in milliseconds
77  */
79 {
80 #if WINVER >= _WIN32_WINNT_VISTA
81  return (DIFF_TIME_TYPE )(new - old);
82 #else
83  if (old < new) /* check for wrap around condition in GetTickCount */
84  return (DIFF_TIME_TYPE)(new - old);
85  else
86  return (DIFF_TIME_TYPE)((0xFFFFFFFFL - old) + 1 + new);
87 #endif
88 }
89 #elif defined(AIX)
90 #define assert(a)
92 {
93  struct timespec result;
94 
95  ntimersub(new, old, result);
96  return (DIFF_TIME_TYPE)((result.tv_sec)*1000L + (result.tv_nsec)/1000000L); /* convert to milliseconds */
97 }
98 #else
100 {
101  struct timeval result;
102 
103  timersub(&new, &old, &result);
104  return (DIFF_TIME_TYPE)(((DIFF_TIME_TYPE)result.tv_sec)*1000 + ((DIFF_TIME_TYPE)result.tv_usec)/1000); /* convert to milliseconds */
105 }
106 #endif
107 
108 
110 {
111  return (ELAPSED_TIME_TYPE)MQTTTime_difftime(MQTTTime_now(), milliseconds);
112 }
#define DIFF_TIME_TYPE
Definition: MQTTTime.h:41
#define FUNC_EXIT
Definition: StackTrace.h:59
DIFF_TIME_TYPE MQTTTime_difftime(START_TIME_TYPE new, START_TIME_TYPE old)
Definition: MQTTTime.c:99
START_TIME_TYPE MQTTTime_start_clock(void)
Definition: MQTTTime.c:55
void MQTTTime_sleep(ELAPSED_TIME_TYPE milliseconds)
Definition: MQTTTime.c:27
#define START_TIME_TYPE
Definition: MQTTTime.h:36
#define FUNC_ENTRY
Definition: StackTrace.h:55
START_TIME_TYPE MQTTTime_now(void)
Definition: MQTTTime.c:66
#define ELAPSED_TIME_TYPE
Definition: MQTTTime.h:40
ELAPSED_TIME_TYPE MQTTTime_elapsed(START_TIME_TYPE milliseconds)
Definition: MQTTTime.c:109


plotjuggler
Author(s): Davide Faconti
autogenerated on Sun Dec 6 2020 03:48:09