blob: df37471bbfc44cd74a28e9edee82d0d96a76639e [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 Slemkoe6889de2006-08-12 00:32:53 +00008#if defined(HAVE_CLOCK_GETTIME)
9#include <time.h>
10#else // defined(HAVE_CLOCK_GETTIME)
Marc Slemkod42a2c22006-08-10 03:30:18 +000011#include <sys/time.h>
Marc Slemkoe6889de2006-08-12 00:32:53 +000012#endif // defined(HAVE_CLOCK_GETTIME)
Marc Slemko0e53ccd2006-07-17 23:51:05 +000013
14namespace facebook { namespace thrift { namespace concurrency {
15
16/** Utility methods
17
18 This class contains basic utility methods for converting time formats, and other common platform-dependent concurrency operations.
19 It should not be included in API headers for other concurrency library headers, since it will, by definition, pull in all sorts of
20 horrid platform dependent crap. Rather it should be inluded directly in concurrency library implementation source.
21
22 @author marc
23 @version $Id:$ */
24
25class Util {
26
Marc Slemkoc7782972006-07-25 02:26:35 +000027 static const long long NS_PER_S = 1000000000LL;
28
29 static const long long MS_PER_S = 1000LL;
30
31 static const long long NS_PER_MS = 1000000LL;
32
Marc Slemko0e53ccd2006-07-17 23:51:05 +000033 public:
34
Marc Slemko9f27a4e2006-07-19 20:02:22 +000035 /** Converts timespec to milliseconds
Marc Slemko0e53ccd2006-07-17 23:51:05 +000036
Marc Slemko9f27a4e2006-07-19 20:02:22 +000037 @param struct timespec& result
38 @param time or duration in milliseconds */
Marc Slemko0e53ccd2006-07-17 23:51:05 +000039
Marc Slemko9f27a4e2006-07-19 20:02:22 +000040 static void toTimespec(struct timespec& result, long long value) {
Marc Slemkoc7782972006-07-25 02:26:35 +000041
42 result.tv_sec = value / MS_PER_S; // ms to s
Marc Slemko0e53ccd2006-07-17 23:51:05 +000043
Marc Slemkoc7782972006-07-25 02:26:35 +000044 result.tv_nsec = (value % MS_PER_S) * NS_PER_MS; // ms to ns
Marc Slemko0e53ccd2006-07-17 23:51:05 +000045 }
46
Marc Slemko9f27a4e2006-07-19 20:02:22 +000047 /** Converts timespec to milliseconds */
Marc Slemko0e53ccd2006-07-17 23:51:05 +000048
49 static const void toMilliseconds(long long& result, const struct timespec& value) {
50
Marc Slemkoc7782972006-07-25 02:26:35 +000051 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 +000052 }
53
54 /** Get current time as milliseconds from epoch */
55
56 static const long long currentTime() {
57
Marc Slemkoc7782972006-07-25 02:26:35 +000058#if defined(HAVE_CLOCK_GETTIME)
59
60 struct timespec now;
61
Marc Slemko6f038a72006-08-03 18:58:09 +000062 assert(clock_gettime(CLOCK_REALTIME, &now) == 0);
Marc Slemkoc7782972006-07-25 02:26:35 +000063
Marc Slemko6f038a72006-08-03 18:58:09 +000064 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 +000065
66#elif defined(HAVE_GETTIMEOFDAY)
67
Marc Slemko9f27a4e2006-07-19 20:02:22 +000068 struct timeval now;
Marc Slemko0e53ccd2006-07-17 23:51:05 +000069
Marc Slemko9f27a4e2006-07-19 20:02:22 +000070 assert(gettimeofday(&now, NULL) == 0);
Marc Slemko0e53ccd2006-07-17 23:51:05 +000071
Marc Slemkoc7782972006-07-25 02:26:35 +000072 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 +000073
Marc Slemkoc7782972006-07-25 02:26:35 +000074#endif // defined(HAVE_GETTIMEDAY)
Marc Slemko0e53ccd2006-07-17 23:51:05 +000075 }
76};
77
Marc Slemko0e53ccd2006-07-17 23:51:05 +000078}}} // facebook::thrift::concurrency
79
80#endif // !defined(_concurrency_Util_h_)