blob: 764b6f5dcd1e1b03732021cc1cd92a576403ccea [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
David Reiss53f18f02008-07-11 00:45:29 +000019
David Reiss53f18f02008-07-11 00:45:29 +000020#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
Roger Meier2fa9c312011-09-05 19:15:53 +000024#include "Util.h"
25
David Reiss53f18f02008-07-11 00:45:29 +000026#if defined(HAVE_CLOCK_GETTIME)
27#include <time.h>
Roger Meier2fa9c312011-09-05 19:15:53 +000028#elif defined(HAVE_SYS_TIME_H)
David Reiss53f18f02008-07-11 00:45:29 +000029#include <sys/time.h>
30#endif // defined(HAVE_CLOCK_GETTIME)
31
T Jake Lucianib5e62212009-01-31 22:36:20 +000032namespace apache { namespace thrift { namespace concurrency {
David Reiss53f18f02008-07-11 00:45:29 +000033
Roger Meier3b771a12010-11-17 22:11:26 +000034int64_t Util::currentTimeTicks(int64_t ticksPerSec) {
David Reiss53f18f02008-07-11 00:45:29 +000035 int64_t result;
36
37#if defined(HAVE_CLOCK_GETTIME)
38 struct timespec now;
39 int ret = clock_gettime(CLOCK_REALTIME, &now);
40 assert(ret == 0);
Roger Meierb69d24d2012-10-04 18:02:15 +000041 ret = ret; //squelching "unused variable" warning
David Reiss7a2065d2010-03-09 05:20:04 +000042 toTicks(result, now, ticksPerSec);
David Reiss53f18f02008-07-11 00:45:29 +000043#elif defined(HAVE_GETTIMEOFDAY)
44 struct timeval now;
45 int ret = gettimeofday(&now, NULL);
46 assert(ret == 0);
David Reiss7a2065d2010-03-09 05:20:04 +000047 toTicks(result, now, ticksPerSec);
David Reiss53f18f02008-07-11 00:45:29 +000048#else
49#error "No high-precision clock is available."
50#endif // defined(HAVE_CLOCK_GETTIME)
51
52 return result;
53}
54
T Jake Lucianib5e62212009-01-31 22:36:20 +000055}}} // apache::thrift::concurrency