blob: 18a221d5763e1812974b17b585cee56c73c7dcad [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 Reiss3bb5e052010-01-25 19:31:31 +000038 * platform dependent stuff. 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;
David Reiss7a2065d2010-03-09 05:20:04 +000050 static const int64_t NS_PER_US = NS_PER_S / US_PER_S;
David Reiss631dcb42008-03-05 07:51:40 +000051 static const int64_t US_PER_MS = US_PER_S / MS_PER_S;
Marc Slemkoc7782972006-07-25 02:26:35 +000052
Marc Slemko0e53ccd2006-07-17 23:51:05 +000053 public:
54
Mark Sleef5f2be42006-09-05 21:05:31 +000055 /**
David Reiss631dcb42008-03-05 07:51:40 +000056 * Converts millisecond timestamp into a timespec struct
Mark Sleef5f2be42006-09-05 21:05:31 +000057 *
58 * @param struct timespec& result
59 * @param time or duration in milliseconds
60 */
Mark Slee9b82d272007-05-23 05:16:07 +000061 static void toTimespec(struct timespec& result, int64_t value) {
David Reiss0c90f6f2008-02-06 22:18:40 +000062 result.tv_sec = value / MS_PER_S; // ms to s
Marc Slemkoc7782972006-07-25 02:26:35 +000063 result.tv_nsec = (value % MS_PER_S) * NS_PER_MS; // ms to ns
Marc Slemko0e53ccd2006-07-17 23:51:05 +000064 }
65
David Reiss631dcb42008-03-05 07:51:40 +000066 static void toTimeval(struct timeval& result, int64_t value) {
67 result.tv_sec = value / MS_PER_S; // ms to s
68 result.tv_usec = (value % MS_PER_S) * US_PER_MS; // ms to us
69 }
70
David Reiss7a2065d2010-03-09 05:20:04 +000071 static const void toTicks(int64_t& result, int64_t secs, int64_t oldTicks,
72 int64_t oldTicksPerSec, int64_t newTicksPerSec) {
73 result = secs * newTicksPerSec;
74 result += oldTicks * newTicksPerSec / oldTicksPerSec;
75
76 int64_t oldPerNew = oldTicksPerSec / newTicksPerSec;
77 if (oldPerNew && ((oldTicks % oldPerNew) >= (oldPerNew / 2))) {
78 ++result;
79 }
80 }
81 /**
82 * Converts struct timespec to arbitrary-sized ticks since epoch
83 */
84 static const void toTicks(int64_t& result,
85 const struct timespec& value,
86 int64_t ticksPerSec) {
87 return toTicks(result, value.tv_sec, value.tv_nsec, NS_PER_S, ticksPerSec);
88 }
89
90 /**
91 * Converts struct timeval to arbitrary-sized ticks since epoch
92 */
93 static const void toTicks(int64_t& result,
94 const struct timeval& value,
95 int64_t ticksPerSec) {
96 return toTicks(result, value.tv_sec, value.tv_usec, US_PER_S, ticksPerSec);
97 }
98
Mark Sleef5f2be42006-09-05 21:05:31 +000099 /**
David Reiss631dcb42008-03-05 07:51:40 +0000100 * Converts struct timespec to milliseconds
Mark Sleef5f2be42006-09-05 21:05:31 +0000101 */
David Reiss7a2065d2010-03-09 05:20:04 +0000102 static const void toMilliseconds(int64_t& result,
103 const struct timespec& value) {
104 return toTicks(result, value, MS_PER_S);
David Reiss631dcb42008-03-05 07:51:40 +0000105 }
106
107 /**
108 * Converts struct timeval to milliseconds
109 */
David Reiss7a2065d2010-03-09 05:20:04 +0000110 static const void toMilliseconds(int64_t& result,
111 const struct timeval& value) {
112 return toTicks(result, value, MS_PER_S);
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000113 }
114
Mark Sleef5f2be42006-09-05 21:05:31 +0000115 /**
David Reiss7a2065d2010-03-09 05:20:04 +0000116 * Converts struct timespec to microseconds
117 */
118 static const void toUsec(int64_t& result, const struct timespec& value) {
119 return toTicks(result, value, US_PER_S);
120 }
121
122 /**
123 * Converts struct timeval to microseconds
124 */
125 static const void toUsec(int64_t& result, const struct timeval& value) {
126 return toTicks(result, value, US_PER_S);
127 }
128
129 /**
130 * Get current time as a number of arbitrary-size ticks from epoch
131 */
132 static const int64_t currentTimeTicks(int64_t ticksPerSec);
133
134 /**
Mark Sleef5f2be42006-09-05 21:05:31 +0000135 * Get current time as milliseconds from epoch
136 */
David Reiss7a2065d2010-03-09 05:20:04 +0000137 static const int64_t currentTime() { return currentTimeTicks(MS_PER_S); }
138
139 /**
140 * Get current time as micros from epoch
141 */
142 static const int64_t currentTimeUsec() { return currentTimeTicks(US_PER_S); }
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000143};
144
T Jake Lucianib5e62212009-01-31 22:36:20 +0000145}}} // apache::thrift::concurrency
Marc Slemko0e53ccd2006-07-17 23:51:05 +0000146
Mark Sleef5f2be42006-09-05 21:05:31 +0000147#endif // #ifndef _THRIFT_CONCURRENCY_UTIL_H_