2013-12-29 04:53:12 +00:00
|
|
|
/*
|
2016-03-25 19:59:40 +00:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
* or more contributor license agreements. See the NOTICE file
|
|
|
|
* distributed with this work for additional information
|
|
|
|
* regarding copyright ownership. The ASF licenses this file
|
|
|
|
* to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at
|
2012-09-06 00:17:18 +00:00
|
|
|
*
|
2016-03-25 19:59:40 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2012-09-06 00:17:18 +00:00
|
|
|
*
|
2016-03-25 19:59:40 +00:00
|
|
|
* Unless required by applicable law or agreed to in writing,
|
|
|
|
* software distributed under the License is distributed on an
|
|
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
* KIND, either express or implied. See the License for the
|
|
|
|
* specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2013-12-29 04:53:12 +00:00
|
|
|
*/
|
|
|
|
|
2014-01-01 22:44:28 +00:00
|
|
|
#include "config.h"
|
2012-09-06 00:17:18 +00:00
|
|
|
|
2014-01-01 22:44:28 +00:00
|
|
|
#include "timestamp.h"
|
2012-09-06 00:17:18 +00:00
|
|
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
2014-01-01 22:44:28 +00:00
|
|
|
#if defined(HAVE_CLOCK_GETTIME) || defined(HAVE_NANOSLEEP)
|
|
|
|
#include <time.h>
|
|
|
|
#endif
|
2012-09-06 00:17:18 +00:00
|
|
|
|
|
|
|
guac_timestamp guac_timestamp_current() {
|
|
|
|
|
|
|
|
#ifdef HAVE_CLOCK_GETTIME
|
|
|
|
|
|
|
|
struct timespec current;
|
|
|
|
|
2017-03-23 22:06:24 +00:00
|
|
|
/* Get current time, monotonically increasing */
|
|
|
|
#ifdef CLOCK_MONOTONIC
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, ¤t);
|
|
|
|
#else
|
2012-09-06 00:17:18 +00:00
|
|
|
clock_gettime(CLOCK_REALTIME, ¤t);
|
2017-03-23 22:06:24 +00:00
|
|
|
#endif
|
|
|
|
|
2012-09-06 00:17:18 +00:00
|
|
|
/* Calculate milliseconds */
|
|
|
|
return (guac_timestamp) current.tv_sec * 1000 + current.tv_nsec / 1000000;
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
struct timeval current;
|
|
|
|
|
|
|
|
/* Get current time */
|
|
|
|
gettimeofday(¤t, NULL);
|
|
|
|
|
|
|
|
/* Calculate milliseconds */
|
|
|
|
return (guac_timestamp) current.tv_sec * 1000 + current.tv_usec / 1000;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-03-01 05:35:32 +00:00
|
|
|
void guac_timestamp_msleep(int duration) {
|
|
|
|
|
|
|
|
/* Split milliseconds into equivalent seconds + nanoseconds */
|
|
|
|
struct timespec sleep_period = {
|
|
|
|
.tv_sec = duration / 1000,
|
|
|
|
.tv_nsec = (duration % 1000) * 1000000
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Sleep for specified interval */
|
|
|
|
nanosleep(&sleep_period, NULL);
|
|
|
|
|
|
|
|
}
|
|
|
|
|