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