blob: d04435d56bac093426421fc6596e4d858cbb8600 [file] [log] [blame]
Marc Slemko0e53ccd2006-07-17 23:51:05 +00001#if !defined(_concurrency_Util_h_)
2#define _concurrency_Util_h_ 1
3
4#include <assert.h>
Marc Slemko9f27a4e2006-07-19 20:02:22 +00005#include <sys/time.h>
Marc Slemko0e53ccd2006-07-17 23:51:05 +00006
7namespace 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
18class Util {
19
20 public:
21
Marc Slemko9f27a4e2006-07-19 20:02:22 +000022 /** Converts timespec to milliseconds
Marc Slemko0e53ccd2006-07-17 23:51:05 +000023
Marc Slemko9f27a4e2006-07-19 20:02:22 +000024 @param struct timespec& result
25 @param time or duration in milliseconds */
Marc Slemko0e53ccd2006-07-17 23:51:05 +000026
Marc Slemko9f27a4e2006-07-19 20:02:22 +000027 static void toTimespec(struct timespec& result, long long value) {
Marc Slemko0e53ccd2006-07-17 23:51:05 +000028
Marc Slemko9f27a4e2006-07-19 20:02:22 +000029 result.tv_sec = value / 1000; // ms to s
Marc Slemko0e53ccd2006-07-17 23:51:05 +000030
Marc Slemko9f27a4e2006-07-19 20:02:22 +000031 result.tv_nsec = (value % 1000) * 1000000; // ms to ns
Marc Slemko0e53ccd2006-07-17 23:51:05 +000032 }
33
Marc Slemko9f27a4e2006-07-19 20:02:22 +000034 /** Converts timespec to milliseconds */
Marc Slemko0e53ccd2006-07-17 23:51:05 +000035
36 static const void toMilliseconds(long long& result, const struct timespec& value) {
37
Marc Slemko9f27a4e2006-07-19 20:02:22 +000038 result = value.tv_sec * 1000 + value.tv_nsec / 1000000;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000039 }
40
41 /** Get current time as milliseconds from epoch */
42
43 static const long long currentTime() {
44
Marc Slemko9f27a4e2006-07-19 20:02:22 +000045 struct timeval now;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000046
Marc Slemko9f27a4e2006-07-19 20:02:22 +000047 assert(gettimeofday(&now, NULL) == 0);
Marc Slemko0e53ccd2006-07-17 23:51:05 +000048
Marc Slemko9f27a4e2006-07-19 20:02:22 +000049 return ((long long)now.tv_sec) * 1000LL + now.tv_usec / 1000;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000050 }
51};
52
53
54}}} // facebook::thrift::concurrency
55
56#endif // !defined(_concurrency_Util_h_)