All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
modules/common/src/include/citrusleaf/cf_clock.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2008-2013 by Aerospike.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to
6  * deal in the Software without restriction, including without limitation the
7  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8  * sell copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20  * IN THE SOFTWARE.
21  *****************************************************************************/
22 #pragma once
23 
24 #include <citrusleaf/cf_atomic.h>
25 #include <citrusleaf/cf_types.h>
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 #ifdef __linux__
32 #include <time.h>
33 #include <bits/time.h>
34 #endif
35 
36 #ifdef __APPLE__
37 #include <sys/time.h>
38 #endif
39 
40 #ifdef CF_WINDOWS
41 #include <citrusleaf/cf_clock_win.h>
42 #endif
43 
44 /******************************************************************************
45  * TYPES
46  ******************************************************************************/
47 
48 typedef uint64_t cf_clock;
49 typedef cf_atomic64 cf_atomic_clock;
50 
51 /******************************************************************************
52  * FUNCTIONS
53  ******************************************************************************/
54 
55 cf_clock cf_getms();
56 cf_clock cf_getmicros();
57 cf_clock cf_getus();
58 cf_clock cf_clock_getabsolute();
59 cf_clock cf_get_seconds();
60 cf_clock cf_secs_since_clepoch();
61 void cf_set_wait_timespec(int ms_wait, struct timespec* tp);
62 
63 /******************************************************************************
64  * INLINE FUNCTIONS
65  ******************************************************************************/
66 
67 static inline cf_clock CF_TIMESPEC_TO_MS_P( struct timespec *ts ) {
68  uint64_t r1 = ts->tv_nsec;
69  r1 /= 1000000;
70  uint64_t r2 = ts->tv_sec;
71  r2 *= 1000;
72  return( r1 + r2 );
73 }
74 
75 static inline cf_clock CF_TIMESPEC_TO_MS( struct timespec ts ) {
76  uint64_t r1 = ts.tv_nsec;
77  r1 /= 1000000;
78  uint64_t r2 = ts.tv_sec;
79  r2 *= 1000;
80  return ( r1 + r2 );
81 }
82 
83 static inline cf_clock CF_TIMESPEC_TO_US( struct timespec ts ) {
84  uint64_t r1 = ts.tv_nsec;
85  r1 /= 1000;
86  uint64_t r2 = ts.tv_sec;
87  r2 *= 1000000;
88  return ( r1 + r2 );
89 }
90 
91 static inline void CF_TIMESPEC_ADD_MS(struct timespec *ts, uint ms) {
92  ts->tv_sec += ms / 1000;
93  ts->tv_nsec += (ms % 1000) * 1000000;
94  if (ts->tv_nsec > 1000000000) {
95  ts->tv_sec ++;
96  ts->tv_nsec -= 1000000000;
97  }
98 }
99 
100 static inline uint32_t cf_clepoch_seconds() {
101 #ifdef __APPLE__
102  struct timeval tv;
103  gettimeofday(&tv, NULL);
104  return (uint32_t)(tv.tv_sec - CITRUSLEAF_EPOCH);
105 #else
106  struct timespec ts;
108  return (uint32_t)(ts.tv_sec - CITRUSLEAF_EPOCH);
109 #endif
110 }
111 
112 // Special client-only conversion utility.
113 static inline uint32_t cf_server_void_time_to_ttl(uint32_t server_void_time) {
114  // This is the server's flag indicating the record never expires...
115  if (server_void_time == 0) {
116  // ... converted to the new client-side convention for "never expires":
117  return (uint32_t)-1;
118  }
119 
120  uint32_t now = cf_clepoch_seconds();
121 
122  // Record may not have expired on server, but delay or clock differences may
123  // cause it to look expired on client. (We give the record to the app anyway
124  // to avoid internal cleanup complications.) Floor at 1, not 0, to avoid old
125  // "never expires" interpretation.
126  return server_void_time > now ? server_void_time - now : 1;
127 }
128 
129 /******************************************************************************/
130 
131 #ifdef __cplusplus
132 } // end extern "C"
133 #endif