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