blob: 724b69a8c11bda223de7c23569a89fcc8a23b857 [file] [log] [blame]
Mark Sleef5f2be42006-09-05 21:05:31 +00001#ifndef _THRIFT_CONCURRENCY_UTIL_H_
2#define _THRIFT_CONCURRENCY_UTIL_H_ 1
Marc Slemko0e53ccd2006-07-17 23:51:05 +00003
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
Mark Sleef5f2be42006-09-05 21:05:31 +000016/**
17 * Utility methods
18 *
19 * This class contains basic utility methods for converting time formats,
20 * and other common platform-dependent concurrency operations.
21 * It should not be included in API headers for other concurrency library
22 * headers, since it will, by definition, pull in all sorts of horrid
23 * platform dependent crap. Rather it should be inluded directly in
24 * concurrency library implementation source.
25 *
26 * @author marc
27 * @version $Id:$
28 */
Marc Slemko0e53ccd2006-07-17 23:51:05 +000029class Util {
30
Marc Slemkoc7782972006-07-25 02:26:35 +000031 static const long long NS_PER_S = 1000000000LL;
Marc Slemkoc7782972006-07-25 02:26:35 +000032 static const long long MS_PER_S = 1000LL;
Marc Slemkoc7782972006-07-25 02:26:35 +000033 static const long long NS_PER_MS = 1000000LL;
34
Marc Slemko0e53ccd2006-07-17 23:51:05 +000035 public:
36
Mark Sleef5f2be42006-09-05 21:05:31 +000037 /**
38 * Converts timespec to milliseconds
39 *
40 * @param struct timespec& result
41 * @param time or duration in milliseconds
42 */
Marc Slemko9f27a4e2006-07-19 20:02:22 +000043 static void toTimespec(struct timespec& result, long long value) {
Mark Sleef5f2be42006-09-05 21:05:31 +000044 result.tv_sec = value / MS_PER_S; // ms to s
Marc Slemkoc7782972006-07-25 02:26:35 +000045 result.tv_nsec = (value % MS_PER_S) * NS_PER_MS; // ms to ns
Marc Slemko0e53ccd2006-07-17 23:51:05 +000046 }
47
Mark Sleef5f2be42006-09-05 21:05:31 +000048 /**
49 * Converts timespec to milliseconds
50 */
Marc Slemko0e53ccd2006-07-17 23:51:05 +000051 static const void toMilliseconds(long long& result, const struct timespec& value) {
Mark Sleef5f2be42006-09-05 21:05:31 +000052 result =
53 (value.tv_sec * MS_PER_S) +
54 (value.tv_nsec / NS_PER_MS) +
55 (value.tv_nsec % NS_PER_MS >= 500000 ? 1 : 0);
Marc Slemko0e53ccd2006-07-17 23:51:05 +000056 }
57
Mark Sleef5f2be42006-09-05 21:05:31 +000058 /**
59 * Get current time as milliseconds from epoch
60 */
Marc Slemko0e53ccd2006-07-17 23:51:05 +000061 static const long long currentTime() {
Marc Slemkoc7782972006-07-25 02:26:35 +000062#if defined(HAVE_CLOCK_GETTIME)
Marc Slemkoc7782972006-07-25 02:26:35 +000063 struct timespec now;
Marc Slemko6f038a72006-08-03 18:58:09 +000064 assert(clock_gettime(CLOCK_REALTIME, &now) == 0);
Mark Sleef5f2be42006-09-05 21:05:31 +000065 return
66 (now.tv_sec * MS_PER_S) +
67 (now.tv_nsec / NS_PER_MS) +
68 (now.tv_nsec % NS_PER_MS >= 500000 ? 1 : 0) ;
Marc Slemkoc7782972006-07-25 02:26:35 +000069#elif defined(HAVE_GETTIMEOFDAY)
Marc Slemko9f27a4e2006-07-19 20:02:22 +000070 struct timeval now;
Marc Slemko9f27a4e2006-07-19 20:02:22 +000071 assert(gettimeofday(&now, NULL) == 0);
Mark Sleef5f2be42006-09-05 21:05:31 +000072 return
73 (((long long)now.tv_sec) * MS_PER_S) +
74 (now.tv_usec / MS_PER_S) +
75 (now.tv_usec % MS_PER_S >= 500 ? 1 : 0);
Marc Slemkoc7782972006-07-25 02:26:35 +000076#endif // defined(HAVE_GETTIMEDAY)
Marc Slemko0e53ccd2006-07-17 23:51:05 +000077 }
Mark Sleef5f2be42006-09-05 21:05:31 +000078
Marc Slemko0e53ccd2006-07-17 23:51:05 +000079};
80
Marc Slemko0e53ccd2006-07-17 23:51:05 +000081}}} // facebook::thrift::concurrency
82
Mark Sleef5f2be42006-09-05 21:05:31 +000083#endif // #ifndef _THRIFT_CONCURRENCY_UTIL_H_