usr/config.h: fix comment for struct iscsi_session_timeout_config
[open-iscsi.git] / usr / iscsi_timer.c
blobde38286c72054bb2568efb28ccb0b86dbd80779e
1 /*
2 * iSCSI timer
4 * Copyright (C) 2002 Cisco Systems, Inc.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published
8 * by the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * See the file COPYING included with this distribution for more details.
18 #include <string.h>
19 #include <sys/time.h>
21 void iscsi_timer_clear(struct timeval *timer)
23 memset(timer, 0, sizeof (*timer));
26 /* set timer to now + seconds */
27 void iscsi_timer_set(struct timeval *timer, int seconds)
29 if (timer) {
30 memset(timer, 0, sizeof (*timer));
31 gettimeofday(timer, NULL);
33 timer->tv_sec += seconds;
37 int iscsi_timer_expired(struct timeval *timer)
39 struct timeval now;
41 /* no timer, can't have expired */
42 if ((timer == NULL) || ((timer->tv_sec == 0) && (timer->tv_usec == 0)))
43 return 0;
45 memset(&now, 0, sizeof (now));
46 gettimeofday(&now, NULL);
48 if (now.tv_sec > timer->tv_sec)
49 return 1;
50 if ((now.tv_sec == timer->tv_sec) && (now.tv_usec >= timer->tv_usec))
51 return 1;
52 return 0;
55 int iscsi_timer_msecs_until(struct timeval *timer)
57 struct timeval now;
58 int msecs;
59 long partial;
61 /* no timer, can't have expired, infinite time til it expires */
62 if ((timer == NULL) || ((timer->tv_sec == 0) && (timer->tv_usec == 0)))
63 return -1;
65 memset(&now, 0, sizeof (now));
66 gettimeofday(&now, NULL);
68 /* already expired? */
69 if (now.tv_sec > timer->tv_sec)
70 return 0;
71 if ((now.tv_sec == timer->tv_sec) && (now.tv_usec >= timer->tv_usec))
72 return 0;
74 /* not expired yet, do the math */
75 partial = timer->tv_usec - now.tv_usec;
76 if (partial < 0) {
77 partial += 1000 * 1000;
78 msecs = (partial + 500) / 1000;
79 msecs += (timer->tv_sec - now.tv_sec - 1) * 1000;
80 } else {
81 msecs = (partial + 500) / 1000;
82 msecs += (timer->tv_sec - now.tv_sec) * 1000;
85 return msecs;