Mark Slee | 9f0c651 | 2007-02-28 23:58:26 +0000 | [diff] [blame^] | 1 | // Copyright (c) 2006- Facebook |
| 2 | // Distributed under the Thrift Software License |
| 3 | // |
| 4 | // See accompanying file LICENSE or visit the Thrift site at: |
| 5 | // http://developers.facebook.com/thrift/ |
| 6 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 7 | #ifndef _THRIFT_CONCURRENCY_UTIL_H_ |
| 8 | #define _THRIFT_CONCURRENCY_UTIL_H_ 1 |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 9 | |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 10 | #include <config.h> |
| 11 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 12 | #include <assert.h> |
Marc Slemko | 03dedd9 | 2006-07-20 00:58:47 +0000 | [diff] [blame] | 13 | #include <stddef.h> |
Marc Slemko | e6889de | 2006-08-12 00:32:53 +0000 | [diff] [blame] | 14 | #if defined(HAVE_CLOCK_GETTIME) |
| 15 | #include <time.h> |
| 16 | #else // defined(HAVE_CLOCK_GETTIME) |
Marc Slemko | d42a2c2 | 2006-08-10 03:30:18 +0000 | [diff] [blame] | 17 | #include <sys/time.h> |
Marc Slemko | e6889de | 2006-08-12 00:32:53 +0000 | [diff] [blame] | 18 | #endif // defined(HAVE_CLOCK_GETTIME) |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 19 | |
| 20 | namespace facebook { namespace thrift { namespace concurrency { |
| 21 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 22 | /** |
| 23 | * Utility methods |
| 24 | * |
| 25 | * This class contains basic utility methods for converting time formats, |
| 26 | * and other common platform-dependent concurrency operations. |
| 27 | * It should not be included in API headers for other concurrency library |
| 28 | * headers, since it will, by definition, pull in all sorts of horrid |
| 29 | * platform dependent crap. Rather it should be inluded directly in |
| 30 | * concurrency library implementation source. |
| 31 | * |
| 32 | * @author marc |
| 33 | * @version $Id:$ |
| 34 | */ |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 35 | class Util { |
| 36 | |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 37 | static const long long NS_PER_S = 1000000000LL; |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 38 | static const long long MS_PER_S = 1000LL; |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 39 | static const long long NS_PER_MS = 1000000LL; |
| 40 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 41 | public: |
| 42 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 43 | /** |
| 44 | * Converts timespec to milliseconds |
| 45 | * |
| 46 | * @param struct timespec& result |
| 47 | * @param time or duration in milliseconds |
| 48 | */ |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 49 | static void toTimespec(struct timespec& result, long long value) { |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 50 | result.tv_sec = value / MS_PER_S; // ms to s |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 51 | 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] | 52 | } |
| 53 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 54 | /** |
| 55 | * Converts timespec to milliseconds |
| 56 | */ |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 57 | static const void toMilliseconds(long long& result, const struct timespec& value) { |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 58 | result = |
| 59 | (value.tv_sec * MS_PER_S) + |
| 60 | (value.tv_nsec / NS_PER_MS) + |
| 61 | (value.tv_nsec % NS_PER_MS >= 500000 ? 1 : 0); |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 64 | /** |
| 65 | * Get current time as milliseconds from epoch |
| 66 | */ |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 67 | static const long long currentTime() { |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 68 | #if defined(HAVE_CLOCK_GETTIME) |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 69 | struct timespec now; |
Marc Slemko | 6f038a7 | 2006-08-03 18:58:09 +0000 | [diff] [blame] | 70 | assert(clock_gettime(CLOCK_REALTIME, &now) == 0); |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 71 | return |
| 72 | (now.tv_sec * MS_PER_S) + |
| 73 | (now.tv_nsec / NS_PER_MS) + |
| 74 | (now.tv_nsec % NS_PER_MS >= 500000 ? 1 : 0) ; |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 75 | #elif defined(HAVE_GETTIMEOFDAY) |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 76 | struct timeval now; |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 77 | assert(gettimeofday(&now, NULL) == 0); |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 78 | return |
| 79 | (((long long)now.tv_sec) * MS_PER_S) + |
| 80 | (now.tv_usec / MS_PER_S) + |
| 81 | (now.tv_usec % MS_PER_S >= 500 ? 1 : 0); |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 82 | #endif // defined(HAVE_GETTIMEDAY) |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 83 | } |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 84 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 85 | }; |
| 86 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 87 | }}} // facebook::thrift::concurrency |
| 88 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 89 | #endif // #ifndef _THRIFT_CONCURRENCY_UTIL_H_ |