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