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> |
Marc Slemko | d42a2c2 | 2006-08-10 03:30:18 +0000 | [diff] [blame] | 27 | #include <sys/time.h> |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 28 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 29 | namespace apache { namespace thrift { namespace concurrency { |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 30 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 31 | /** |
| 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 Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 38 | * platform dependent crap. Rather it should be inluded directly in |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 39 | * concurrency library implementation source. |
| 40 | * |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 41 | * @version $Id:$ |
| 42 | */ |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 43 | class Util { |
| 44 | |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 45 | static const int64_t NS_PER_S = 1000000000LL; |
David Reiss | 631dcb4 | 2008-03-05 07:51:40 +0000 | [diff] [blame] | 46 | static const int64_t US_PER_S = 1000000LL; |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 47 | static const int64_t MS_PER_S = 1000LL; |
David Reiss | 631dcb4 | 2008-03-05 07:51:40 +0000 | [diff] [blame] | 48 | |
| 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 Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 51 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 52 | public: |
| 53 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 54 | /** |
David Reiss | 631dcb4 | 2008-03-05 07:51:40 +0000 | [diff] [blame] | 55 | * Converts millisecond timestamp into a timespec struct |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 56 | * |
| 57 | * @param struct timespec& result |
| 58 | * @param time or duration in milliseconds |
| 59 | */ |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 60 | static void toTimespec(struct timespec& result, int64_t value) { |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 61 | result.tv_sec = value / MS_PER_S; // ms to s |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 62 | 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] | 63 | } |
| 64 | |
David Reiss | 631dcb4 | 2008-03-05 07:51:40 +0000 | [diff] [blame] | 65 | 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 Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 70 | /** |
David Reiss | 631dcb4 | 2008-03-05 07:51:40 +0000 | [diff] [blame] | 71 | * Converts struct timespec to milliseconds |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 72 | */ |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 73 | static const void toMilliseconds(int64_t& result, const struct timespec& value) { |
David Reiss | 631dcb4 | 2008-03-05 07:51:40 +0000 | [diff] [blame] | 74 | 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 Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 92 | /** |
| 93 | * Get current time as milliseconds from epoch |
| 94 | */ |
David Reiss | 53f18f0 | 2008-07-11 00:45:29 +0000 | [diff] [blame] | 95 | static const int64_t currentTime(); |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 98 | }}} // apache::thrift::concurrency |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 99 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 100 | #endif // #ifndef _THRIFT_CONCURRENCY_UTIL_H_ |