blob: 8467a2cbf598909cd6d1732f040cbb3ab5e3fdcd [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 Slemko03dedd92006-07-20 00:58:47 +00005#include <stddef.h>
Marc Slemko9f27a4e2006-07-19 20:02:22 +00006#include <sys/time.h>
Marc Slemko0e53ccd2006-07-17 23:51:05 +00007
8namespace 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
19class Util {
20
21 public:
22
Marc Slemko9f27a4e2006-07-19 20:02:22 +000023 /** Converts timespec to milliseconds
Marc Slemko0e53ccd2006-07-17 23:51:05 +000024
Marc Slemko9f27a4e2006-07-19 20:02:22 +000025 @param struct timespec& result
26 @param time or duration in milliseconds */
Marc Slemko0e53ccd2006-07-17 23:51:05 +000027
Marc Slemko9f27a4e2006-07-19 20:02:22 +000028 static void toTimespec(struct timespec& result, long long value) {
Marc Slemko0e53ccd2006-07-17 23:51:05 +000029
Marc Slemko9f27a4e2006-07-19 20:02:22 +000030 result.tv_sec = value / 1000; // ms to s
Marc Slemko0e53ccd2006-07-17 23:51:05 +000031
Marc Slemko9f27a4e2006-07-19 20:02:22 +000032 result.tv_nsec = (value % 1000) * 1000000; // ms to ns
Marc Slemko0e53ccd2006-07-17 23:51:05 +000033 }
34
Marc Slemko9f27a4e2006-07-19 20:02:22 +000035 /** Converts timespec to milliseconds */
Marc Slemko0e53ccd2006-07-17 23:51:05 +000036
37 static const void toMilliseconds(long long& result, const struct timespec& value) {
38
Marc Slemko9f27a4e2006-07-19 20:02:22 +000039 result = value.tv_sec * 1000 + value.tv_nsec / 1000000;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000040 }
41
42 /** Get current time as milliseconds from epoch */
43
44 static const long long currentTime() {
45
Marc Slemko9f27a4e2006-07-19 20:02:22 +000046 struct timeval now;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000047
Marc Slemko9f27a4e2006-07-19 20:02:22 +000048 assert(gettimeofday(&now, NULL) == 0);
Marc Slemko0e53ccd2006-07-17 23:51:05 +000049
Marc Slemko9f27a4e2006-07-19 20:02:22 +000050 return ((long long)now.tv_sec) * 1000LL + now.tv_usec / 1000;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000051 }
52};
53
54
55}}} // facebook::thrift::concurrency
56
57#endif // !defined(_concurrency_Util_h_)