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.
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
)
30 memset(timer
, 0, sizeof (*timer
));
31 gettimeofday(timer
, NULL
);
33 timer
->tv_sec
+= seconds
;
37 int iscsi_timer_expired(struct timeval
*timer
)
41 /* no timer, can't have expired */
42 if ((timer
== NULL
) || ((timer
->tv_sec
== 0) && (timer
->tv_usec
== 0)))
45 memset(&now
, 0, sizeof (now
));
46 gettimeofday(&now
, NULL
);
48 if (now
.tv_sec
> timer
->tv_sec
)
50 if ((now
.tv_sec
== timer
->tv_sec
) && (now
.tv_usec
>= timer
->tv_usec
))
55 int iscsi_timer_msecs_until(struct timeval
*timer
)
61 /* no timer, can't have expired, infinite time til it expires */
62 if ((timer
== NULL
) || ((timer
->tv_sec
== 0) && (timer
->tv_usec
== 0)))
65 memset(&now
, 0, sizeof (now
));
66 gettimeofday(&now
, NULL
);
68 /* already expired? */
69 if (now
.tv_sec
> timer
->tv_sec
)
71 if ((now
.tv_sec
== timer
->tv_sec
) && (now
.tv_usec
>= timer
->tv_usec
))
74 /* not expired yet, do the math */
75 partial
= timer
->tv_usec
- now
.tv_usec
;
77 partial
+= 1000 * 1000;
78 msecs
= (partial
+ 500) / 1000;
79 msecs
+= (timer
->tv_sec
- now
.tv_sec
- 1) * 1000;
81 msecs
= (partial
+ 500) / 1000;
82 msecs
+= (timer
->tv_sec
- now
.tv_sec
) * 1000;