blob: 3342653960a2220f0c1381dd0355390c1e89262e [file] [log] [blame]
Marc Slemko0e53ccd2006-07-17 23:51:05 +00001#if !defined(_concurrency_Util_h_)
2#define _concurrency_Util_h_ 1
3
Marc Slemkoc7782972006-07-25 02:26:35 +00004#include <config.h>
5
Marc Slemko0e53ccd2006-07-17 23:51:05 +00006#include <assert.h>
Marc Slemko03dedd92006-07-20 00:58:47 +00007#include <stddef.h>
Marc Slemkod42a2c22006-08-10 03:30:18 +00008#include <sys/time.h>
Marc Slemko0e53ccd2006-07-17 23:51:05 +00009
10namespace facebook { namespace thrift { namespace concurrency {
11
12/** Utility methods
13
14 This class contains basic utility methods for converting time formats, and other common platform-dependent concurrency operations.
15 It should not be included in API headers for other concurrency library headers, since it will, by definition, pull in all sorts of
16 horrid platform dependent crap. Rather it should be inluded directly in concurrency library implementation source.
17
18 @author marc
19 @version $Id:$ */
20
21class Util {
22
Marc Slemkoc7782972006-07-25 02:26:35 +000023 static const long long NS_PER_S = 1000000000LL;
24
25 static const long long MS_PER_S = 1000LL;
26
27 static const long long NS_PER_MS = 1000000LL;
28
Marc Slemko0e53ccd2006-07-17 23:51:05 +000029 public:
30
Marc Slemko9f27a4e2006-07-19 20:02:22 +000031 /** Converts timespec to milliseconds
Marc Slemko0e53ccd2006-07-17 23:51:05 +000032
Marc Slemko9f27a4e2006-07-19 20:02:22 +000033 @param struct timespec& result
34 @param time or duration in milliseconds */
Marc Slemko0e53ccd2006-07-17 23:51:05 +000035
Marc Slemko9f27a4e2006-07-19 20:02:22 +000036 static void toTimespec(struct timespec& result, long long value) {
Marc Slemkoc7782972006-07-25 02:26:35 +000037
38 result.tv_sec = value / MS_PER_S; // ms to s
Marc Slemko0e53ccd2006-07-17 23:51:05 +000039
Marc Slemkoc7782972006-07-25 02:26:35 +000040 result.tv_nsec = (value % MS_PER_S) * NS_PER_MS; // ms to ns
Marc Slemko0e53ccd2006-07-17 23:51:05 +000041 }
42
Marc Slemko9f27a4e2006-07-19 20:02:22 +000043 /** Converts timespec to milliseconds */
Marc Slemko0e53ccd2006-07-17 23:51:05 +000044
45 static const void toMilliseconds(long long& result, const struct timespec& value) {
46
Marc Slemkoc7782972006-07-25 02:26:35 +000047 result = (value.tv_sec * MS_PER_S) + (value.tv_nsec / NS_PER_MS) + (value.tv_nsec % NS_PER_MS >= 500000 ? 1 : 0) ;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000048 }
49
50 /** Get current time as milliseconds from epoch */
51
52 static const long long currentTime() {
53
Marc Slemkoc7782972006-07-25 02:26:35 +000054#if defined(HAVE_CLOCK_GETTIME)
55
56 struct timespec now;
57
Marc Slemko6f038a72006-08-03 18:58:09 +000058 assert(clock_gettime(CLOCK_REALTIME, &now) == 0);
Marc Slemkoc7782972006-07-25 02:26:35 +000059
Marc Slemko6f038a72006-08-03 18:58:09 +000060 return (now.tv_sec * MS_PER_S) + (now.tv_nsec / NS_PER_MS) + (now.tv_nsec % NS_PER_MS >= 500000 ? 1 : 0) ;
Marc Slemkoc7782972006-07-25 02:26:35 +000061
62#elif defined(HAVE_GETTIMEOFDAY)
63
Marc Slemko9f27a4e2006-07-19 20:02:22 +000064 struct timeval now;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000065
Marc Slemko9f27a4e2006-07-19 20:02:22 +000066 assert(gettimeofday(&now, NULL) == 0);
Marc Slemko0e53ccd2006-07-17 23:51:05 +000067
Marc Slemkoc7782972006-07-25 02:26:35 +000068 return (((long long)now.tv_sec) * MS_PER_S) + (now.tv_usec / MS_PER_S) + (now.tv_usec % MS_PER_S >= 500 ? 1 : 0);
Marc Slemko6f038a72006-08-03 18:58:09 +000069
Marc Slemkoc7782972006-07-25 02:26:35 +000070#endif // defined(HAVE_GETTIMEDAY)
Marc Slemko0e53ccd2006-07-17 23:51:05 +000071 }
72};
73
Marc Slemko0e53ccd2006-07-17 23:51:05 +000074}}} // facebook::thrift::concurrency
75
76#endif // !defined(_concurrency_Util_h_)