mirror of https://github.com/eclipse/paho.mqtt.c
Add two new files as part of #758
This commit is contained in:
parent
39ef1075e2
commit
0567b0814d
|
|
@ -0,0 +1,117 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2020 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial implementation
|
||||
*******************************************************************************/
|
||||
|
||||
#include "MQTTTime.h"
|
||||
#include "StackTrace.h"
|
||||
|
||||
void MQTTTime_sleep(long milliseconds)
|
||||
{
|
||||
FUNC_ENTRY;
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
Sleep(milliseconds);
|
||||
#else
|
||||
usleep(milliseconds*1000);
|
||||
#endif
|
||||
FUNC_EXIT;
|
||||
}
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
START_TIME_TYPE MQTTTime_start_clock(void)
|
||||
{
|
||||
return GetTickCount();
|
||||
}
|
||||
#elif defined(AIX)
|
||||
START_TIME_TYPE MQTTTime_start_clock(void)
|
||||
{
|
||||
static struct timespec start;
|
||||
clock_gettime(CLOCK_MONOTONIC, &start);
|
||||
return start;
|
||||
}
|
||||
#else
|
||||
START_TIME_TYPE MQTTTime_start_clock(void)
|
||||
{
|
||||
static struct timeval start;
|
||||
static struct timespec start_ts;
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &start_ts);
|
||||
start.tv_sec = start_ts.tv_sec;
|
||||
start.tv_usec = start_ts.tv_nsec / 1000;
|
||||
return start;
|
||||
}
|
||||
#endif
|
||||
START_TIME_TYPE MQTTTime_now(void)
|
||||
{
|
||||
return MQTTTime_start_clock();
|
||||
}
|
||||
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
long MQTTTime_elapsed(DWORD milliseconds)
|
||||
{
|
||||
return GetTickCount() - milliseconds;
|
||||
}
|
||||
#elif defined(AIX)
|
||||
#define assert(a)
|
||||
long MQTTTime_elapsed(struct timespec start)
|
||||
{
|
||||
struct timespec now, res;
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &now);
|
||||
ntimersub(now, start, res);
|
||||
return (res.tv_sec)*1000L + (res.tv_nsec)/1000000L;
|
||||
}
|
||||
#else
|
||||
long MQTTTime_elapsed(struct timeval start)
|
||||
{
|
||||
struct timeval now, res;
|
||||
static struct timespec now_ts;
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC, &now_ts);
|
||||
now.tv_sec = now_ts.tv_sec;
|
||||
now.tv_usec = now_ts.tv_nsec / 1000;
|
||||
timersub(&now, &start, &res);
|
||||
return (res.tv_sec)*1000 + (res.tv_usec)/1000;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
/*
|
||||
* @param new most recent time in milliseconds from GetTickCount()
|
||||
* @param old older time in milliseconds from GetTickCount()
|
||||
* @return difference in milliseconds
|
||||
*/
|
||||
long MQTTTime_difftime(DWORD new, DWORD old)
|
||||
{
|
||||
return new - old;
|
||||
}
|
||||
#elif defined(AIX)
|
||||
#define assert(a)
|
||||
long MQTTTime_difftime(struct timespec new, struct timespec old)
|
||||
{
|
||||
struct timespec result;
|
||||
|
||||
ntimersub(new, old, result);
|
||||
return (result.tv_sec)*1000L + (result.tv_nsec)/1000000L; /* convert to milliseconds */
|
||||
}
|
||||
#else
|
||||
long MQTTTime_difftime(struct timeval new, struct timeval old)
|
||||
{
|
||||
struct timeval result;
|
||||
|
||||
timersub(&new, &old, &result);
|
||||
return (result.tv_sec)*1000 + (result.tv_usec)/1000; /* convert to milliseconds */
|
||||
}
|
||||
#endif
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
/*******************************************************************************
|
||||
* Copyright (c) 2020 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial implementation
|
||||
*******************************************************************************/
|
||||
|
||||
#if !defined(MQTTTIME_H)
|
||||
#define MQTTTIME_H
|
||||
|
||||
#if defined(_WIN32) || defined(_WIN64)
|
||||
#include <windows.h>
|
||||
#define START_TIME_TYPE DWORD
|
||||
#elif defined(AIX)
|
||||
#define START_TIME_TYPE struct timespec
|
||||
#else
|
||||
#define START_TIME_TYPE struct timeval
|
||||
#endif
|
||||
|
||||
void MQTTTime_sleep(long milliseconds);
|
||||
START_TIME_TYPE MQTTTime_start_clock(void);
|
||||
START_TIME_TYPE MQTTTime_now(void);
|
||||
long MQTTTime_elapsed(START_TIME_TYPE milliseconds);
|
||||
long MQTTTime_difftime(START_TIME_TYPE new, START_TIME_TYPE old);
|
||||
|
||||
#endif
|
||||
Loading…
Reference in New Issue