Subversion Repositories SmartDukaan

Rev

Rev 30 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
30 ashish 1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one
3
 * or more contributor license agreements. See the NOTICE file
4
 * distributed with this work for additional information
5
 * regarding copyright ownership. The ASF licenses this file
6
 * to you under the Apache License, Version 2.0 (the
7
 * "License"); you may not use this file except in compliance
8
 * with the License. You may obtain a copy of the License at
9
 *
10
 *   http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing,
13
 * software distributed under the License is distributed on an
14
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
 * KIND, either express or implied. See the License for the
16
 * specific language governing permissions and limitations
17
 * under the License.
18
 */
19
 
20
#ifndef _THRIFT_CONCURRENCY_UTIL_H_
21
#define _THRIFT_CONCURRENCY_UTIL_H_ 1
22
 
23
#include <assert.h>
24
#include <stddef.h>
25
#include <stdint.h>
26
#include <time.h>
27
#include <sys/time.h>
28
 
29
namespace apache { namespace thrift { namespace concurrency {
30
 
31
/**
32
 * Utility methods
33
 *
34
 * This class contains basic utility methods for converting time formats,
35
 * and other common platform-dependent concurrency operations.
36
 * It should not be included in API headers for other concurrency library
37
 * headers, since it will, by definition, pull in all sorts of horrid
38
 * platform dependent crap.  Rather it should be inluded directly in
39
 * concurrency library implementation source.
40
 *
41
 * @version $Id:$
42
 */
43
class Util {
44
 
45
  static const int64_t NS_PER_S = 1000000000LL;
46
  static const int64_t US_PER_S = 1000000LL;
47
  static const int64_t MS_PER_S = 1000LL;
48
 
49
  static const int64_t NS_PER_MS = NS_PER_S / MS_PER_S;
50
  static const int64_t US_PER_MS = US_PER_S / MS_PER_S;
51
 
52
 public:
53
 
54
  /**
55
   * Converts millisecond timestamp into a timespec struct
56
   *
57
   * @param struct timespec& result
58
   * @param time or duration in milliseconds
59
   */
60
  static void toTimespec(struct timespec& result, int64_t value) {
61
    result.tv_sec = value / MS_PER_S; // ms to s
62
    result.tv_nsec = (value % MS_PER_S) * NS_PER_MS; // ms to ns
63
  }
64
 
65
  static void toTimeval(struct timeval& result, int64_t value) {
66
    result.tv_sec = value / MS_PER_S; // ms to s
67
    result.tv_usec = (value % MS_PER_S) * US_PER_MS; // ms to us
68
  }
69
 
70
  /**
71
   * Converts struct timespec to milliseconds
72
   */
73
  static const void toMilliseconds(int64_t& result, const struct timespec& value) {
74
    result = (value.tv_sec * MS_PER_S) + (value.tv_nsec / NS_PER_MS);
75
    // round up -- int64_t cast is to avoid a compiler error for some GCCs
76
    if (int64_t(value.tv_nsec) % NS_PER_MS >= (NS_PER_MS / 2)) {
77
      ++result;
78
    }
79
  }
80
 
81
  /**
82
   * Converts struct timeval to milliseconds
83
   */
84
  static const void toMilliseconds(int64_t& result, const struct timeval& value) {
85
    result = (value.tv_sec * MS_PER_S) + (value.tv_usec / US_PER_MS);
86
    // round up -- int64_t cast is to avoid a compiler error for some GCCs
87
    if (int64_t(value.tv_usec) % US_PER_MS >= (US_PER_MS / 2)) {
88
      ++result;
89
    }
90
  }
91
 
92
  /**
93
   * Get current time as milliseconds from epoch
94
   */
95
  static const int64_t currentTime();
96
};
97
 
98
}}} // apache::thrift::concurrency
99
 
100
#endif // #ifndef _THRIFT_CONCURRENCY_UTIL_H_