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 | |
Mark Slee | a8de489 | 2008-02-09 00:02:26 +0000 | [diff] [blame] | 10 | #ifdef HAVE_CONFIG_H |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 11 | #include <config.h> |
Mark Slee | a8de489 | 2008-02-09 00:02:26 +0000 | [diff] [blame] | 12 | #endif |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 13 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 14 | #include <assert.h> |
Marc Slemko | 03dedd9 | 2006-07-20 00:58:47 +0000 | [diff] [blame] | 15 | #include <stddef.h> |
Marc Slemko | e6889de | 2006-08-12 00:32:53 +0000 | [diff] [blame] | 16 | #if defined(HAVE_CLOCK_GETTIME) |
| 17 | #include <time.h> |
| 18 | #else // defined(HAVE_CLOCK_GETTIME) |
Marc Slemko | d42a2c2 | 2006-08-10 03:30:18 +0000 | [diff] [blame] | 19 | #include <sys/time.h> |
Marc Slemko | e6889de | 2006-08-12 00:32:53 +0000 | [diff] [blame] | 20 | #endif // defined(HAVE_CLOCK_GETTIME) |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 21 | |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 22 | namespace facebook { namespace thrift { namespace concurrency { |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 23 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 24 | /** |
| 25 | * Utility methods |
| 26 | * |
| 27 | * This class contains basic utility methods for converting time formats, |
| 28 | * and other common platform-dependent concurrency operations. |
| 29 | * It should not be included in API headers for other concurrency library |
| 30 | * headers, since it will, by definition, pull in all sorts of horrid |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 31 | * platform dependent crap. Rather it should be inluded directly in |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 32 | * concurrency library implementation source. |
| 33 | * |
| 34 | * @author marc |
| 35 | * @version $Id:$ |
| 36 | */ |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 37 | class Util { |
| 38 | |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 39 | static const int64_t NS_PER_S = 1000000000LL; |
David Reiss | 631dcb4 | 2008-03-05 07:51:40 +0000 | [diff] [blame] | 40 | static const int64_t US_PER_S = 1000000LL; |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 41 | static const int64_t MS_PER_S = 1000LL; |
David Reiss | 631dcb4 | 2008-03-05 07:51:40 +0000 | [diff] [blame] | 42 | |
| 43 | static const int64_t NS_PER_MS = NS_PER_S / MS_PER_S; |
| 44 | 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] | 45 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 46 | public: |
| 47 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 48 | /** |
David Reiss | 631dcb4 | 2008-03-05 07:51:40 +0000 | [diff] [blame] | 49 | * Converts millisecond timestamp into a timespec struct |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 50 | * |
| 51 | * @param struct timespec& result |
| 52 | * @param time or duration in milliseconds |
| 53 | */ |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 54 | static void toTimespec(struct timespec& result, int64_t value) { |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 55 | result.tv_sec = value / MS_PER_S; // ms to s |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 56 | 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] | 57 | } |
| 58 | |
David Reiss | 631dcb4 | 2008-03-05 07:51:40 +0000 | [diff] [blame] | 59 | static void toTimeval(struct timeval& result, int64_t value) { |
| 60 | result.tv_sec = value / MS_PER_S; // ms to s |
| 61 | result.tv_usec = (value % MS_PER_S) * US_PER_MS; // ms to us |
| 62 | } |
| 63 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 64 | /** |
David Reiss | 631dcb4 | 2008-03-05 07:51:40 +0000 | [diff] [blame] | 65 | * Converts struct timespec to milliseconds |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 66 | */ |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 67 | static const void toMilliseconds(int64_t& result, const struct timespec& value) { |
David Reiss | 631dcb4 | 2008-03-05 07:51:40 +0000 | [diff] [blame] | 68 | result = (value.tv_sec * MS_PER_S) + (value.tv_nsec / NS_PER_MS); |
| 69 | // round up -- int64_t cast is to avoid a compiler error for some GCCs |
| 70 | if (int64_t(value.tv_nsec) % NS_PER_MS >= (NS_PER_MS / 2)) { |
| 71 | ++result; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Converts struct timeval to milliseconds |
| 77 | */ |
| 78 | static const void toMilliseconds(int64_t& result, const struct timeval& value) { |
| 79 | result = (value.tv_sec * MS_PER_S) + (value.tv_usec / US_PER_MS); |
| 80 | // round up -- int64_t cast is to avoid a compiler error for some GCCs |
| 81 | if (int64_t(value.tv_usec) % US_PER_MS >= (US_PER_MS / 2)) { |
| 82 | ++result; |
| 83 | } |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 86 | /** |
| 87 | * Get current time as milliseconds from epoch |
| 88 | */ |
Mark Slee | 9b82d27 | 2007-05-23 05:16:07 +0000 | [diff] [blame] | 89 | static const int64_t currentTime() { |
David Reiss | 631dcb4 | 2008-03-05 07:51:40 +0000 | [diff] [blame] | 90 | int64_t result; |
| 91 | |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 92 | #if defined(HAVE_CLOCK_GETTIME) |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 93 | struct timespec now; |
Aditya Agarwal | 9dc5740 | 2007-03-31 17:45:12 +0000 | [diff] [blame] | 94 | int ret = clock_gettime(CLOCK_REALTIME, &now); |
Aditya Agarwal | 3f234da | 2007-04-01 01:19:57 +0000 | [diff] [blame] | 95 | assert(ret == 0); |
David Reiss | 631dcb4 | 2008-03-05 07:51:40 +0000 | [diff] [blame] | 96 | toMilliseconds(result, now); |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 97 | #elif defined(HAVE_GETTIMEOFDAY) |
Marc Slemko | 9f27a4e | 2006-07-19 20:02:22 +0000 | [diff] [blame] | 98 | struct timeval now; |
Aditya Agarwal | 9dc5740 | 2007-03-31 17:45:12 +0000 | [diff] [blame] | 99 | int ret = gettimeofday(&now, NULL); |
Aditya Agarwal | 3f234da | 2007-04-01 01:19:57 +0000 | [diff] [blame] | 100 | assert(ret == 0); |
David Reiss | 631dcb4 | 2008-03-05 07:51:40 +0000 | [diff] [blame] | 101 | toMilliseconds(result, now); |
Marc Slemko | c778297 | 2006-07-25 02:26:35 +0000 | [diff] [blame] | 102 | #endif // defined(HAVE_GETTIMEDAY) |
David Reiss | 631dcb4 | 2008-03-05 07:51:40 +0000 | [diff] [blame] | 103 | |
| 104 | return result; |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 105 | } |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 106 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 107 | }; |
| 108 | |
Marc Slemko | 0e53ccd | 2006-07-17 23:51:05 +0000 | [diff] [blame] | 109 | }}} // facebook::thrift::concurrency |
| 110 | |
Mark Slee | f5f2be4 | 2006-09-05 21:05:31 +0000 | [diff] [blame] | 111 | #endif // #ifndef _THRIFT_CONCURRENCY_UTIL_H_ |