blob: a2e7dfca476d757383d8ae9ebcae22ff7682850c [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
10#include <assert.h>
Marc Slemko03dedd92006-07-20 00:58:47 +000011#include <stddef.h>
David Reiss53f18f02008-07-11 00:45:29 +000012#include <stdint.h>
Marc Slemkoe6889de2006-08-12 00:32:53 +000013#include <time.h>
Marc Slemkod42a2c22006-08-10 03:30:18 +000014#include <sys/time.h>
Marc Slemko0e53ccd2006-07-17 23:51:05 +000015
T Jake Lucianib5e62212009-01-31 22:36:20 +000016namespace apache { namespace thrift { namespace concurrency {
Marc Slemko0e53ccd2006-07-17 23:51:05 +000017
Mark Sleef5f2be42006-09-05 21:05:31 +000018/**
19 * Utility methods
20 *
21 * This class contains basic utility methods for converting time formats,
22 * and other common platform-dependent concurrency operations.
23 * It should not be included in API headers for other concurrency library
24 * headers, since it will, by definition, pull in all sorts of horrid
David Reiss0c90f6f2008-02-06 22:18:40 +000025 * platform dependent crap. Rather it should be inluded directly in
Mark Sleef5f2be42006-09-05 21:05:31 +000026 * concurrency library implementation source.
27 *
Mark Sleef5f2be42006-09-05 21:05:31 +000028 * @version $Id:$
29 */
Marc Slemko0e53ccd2006-07-17 23:51:05 +000030class Util {
31
Mark Slee9b82d272007-05-23 05:16:07 +000032 static const int64_t NS_PER_S = 1000000000LL;
David Reiss631dcb42008-03-05 07:51:40 +000033 static const int64_t US_PER_S = 1000000LL;
Mark Slee9b82d272007-05-23 05:16:07 +000034 static const int64_t MS_PER_S = 1000LL;
David Reiss631dcb42008-03-05 07:51:40 +000035
36 static const int64_t NS_PER_MS = NS_PER_S / MS_PER_S;
37 static const int64_t US_PER_MS = US_PER_S / MS_PER_S;
Marc Slemkoc7782972006-07-25 02:26:35 +000038
Marc Slemko0e53ccd2006-07-17 23:51:05 +000039 public:
40
Mark Sleef5f2be42006-09-05 21:05:31 +000041 /**
David Reiss631dcb42008-03-05 07:51:40 +000042 * Converts millisecond timestamp into a timespec struct
Mark Sleef5f2be42006-09-05 21:05:31 +000043 *
44 * @param struct timespec& result
45 * @param time or duration in milliseconds
46 */
Mark Slee9b82d272007-05-23 05:16:07 +000047 static void toTimespec(struct timespec& result, int64_t value) {
David Reiss0c90f6f2008-02-06 22:18:40 +000048 result.tv_sec = value / MS_PER_S; // ms to s
Marc Slemkoc7782972006-07-25 02:26:35 +000049 result.tv_nsec = (value % MS_PER_S) * NS_PER_MS; // ms to ns
Marc Slemko0e53ccd2006-07-17 23:51:05 +000050 }
51
David Reiss631dcb42008-03-05 07:51:40 +000052 static void toTimeval(struct timeval& result, int64_t value) {
53 result.tv_sec = value / MS_PER_S; // ms to s
54 result.tv_usec = (value % MS_PER_S) * US_PER_MS; // ms to us
55 }
56
Mark Sleef5f2be42006-09-05 21:05:31 +000057 /**
David Reiss631dcb42008-03-05 07:51:40 +000058 * Converts struct timespec to milliseconds
Mark Sleef5f2be42006-09-05 21:05:31 +000059 */
Mark Slee9b82d272007-05-23 05:16:07 +000060 static const void toMilliseconds(int64_t& result, const struct timespec& value) {
David Reiss631dcb42008-03-05 07:51:40 +000061 result = (value.tv_sec * MS_PER_S) + (value.tv_nsec / NS_PER_MS);
62 // round up -- int64_t cast is to avoid a compiler error for some GCCs
63 if (int64_t(value.tv_nsec) % NS_PER_MS >= (NS_PER_MS / 2)) {
64 ++result;
65 }
66 }
67
68 /**
69 * Converts struct timeval to milliseconds
70 */
71 static const void toMilliseconds(int64_t& result, const struct timeval& value) {
72 result = (value.tv_sec * MS_PER_S) + (value.tv_usec / US_PER_MS);
73 // round up -- int64_t cast is to avoid a compiler error for some GCCs
74 if (int64_t(value.tv_usec) % US_PER_MS >= (US_PER_MS / 2)) {
75 ++result;
76 }
Marc Slemko0e53ccd2006-07-17 23:51:05 +000077 }
78
Mark Sleef5f2be42006-09-05 21:05:31 +000079 /**
80 * Get current time as milliseconds from epoch
81 */
David Reiss53f18f02008-07-11 00:45:29 +000082 static const int64_t currentTime();
Marc Slemko0e53ccd2006-07-17 23:51:05 +000083};
84
T Jake Lucianib5e62212009-01-31 22:36:20 +000085}}} // apache::thrift::concurrency
Marc Slemko0e53ccd2006-07-17 23:51:05 +000086
Mark Sleef5f2be42006-09-05 21:05:31 +000087#endif // #ifndef _THRIFT_CONCURRENCY_UTIL_H_