David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | /* |
| 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 Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame] | 19 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 20 | #ifndef _THRIFT_CONCURRENCY_UTIL_H_ |
| 21 | #define _THRIFT_CONCURRENCY_UTIL_H_ 1 |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 22 | |
| 23 | #include <assert.h> |
Marc Slemko | 03dedd9 | 2006-07-20 00:58:47 +0000 | [diff] [blame] | 24 | #include <stddef.h> |
David Reiss | 53f18f0 | 2008-07-11 00:45:29 +0000 | [diff] [blame] | 25 | #include <stdint.h> |
Marc Slemko | e6889de | 2006-08-12 00:32:53 +0000 | [diff] [blame] | 26 | #include <time.h> |
Roger Meier | 2fa9c31 | 2011-09-05 19:15:53 +0000 | [diff] [blame] | 27 | |
| 28 | #ifdef HAVE_SYS_TIME_H |
Marc Slemko | d42a2c2 | 2006-08-10 03:30:18 +0000 | [diff] [blame] | 29 | #include <sys/time.h> |
Roger Meier | 2fa9c31 | 2011-09-05 19:15:53 +0000 | [diff] [blame] | 30 | #endif |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 31 | |
Roger Meier | 7158d33 | 2013-06-29 12:48:05 +0200 | [diff] [blame] | 32 | #include <thrift/transport/PlatformSocket.h> |
| 33 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 34 | namespace apache { namespace thrift { namespace concurrency { |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 35 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 36 | /** |
| 37 | * Utility methods |
| 38 | * |
| 39 | * This class contains basic utility methods for converting time formats, |
| 40 | * and other common platform-dependent concurrency operations. |
| 41 | * It should not be included in API headers for other concurrency library |
| 42 | * headers, since it will, by definition, pull in all sorts of horrid |
David Reiss | 3bb5e05 | 2010-01-25 19:31:31 +0000 | [diff] [blame] | 43 | * platform dependent stuff. Rather it should be inluded directly in |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 44 | * concurrency library implementation source. |
| 45 | * |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 46 | * @version $Id:$ |
| 47 | */ |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 48 | class Util { |
| 49 | |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 50 | static const int64_t NS_PER_S = 1000000000LL; |
David Reiss | 631dcb4 | 2008-03-05 07:51:40 +0000 | [diff] [blame] | 51 | static const int64_t US_PER_S = 1000000LL; |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 52 | static const int64_t MS_PER_S = 1000LL; |
David Reiss | 631dcb4 | 2008-03-05 07:51:40 +0000 | [diff] [blame] | 53 | |
| 54 | static const int64_t NS_PER_MS = NS_PER_S / MS_PER_S; |
David Reiss | 7a2065d | 2010-03-09 05:20:04 +0000 | [diff] [blame] | 55 | static const int64_t NS_PER_US = NS_PER_S / US_PER_S; |
David Reiss | 631dcb4 | 2008-03-05 07:51:40 +0000 | [diff] [blame] | 56 | static const int64_t US_PER_MS = US_PER_S / MS_PER_S; |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 57 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 58 | public: |
| 59 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 60 | /** |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 61 | * Converts millisecond timestamp into a THRIFT_TIMESPEC struct |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 62 | * |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 63 | * @param struct THRIFT_TIMESPEC& result |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 64 | * @param time or duration in milliseconds |
| 65 | */ |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 66 | static void toTimespec(struct THRIFT_TIMESPEC& result, int64_t value) { |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 67 | result.tv_sec = value / MS_PER_S; // ms to s |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 68 | result.tv_nsec = (value % MS_PER_S) * NS_PER_MS; // ms to ns |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 69 | } |
| 70 | |
David Reiss | 631dcb4 | 2008-03-05 07:51:40 +0000 | [diff] [blame] | 71 | static void toTimeval(struct timeval& result, int64_t value) { |
Roger Meier | 8252577 | 2012-11-16 00:38:27 +0000 | [diff] [blame] | 72 | result.tv_sec = static_cast<uint32_t>(value / MS_PER_S); // ms to s |
| 73 | result.tv_usec = static_cast<uint32_t>((value % MS_PER_S) * US_PER_MS); // ms to us |
David Reiss | 631dcb4 | 2008-03-05 07:51:40 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 76 | static void toTicks(int64_t& result, int64_t secs, int64_t oldTicks, |
Roger Meier | 7158d33 | 2013-06-29 12:48:05 +0200 | [diff] [blame] | 77 | int64_t oldTicksPerSec, int64_t newTicksPerSec) { |
David Reiss | 7a2065d | 2010-03-09 05:20:04 +0000 | [diff] [blame] | 78 | result = secs * newTicksPerSec; |
| 79 | result += oldTicks * newTicksPerSec / oldTicksPerSec; |
| 80 | |
| 81 | int64_t oldPerNew = oldTicksPerSec / newTicksPerSec; |
| 82 | if (oldPerNew && ((oldTicks % oldPerNew) >= (oldPerNew / 2))) { |
| 83 | ++result; |
| 84 | } |
| 85 | } |
| 86 | /** |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 87 | * Converts struct THRIFT_TIMESPEC to arbitrary-sized ticks since epoch |
David Reiss | 7a2065d | 2010-03-09 05:20:04 +0000 | [diff] [blame] | 88 | */ |
Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 89 | static void toTicks(int64_t& result, |
Roger Meier | 7158d33 | 2013-06-29 12:48:05 +0200 | [diff] [blame] | 90 | const struct THRIFT_TIMESPEC& value, |
| 91 | int64_t ticksPerSec) { |
David Reiss | 7a2065d | 2010-03-09 05:20:04 +0000 | [diff] [blame] | 92 | return toTicks(result, value.tv_sec, value.tv_nsec, NS_PER_S, ticksPerSec); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Converts struct timeval to arbitrary-sized ticks since epoch |
| 97 | */ |
Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 98 | static void toTicks(int64_t& result, |
Roger Meier | 7158d33 | 2013-06-29 12:48:05 +0200 | [diff] [blame] | 99 | const struct timeval& value, |
| 100 | int64_t ticksPerSec) { |
David Reiss | 7a2065d | 2010-03-09 05:20:04 +0000 | [diff] [blame] | 101 | return toTicks(result, value.tv_sec, value.tv_usec, US_PER_S, ticksPerSec); |
| 102 | } |
| 103 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 104 | /** |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 105 | * Converts struct THRIFT_TIMESPEC to milliseconds |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 106 | */ |
Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 107 | static void toMilliseconds(int64_t& result, |
Roger Meier | 7158d33 | 2013-06-29 12:48:05 +0200 | [diff] [blame] | 108 | const struct THRIFT_TIMESPEC& value) { |
David Reiss | 7a2065d | 2010-03-09 05:20:04 +0000 | [diff] [blame] | 109 | return toTicks(result, value, MS_PER_S); |
David Reiss | 631dcb4 | 2008-03-05 07:51:40 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Converts struct timeval to milliseconds |
| 114 | */ |
Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 115 | static void toMilliseconds(int64_t& result, |
Roger Meier | 7158d33 | 2013-06-29 12:48:05 +0200 | [diff] [blame] | 116 | const struct timeval& value) { |
David Reiss | 7a2065d | 2010-03-09 05:20:04 +0000 | [diff] [blame] | 117 | return toTicks(result, value, MS_PER_S); |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 120 | /** |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 121 | * Converts struct THRIFT_TIMESPEC to microseconds |
David Reiss | 7a2065d | 2010-03-09 05:20:04 +0000 | [diff] [blame] | 122 | */ |
Carl Yeksigian | 7cb7fc8 | 2013-06-07 07:33:01 -0400 | [diff] [blame] | 123 | static void toUsec(int64_t& result, const struct THRIFT_TIMESPEC& value) { |
David Reiss | 7a2065d | 2010-03-09 05:20:04 +0000 | [diff] [blame] | 124 | return toTicks(result, value, US_PER_S); |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Converts struct timeval to microseconds |
| 129 | */ |
Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 130 | static void toUsec(int64_t& result, const struct timeval& value) { |
David Reiss | 7a2065d | 2010-03-09 05:20:04 +0000 | [diff] [blame] | 131 | return toTicks(result, value, US_PER_S); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Get current time as a number of arbitrary-size ticks from epoch |
| 136 | */ |
Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 137 | static int64_t currentTimeTicks(int64_t ticksPerSec); |
David Reiss | 7a2065d | 2010-03-09 05:20:04 +0000 | [diff] [blame] | 138 | |
| 139 | /** |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 140 | * Get current time as milliseconds from epoch |
| 141 | */ |
Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 142 | static int64_t currentTime() { return currentTimeTicks(MS_PER_S); } |
David Reiss | 7a2065d | 2010-03-09 05:20:04 +0000 | [diff] [blame] | 143 | |
| 144 | /** |
| 145 | * Get current time as micros from epoch |
| 146 | */ |
Roger Meier | 3b771a1 | 2010-11-17 22:11:26 +0000 | [diff] [blame] | 147 | static int64_t currentTimeUsec() { return currentTimeTicks(US_PER_S); } |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 148 | }; |
| 149 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 150 | }}} // apache::thrift::concurrency |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 151 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 152 | #endif // #ifndef _THRIFT_CONCURRENCY_UTIL_H_ |