blob: 06b4d183f22948894aa2ceffd9a1b60954d4cbb3 [file] [log] [blame]
Mark Slee9f0c6512007-02-28 23:58:26 +00001// Copyright (c) 2006- Facebook
2// Distributed under the Thrift Software License
3//
4// See accompanying file LICENSE or visit the Thrift site at:
5// http://developers.facebook.com/thrift/
6
Mark Sleef5f2be42006-09-05 21:05:31 +00007#ifndef _THRIFT_CONCURRENCY_UTIL_H_
8#define _THRIFT_CONCURRENCY_UTIL_H_ 1
Marc Slemko0e53ccd2006-07-17 23:51:05 +00009
Marc Slemkoc7782972006-07-25 02:26:35 +000010#include <config.h>
11
Marc Slemko0e53ccd2006-07-17 23:51:05 +000012#include <assert.h>
Marc Slemko03dedd92006-07-20 00:58:47 +000013#include <stddef.h>
Marc Slemkoe6889de2006-08-12 00:32:53 +000014#if defined(HAVE_CLOCK_GETTIME)
15#include <time.h>
16#else // defined(HAVE_CLOCK_GETTIME)
Marc Slemkod42a2c22006-08-10 03:30:18 +000017#include <sys/time.h>
Marc Slemkoe6889de2006-08-12 00:32:53 +000018#endif // defined(HAVE_CLOCK_GETTIME)
Marc Slemko0e53ccd2006-07-17 23:51:05 +000019
20namespace facebook { namespace thrift { namespace concurrency {
21
Mark Sleef5f2be42006-09-05 21:05:31 +000022/**
23 * Utility methods
24 *
25 * This class contains basic utility methods for converting time formats,
26 * and other common platform-dependent concurrency operations.
27 * It should not be included in API headers for other concurrency library
28 * headers, since it will, by definition, pull in all sorts of horrid
29 * platform dependent crap. Rather it should be inluded directly in
30 * concurrency library implementation source.
31 *
32 * @author marc
33 * @version $Id:$
34 */
Marc Slemko0e53ccd2006-07-17 23:51:05 +000035class Util {
36
Marc Slemkoc7782972006-07-25 02:26:35 +000037 static const long long NS_PER_S = 1000000000LL;
Marc Slemkoc7782972006-07-25 02:26:35 +000038 static const long long MS_PER_S = 1000LL;
Marc Slemkoc7782972006-07-25 02:26:35 +000039 static const long long NS_PER_MS = 1000000LL;
40
Marc Slemko0e53ccd2006-07-17 23:51:05 +000041 public:
42
Mark Sleef5f2be42006-09-05 21:05:31 +000043 /**
44 * Converts timespec to milliseconds
45 *
46 * @param struct timespec& result
47 * @param time or duration in milliseconds
48 */
Marc Slemko9f27a4e2006-07-19 20:02:22 +000049 static void toTimespec(struct timespec& result, long long value) {
Mark Sleef5f2be42006-09-05 21:05:31 +000050 result.tv_sec = value / MS_PER_S; // ms to s
Marc Slemkoc7782972006-07-25 02:26:35 +000051 result.tv_nsec = (value % MS_PER_S) * NS_PER_MS; // ms to ns
Marc Slemko0e53ccd2006-07-17 23:51:05 +000052 }
53
Mark Sleef5f2be42006-09-05 21:05:31 +000054 /**
55 * Converts timespec to milliseconds
56 */
Marc Slemko0e53ccd2006-07-17 23:51:05 +000057 static const void toMilliseconds(long long& result, const struct timespec& value) {
Mark Sleef5f2be42006-09-05 21:05:31 +000058 result =
59 (value.tv_sec * MS_PER_S) +
60 (value.tv_nsec / NS_PER_MS) +
61 (value.tv_nsec % NS_PER_MS >= 500000 ? 1 : 0);
Marc Slemko0e53ccd2006-07-17 23:51:05 +000062 }
63
Mark Sleef5f2be42006-09-05 21:05:31 +000064 /**
65 * Get current time as milliseconds from epoch
66 */
Marc Slemko0e53ccd2006-07-17 23:51:05 +000067 static const long long currentTime() {
Marc Slemkoc7782972006-07-25 02:26:35 +000068#if defined(HAVE_CLOCK_GETTIME)
Marc Slemkoc7782972006-07-25 02:26:35 +000069 struct timespec now;
Marc Slemko6f038a72006-08-03 18:58:09 +000070 assert(clock_gettime(CLOCK_REALTIME, &now) == 0);
Mark Sleef5f2be42006-09-05 21:05:31 +000071 return
72 (now.tv_sec * MS_PER_S) +
73 (now.tv_nsec / NS_PER_MS) +
74 (now.tv_nsec % NS_PER_MS >= 500000 ? 1 : 0) ;
Marc Slemkoc7782972006-07-25 02:26:35 +000075#elif defined(HAVE_GETTIMEOFDAY)
Marc Slemko9f27a4e2006-07-19 20:02:22 +000076 struct timeval now;
Marc Slemko9f27a4e2006-07-19 20:02:22 +000077 assert(gettimeofday(&now, NULL) == 0);
Mark Sleef5f2be42006-09-05 21:05:31 +000078 return
79 (((long long)now.tv_sec) * MS_PER_S) +
80 (now.tv_usec / MS_PER_S) +
81 (now.tv_usec % MS_PER_S >= 500 ? 1 : 0);
Marc Slemkoc7782972006-07-25 02:26:35 +000082#endif // defined(HAVE_GETTIMEDAY)
Marc Slemko0e53ccd2006-07-17 23:51:05 +000083 }
Mark Sleef5f2be42006-09-05 21:05:31 +000084
Marc Slemko0e53ccd2006-07-17 23:51:05 +000085};
86
Marc Slemko0e53ccd2006-07-17 23:51:05 +000087}}} // facebook::thrift::concurrency
88
Mark Sleef5f2be42006-09-05 21:05:31 +000089#endif // #ifndef _THRIFT_CONCURRENCY_UTIL_H_