blob: 25fcc208640f076aa39131c6aeb451b89c413aa5 [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 */
Mark Slee9f0c6512007-02-28 23:58:26 +000019
Mark Sleef5f2be42006-09-05 21:05:31 +000020#ifndef _THRIFT_CONCURRENCY_UTIL_H_
21#define _THRIFT_CONCURRENCY_UTIL_H_ 1
Marc Slemko0e53ccd2006-07-17 23:51:05 +000022
23#include <assert.h>
Marc Slemko03dedd92006-07-20 00:58:47 +000024#include <stddef.h>
David Reiss53f18f02008-07-11 00:45:29 +000025#include <stdint.h>
Marc Slemkoe6889de2006-08-12 00:32:53 +000026#include <time.h>
Marc Slemkod42a2c22006-08-10 03:30:18 +000027#include <sys/time.h>
Marc Slemko0e53ccd2006-07-17 23:51:05 +000028
T Jake Lucianib5e62212009-01-31 22:36:20 +000029namespace apache { namespace thrift { namespace concurrency {
Marc Slemko0e53ccd2006-07-17 23:51:05 +000030
Mark Sleef5f2be42006-09-05 21:05:31 +000031/**
32 * Utility methods
33 *
34 * This class contains basic utility methods for converting time formats,
35 * and other common platform-dependent concurrency operations.
36 * It should not be included in API headers for other concurrency library
37 * headers, since it will, by definition, pull in all sorts of horrid
David Reiss0c90f6f2008-02-06 22:18:40 +000038 * platform dependent crap. Rather it should be inluded directly in
Mark Sleef5f2be42006-09-05 21:05:31 +000039 * concurrency library implementation source.
40 *
Mark Sleef5f2be42006-09-05 21:05:31 +000041 * @version $Id:$
42 */
Marc Slemko0e53ccd2006-07-17 23:51:05 +000043class Util {
44
Mark Slee9b82d272007-05-23 05:16:07 +000045 static const int64_t NS_PER_S = 1000000000LL;
David Reiss631dcb42008-03-05 07:51:40 +000046 static const int64_t US_PER_S = 1000000LL;
Mark Slee9b82d272007-05-23 05:16:07 +000047 static const int64_t MS_PER_S = 1000LL;
David Reiss631dcb42008-03-05 07:51:40 +000048
49 static const int64_t NS_PER_MS = NS_PER_S / MS_PER_S;
50 static const int64_t US_PER_MS = US_PER_S / MS_PER_S;
Marc Slemkoc7782972006-07-25 02:26:35 +000051
Marc Slemko0e53ccd2006-07-17 23:51:05 +000052 public:
53
Mark Sleef5f2be42006-09-05 21:05:31 +000054 /**
David Reiss631dcb42008-03-05 07:51:40 +000055 * Converts millisecond timestamp into a timespec struct
Mark Sleef5f2be42006-09-05 21:05:31 +000056 *
57 * @param struct timespec& result
58 * @param time or duration in milliseconds
59 */
Mark Slee9b82d272007-05-23 05:16:07 +000060 static void toTimespec(struct timespec& result, int64_t value) {
David Reiss0c90f6f2008-02-06 22:18:40 +000061 result.tv_sec = value / MS_PER_S; // ms to s
Marc Slemkoc7782972006-07-25 02:26:35 +000062 result.tv_nsec = (value % MS_PER_S) * NS_PER_MS; // ms to ns
Marc Slemko0e53ccd2006-07-17 23:51:05 +000063 }
64
David Reiss631dcb42008-03-05 07:51:40 +000065 static void toTimeval(struct timeval& result, int64_t value) {
66 result.tv_sec = value / MS_PER_S; // ms to s
67 result.tv_usec = (value % MS_PER_S) * US_PER_MS; // ms to us
68 }
69
Mark Sleef5f2be42006-09-05 21:05:31 +000070 /**
David Reiss631dcb42008-03-05 07:51:40 +000071 * Converts struct timespec to milliseconds
Mark Sleef5f2be42006-09-05 21:05:31 +000072 */
Mark Slee9b82d272007-05-23 05:16:07 +000073 static const void toMilliseconds(int64_t& result, const struct timespec& value) {
David Reiss631dcb42008-03-05 07:51:40 +000074 result = (value.tv_sec * MS_PER_S) + (value.tv_nsec / NS_PER_MS);
75 // round up -- int64_t cast is to avoid a compiler error for some GCCs
76 if (int64_t(value.tv_nsec) % NS_PER_MS >= (NS_PER_MS / 2)) {
77 ++result;
78 }
79 }
80
81 /**
82 * Converts struct timeval to milliseconds
83 */
84 static const void toMilliseconds(int64_t& result, const struct timeval& value) {
85 result = (value.tv_sec * MS_PER_S) + (value.tv_usec / US_PER_MS);
86 // round up -- int64_t cast is to avoid a compiler error for some GCCs
87 if (int64_t(value.tv_usec) % US_PER_MS >= (US_PER_MS / 2)) {
88 ++result;
89 }
Marc Slemko0e53ccd2006-07-17 23:51:05 +000090 }
91
Mark Sleef5f2be42006-09-05 21:05:31 +000092 /**
93 * Get current time as milliseconds from epoch
94 */
David Reiss53f18f02008-07-11 00:45:29 +000095 static const int64_t currentTime();
Marc Slemko0e53ccd2006-07-17 23:51:05 +000096};
97
T Jake Lucianib5e62212009-01-31 22:36:20 +000098}}} // apache::thrift::concurrency
Marc Slemko0e53ccd2006-07-17 23:51:05 +000099
Mark Sleef5f2be42006-09-05 21:05:31 +0000100#endif // #ifndef _THRIFT_CONCURRENCY_UTIL_H_