blob: 73b0c0520fd0a5e62a276dea47f18b1b03d767ae [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);
David Reiss7a2065d2010-03-09 05:20:04 +000041 toTicks(result, now, ticksPerSec);
David Reiss53f18f02008-07-11 00:45:29 +000042#elif defined(HAVE_GETTIMEOFDAY)
43 struct timeval now;
44 int ret = gettimeofday(&now, NULL);
45 assert(ret == 0);
David Reiss7a2065d2010-03-09 05:20:04 +000046 toTicks(result, now, ticksPerSec);
David Reiss53f18f02008-07-11 00:45:29 +000047#else
48#error "No high-precision clock is available."
49#endif // defined(HAVE_CLOCK_GETTIME)
50
51 return result;
52}
53
T Jake Lucianib5e62212009-01-31 22:36:20 +000054}}} // apache::thrift::concurrency