Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 2 | include/common/time.h |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 3 | Time calculation functions and macros. |
| 4 | |
| 5 | Copyright (C) 2000-2006 Willy Tarreau - w@1wt.eu |
| 6 | |
| 7 | This library is free software; you can redistribute it and/or |
| 8 | modify it under the terms of the GNU Lesser General Public |
| 9 | License as published by the Free Software Foundation, version 2.1 |
| 10 | exclusively. |
| 11 | |
| 12 | This library is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | Lesser General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU Lesser General Public |
| 18 | License along with this library; if not, write to the Free Software |
| 19 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 22 | #ifndef _COMMON_TIME_H |
| 23 | #define _COMMON_TIME_H |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 24 | |
| 25 | #include <stdlib.h> |
| 26 | #include <sys/time.h> |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 27 | #include <common/config.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 28 | |
| 29 | #define TIME_ETERNITY -1 |
| 30 | |
| 31 | /* returns the lowest delay amongst <old> and <new>, and respects TIME_ETERNITY */ |
| 32 | #define MINTIME(old, new) (((new)<0)?(old):(((old)<0||(new)<(old))?(new):(old))) |
| 33 | #define SETNOW(a) (*a=now) |
| 34 | |
| 35 | extern struct timeval now; /* the current date at any moment */ |
| 36 | extern struct timeval start_date; /* the process's start date */ |
| 37 | |
| 38 | |
| 39 | /* |
| 40 | * adds <ms> ms to <from>, set the result to <tv> and returns a pointer <tv> |
| 41 | */ |
| 42 | struct timeval *tv_delayfrom(struct timeval *tv, struct timeval *from, int ms); |
| 43 | |
| 44 | /* |
| 45 | * compares <tv1> and <tv2> modulo 1ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2 |
| 46 | * Must not be used when either argument is eternity. Use tv_cmp2_ms() for that. |
| 47 | */ |
| 48 | int tv_cmp_ms(struct timeval *tv1, struct timeval *tv2); |
| 49 | |
| 50 | /* |
| 51 | * compares <tv1> and <tv2> : returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2, |
| 52 | * considering that 0 is the eternity. |
| 53 | */ |
| 54 | int tv_cmp2(struct timeval *tv1, struct timeval *tv2); |
| 55 | /* |
| 56 | * compares <tv1> and <tv2> modulo 1 ms: returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2, |
| 57 | * considering that 0 is the eternity. |
| 58 | */ |
| 59 | int tv_cmp2_ms(struct timeval *tv1, struct timeval *tv2); |
| 60 | |
| 61 | /* |
| 62 | * returns the remaining time between tv1=now and event=tv2 |
| 63 | * if tv2 is passed, 0 is returned. |
| 64 | * Returns TIME_ETERNITY if tv2 is eternity. |
| 65 | */ |
| 66 | unsigned long tv_remain2(struct timeval *tv1, struct timeval *tv2); |
| 67 | |
| 68 | |
| 69 | /* sets <tv> to the current time */ |
| 70 | static inline struct timeval *tv_now(struct timeval *tv) |
| 71 | { |
| 72 | if (tv) |
| 73 | gettimeofday(tv, NULL); |
| 74 | return tv; |
| 75 | } |
| 76 | |
| 77 | /* |
| 78 | * compares <tv1> and <tv2> : returns 0 if equal, -1 if tv1 < tv2, 1 if tv1 > tv2 |
| 79 | * Must not be used when either argument is eternity. Use tv_cmp2() for that. |
| 80 | */ |
| 81 | static inline int tv_cmp(struct timeval *tv1, struct timeval *tv2) |
| 82 | { |
| 83 | if (tv1->tv_sec < tv2->tv_sec) |
| 84 | return -1; |
| 85 | else if (tv1->tv_sec > tv2->tv_sec) |
| 86 | return 1; |
| 87 | else if (tv1->tv_usec < tv2->tv_usec) |
| 88 | return -1; |
| 89 | else if (tv1->tv_usec > tv2->tv_usec) |
| 90 | return 1; |
| 91 | else |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | /* |
| 96 | * returns the difference, in ms, between tv1 and tv2 |
| 97 | * Must not be used when either argument is eternity. |
| 98 | */ |
| 99 | static inline unsigned long tv_diff(struct timeval *tv1, struct timeval *tv2) |
| 100 | { |
| 101 | unsigned long ret; |
| 102 | |
| 103 | ret = (tv2->tv_sec - tv1->tv_sec) * 1000; |
| 104 | if (tv2->tv_usec > tv1->tv_usec) |
| 105 | ret += (tv2->tv_usec - tv1->tv_usec) / 1000; |
| 106 | else |
| 107 | ret -= (tv1->tv_usec - tv2->tv_usec) / 1000; |
| 108 | return (unsigned long) ret; |
| 109 | } |
| 110 | |
| 111 | /* |
| 112 | * returns the remaining time between tv1=now and event=tv2 |
| 113 | * if tv2 is passed, 0 is returned. |
| 114 | * Must not be used when either argument is eternity. |
| 115 | */ |
| 116 | static inline unsigned long tv_remain(struct timeval *tv1, struct timeval *tv2) |
| 117 | { |
| 118 | unsigned long ret; |
| 119 | |
| 120 | if (tv_cmp_ms(tv1, tv2) >= 0) |
| 121 | return 0; /* event elapsed */ |
| 122 | |
| 123 | ret = (tv2->tv_sec - tv1->tv_sec) * 1000; |
| 124 | if (tv2->tv_usec > tv1->tv_usec) |
| 125 | ret += (tv2->tv_usec - tv1->tv_usec) / 1000; |
| 126 | else |
| 127 | ret -= (tv1->tv_usec - tv2->tv_usec) / 1000; |
| 128 | return (unsigned long) ret; |
| 129 | } |
| 130 | |
| 131 | |
| 132 | /* |
| 133 | * zeroes a struct timeval |
| 134 | */ |
| 135 | |
| 136 | static inline struct timeval *tv_eternity(struct timeval *tv) |
| 137 | { |
| 138 | tv->tv_sec = tv->tv_usec = 0; |
| 139 | return tv; |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | * returns 1 if tv is null, else 0 |
| 144 | */ |
| 145 | static inline int tv_iseternity(struct timeval *tv) |
| 146 | { |
| 147 | if (tv->tv_sec == 0 && tv->tv_usec == 0) |
| 148 | return 1; |
| 149 | else |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * returns the first event between tv1 and tv2 into tvmin. |
| 155 | * a zero tv is ignored. tvmin is returned. |
| 156 | */ |
| 157 | static inline struct timeval *tv_min(struct timeval *tvmin, |
| 158 | struct timeval *tv1, struct timeval *tv2) |
| 159 | { |
| 160 | |
| 161 | if (tv_cmp2(tv1, tv2) <= 0) |
| 162 | *tvmin = *tv1; |
| 163 | else |
| 164 | *tvmin = *tv2; |
| 165 | |
| 166 | return tvmin; |
| 167 | } |
| 168 | |
| 169 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 170 | #endif /* _COMMON_TIME_H */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 171 | |
| 172 | /* |
| 173 | * Local variables: |
| 174 | * c-indent-level: 8 |
| 175 | * c-basic-offset: 8 |
| 176 | * End: |
| 177 | */ |