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