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 | */ |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 42 | REGPRM3 struct timeval *tv_delayfrom(struct timeval *tv, const struct timeval *from, int ms); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 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 | */ |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 48 | REGPRM2 int tv_cmp_ms(const struct timeval *tv1, const struct timeval *tv2); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 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 | */ |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 54 | REGPRM2 int tv_cmp2(const struct timeval *tv1, const struct timeval *tv2); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 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 | */ |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 59 | REGPRM2 int tv_cmp2_ms(const struct timeval *tv1, const struct timeval *tv2); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 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 | */ |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 66 | REGPRM2 unsigned long tv_remain2(const struct timeval *tv1, const struct timeval *tv2); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 67 | |
| 68 | |
| 69 | /* sets <tv> to the current time */ |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 70 | REGPRM1 static inline struct timeval *tv_now(struct timeval *tv) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 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 | */ |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 81 | REGPRM2 static inline int tv_cmp(const struct timeval *tv1, const struct timeval *tv2) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 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 | */ |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 99 | REGPRM2 static inline unsigned long tv_diff(const struct timeval *tv1, const struct timeval *tv2) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 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 | */ |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 116 | REGPRM2 static inline unsigned long tv_remain(const struct timeval *tv1, const struct timeval *tv2) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 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 | |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 136 | REGPRM1 static inline struct timeval *tv_eternity(struct timeval *tv) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 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 | */ |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 145 | REGPRM1 static inline int tv_iseternity(const struct timeval *tv) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 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 | */ |
Willy Tarreau | fb27867 | 2006-10-15 15:38:50 +0200 | [diff] [blame] | 157 | REGPRM3 static inline const struct timeval *tv_min(struct timeval *tvmin, |
Willy Tarreau | b17916e | 2006-10-15 15:17:57 +0200 | [diff] [blame] | 158 | const struct timeval *tv1, |
| 159 | const struct timeval *tv2) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 160 | { |
| 161 | |
| 162 | if (tv_cmp2(tv1, tv2) <= 0) |
| 163 | *tvmin = *tv1; |
| 164 | else |
| 165 | *tvmin = *tv2; |
| 166 | |
| 167 | return tvmin; |
| 168 | } |
| 169 | |
| 170 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 171 | #endif /* _COMMON_TIME_H */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 172 | |
| 173 | /* |
| 174 | * Local variables: |
| 175 | * c-indent-level: 8 |
| 176 | * c-basic-offset: 8 |
| 177 | * End: |
| 178 | */ |