blob: 29e3567d2ae3e156a221ff57a9c15aa0a1918c3a [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
Mark Sleea8de4892008-02-09 00:02:26 +000010#ifdef HAVE_CONFIG_H
Marc Slemkoc7782972006-07-25 02:26:35 +000011#include <config.h>
Mark Sleea8de4892008-02-09 00:02:26 +000012#endif
Marc Slemkoc7782972006-07-25 02:26:35 +000013
Marc Slemko0e53ccd2006-07-17 23:51:05 +000014#include <assert.h>
Marc Slemko03dedd92006-07-20 00:58:47 +000015#include <stddef.h>
Marc Slemkoe6889de2006-08-12 00:32:53 +000016#if defined(HAVE_CLOCK_GETTIME)
17#include <time.h>
18#else // defined(HAVE_CLOCK_GETTIME)
Marc Slemkod42a2c22006-08-10 03:30:18 +000019#include <sys/time.h>
Marc Slemkoe6889de2006-08-12 00:32:53 +000020#endif // defined(HAVE_CLOCK_GETTIME)
Marc Slemko0e53ccd2006-07-17 23:51:05 +000021
David Reiss0c90f6f2008-02-06 22:18:40 +000022namespace facebook { namespace thrift { namespace concurrency {
Marc Slemko0e53ccd2006-07-17 23:51:05 +000023
Mark Sleef5f2be42006-09-05 21:05:31 +000024/**
25 * Utility methods
26 *
27 * This class contains basic utility methods for converting time formats,
28 * and other common platform-dependent concurrency operations.
29 * It should not be included in API headers for other concurrency library
30 * headers, since it will, by definition, pull in all sorts of horrid
David Reiss0c90f6f2008-02-06 22:18:40 +000031 * platform dependent crap. Rather it should be inluded directly in
Mark Sleef5f2be42006-09-05 21:05:31 +000032 * concurrency library implementation source.
33 *
34 * @author marc
35 * @version $Id:$
36 */
Marc Slemko0e53ccd2006-07-17 23:51:05 +000037class Util {
38
Mark Slee9b82d272007-05-23 05:16:07 +000039 static const int64_t NS_PER_S = 1000000000LL;
40 static const int64_t MS_PER_S = 1000LL;
41 static const int64_t NS_PER_MS = 1000000LL;
Marc Slemkoc7782972006-07-25 02:26:35 +000042
Marc Slemko0e53ccd2006-07-17 23:51:05 +000043 public:
44
Mark Sleef5f2be42006-09-05 21:05:31 +000045 /**
46 * Converts timespec to milliseconds
47 *
48 * @param struct timespec& result
49 * @param time or duration in milliseconds
50 */
Mark Slee9b82d272007-05-23 05:16:07 +000051 static void toTimespec(struct timespec& result, int64_t value) {
David Reiss0c90f6f2008-02-06 22:18:40 +000052 result.tv_sec = value / MS_PER_S; // ms to s
Marc Slemkoc7782972006-07-25 02:26:35 +000053 result.tv_nsec = (value % MS_PER_S) * NS_PER_MS; // ms to ns
Marc Slemko0e53ccd2006-07-17 23:51:05 +000054 }
55
Mark Sleef5f2be42006-09-05 21:05:31 +000056 /**
57 * Converts timespec to milliseconds
58 */
Mark Slee9b82d272007-05-23 05:16:07 +000059 static const void toMilliseconds(int64_t& result, const struct timespec& value) {
Mark Sleef5f2be42006-09-05 21:05:31 +000060 result =
61 (value.tv_sec * MS_PER_S) +
62 (value.tv_nsec / NS_PER_MS) +
63 (value.tv_nsec % NS_PER_MS >= 500000 ? 1 : 0);
Marc Slemko0e53ccd2006-07-17 23:51:05 +000064 }
65
Mark Sleef5f2be42006-09-05 21:05:31 +000066 /**
67 * Get current time as milliseconds from epoch
68 */
Mark Slee9b82d272007-05-23 05:16:07 +000069 static const int64_t currentTime() {
Marc Slemkoc7782972006-07-25 02:26:35 +000070#if defined(HAVE_CLOCK_GETTIME)
Marc Slemkoc7782972006-07-25 02:26:35 +000071 struct timespec now;
Aditya Agarwal9dc57402007-03-31 17:45:12 +000072 int ret = clock_gettime(CLOCK_REALTIME, &now);
Aditya Agarwal3f234da2007-04-01 01:19:57 +000073 assert(ret == 0);
Mark Sleef5f2be42006-09-05 21:05:31 +000074 return
75 (now.tv_sec * MS_PER_S) +
76 (now.tv_nsec / NS_PER_MS) +
77 (now.tv_nsec % NS_PER_MS >= 500000 ? 1 : 0) ;
Marc Slemkoc7782972006-07-25 02:26:35 +000078#elif defined(HAVE_GETTIMEOFDAY)
Marc Slemko9f27a4e2006-07-19 20:02:22 +000079 struct timeval now;
Aditya Agarwal9dc57402007-03-31 17:45:12 +000080 int ret = gettimeofday(&now, NULL);
Aditya Agarwal3f234da2007-04-01 01:19:57 +000081 assert(ret == 0);
Mark Sleef5f2be42006-09-05 21:05:31 +000082 return
Mark Slee9b82d272007-05-23 05:16:07 +000083 (((int64_t)now.tv_sec) * MS_PER_S) +
Mark Sleef5f2be42006-09-05 21:05:31 +000084 (now.tv_usec / MS_PER_S) +
85 (now.tv_usec % MS_PER_S >= 500 ? 1 : 0);
Marc Slemkoc7782972006-07-25 02:26:35 +000086#endif // defined(HAVE_GETTIMEDAY)
Marc Slemko0e53ccd2006-07-17 23:51:05 +000087 }
Mark Sleef5f2be42006-09-05 21:05:31 +000088
Marc Slemko0e53ccd2006-07-17 23:51:05 +000089};
90
Marc Slemko0e53ccd2006-07-17 23:51:05 +000091}}} // facebook::thrift::concurrency
92
Mark Sleef5f2be42006-09-05 21:05:31 +000093#endif // #ifndef _THRIFT_CONCURRENCY_UTIL_H_