|
Home > Archive > Unix Programming > May 2006 > Question from Robbins' USP code
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Question from Robbins' USP code
|
|
|
| Hi, I don't see why the first and third if's are needed in the code
below.
Thanks for any clue!
#define MILLON 1000000L
static int
gettimeout(struct timeval end, struct timeval *timeout)
{
gettimeofday(timeout, NULL);
timeout->tv_sec = end.tv_sec - timeout->tv_sec;
timeout->tv_usec = end.tv_usec - timeout->tv_usec;
if (timeout->tv_usec >= MILLON) {
timeout->tv_sec++;
timeout->tv_usec -= MILLON;
}
if (timeout->tv_usec < 0) {
timeout->tv_sec--;
timeout->tv_usec += MILLON;
}
if ((timeout->tv_sec < 0) ||
((timeout->tv_sec == 0) && (timeout->tv_usec == 0))) {
errno = ETIME;
return -1;
}
return 0;
}
| |
| Barry Margolin 2006-05-20, 7:14 pm |
| In article <1148161864.552698.39290@j55g2000cwa.googlegroups.com>,
"kroty" <kroty@fibertel.com.ar> wrote:
> Hi, I don't see why the first and third if's are needed in the code
> below.
I'm not sure the first one is really needed. As long as end.tv_usec is
less than a million, as it should be, timeout->tv_usec can never be
greater than a million.
The third if handles the case where the end time is not actually in the
future.
>
> Thanks for any clue!
>
> #define MILLON 1000000L
> static int
> gettimeout(struct timeval end, struct timeval *timeout)
> {
> gettimeofday(timeout, NULL);
> timeout->tv_sec = end.tv_sec - timeout->tv_sec;
> timeout->tv_usec = end.tv_usec - timeout->tv_usec;
>
> if (timeout->tv_usec >= MILLON) {
> timeout->tv_sec++;
> timeout->tv_usec -= MILLON;
> }
> if (timeout->tv_usec < 0) {
> timeout->tv_sec--;
> timeout->tv_usec += MILLON;
> }
> if ((timeout->tv_sec < 0) ||
> ((timeout->tv_sec == 0) && (timeout->tv_usec == 0))) {
> errno = ETIME;
> return -1;
> }
>
> return 0;
> }
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
| |
| Bjorn Reese 2006-05-21, 7:15 am |
| Barry Margolin wrote:
> I'm not sure the first one is really needed. As long as end.tv_usec is
> less than a million, as it should be, timeout->tv_usec can never be
> greater than a million.
Furthermore, if end.tv_usec could ever be greater than a million, then
theoretically it could be 3 million as well. If that was the case, then
one could argue that the if statement ought to be changed to a while
statement.
[vbcol=seagreen]
--
mail1dotstofanetdotdk
| |
| Ian Collins 2006-05-21, 7:15 am |
| Bjorn Reese wrote:
> Barry Margolin wrote:
>
>
>
> Furthermore, if end.tv_usec could ever be greater than a million, then
> theoretically it could be 3 million as well. If that was the case, then
> one could argue that the if statement ought to be changed to a while
> statement.
>
If timeout->tv_usec >= MILLON, something horrible has happened, so an
assert might be more appropriate.
>
>
--
Ian Collins.
|
|
|
|
|