blob: 3ab892933cf92f1407f96aa4a4faafbc8ebc28ad [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>
5#include <time.h>
6
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
22 /** Converts relative timeout specified as a duration in milliseconds to a struct timespec structure
23 specifying current time plus timeout
24
25 @param struct timespec& current time plus timeout result
26 @param timeout time to delay in milliseconds */
27
28 static const void toAbsoluteTimespec(struct timespec& result, long long value) {
29
30 // XXX Darwin doesn't seem to have any readily useable hi-res clock.
31
32 time_t seconds;
33
34 assert(time(&seconds) != (time_t)-1);
35
36 seconds+= (value / 1000);
37
38 long nanoseconds = (value % 1000) * 1000000;
39
40 result.tv_sec = seconds + (nanoseconds / 1000000000);
41
42 result.tv_nsec = nanoseconds % 1000000000;
43 }
44
45 /** Converts absolute timespec to milliseconds from epoch */
46
47 static const void toMilliseconds(long long& result, const struct timespec& value) {
48
49 result = value.tv_sec * 1000 + value.tv_nsec * 1000000;
50 }
51
52 /** Get current time as milliseconds from epoch */
53
54 static const long long currentTime() {
55
56 time_t now;
57
58 time(&now);
59
60 return (long long)now * 1000;
61 }
62};
63
64
65}}} // facebook::thrift::concurrency
66
67#endif // !defined(_concurrency_Util_h_)