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