diff options
author | rpj <rpj> | 2003-12-10 01:19:04 +0000 |
---|---|---|
committer | rpj <rpj> | 2003-12-10 01:19:04 +0000 |
commit | 28c3d0a235d121db531469449b388af5eaf14013 (patch) | |
tree | e98f04101ff3824c011842a23d571bfa7fd57379 | |
parent | 41f042479e78dc0ce4daa8fd85ef59f61bb56325 (diff) |
Fix timeout calculations.
-rw-r--r-- | sem_timedwait.c | 134 |
1 files changed, 59 insertions, 75 deletions
diff --git a/sem_timedwait.c b/sem_timedwait.c index d78e10f..a600ff4 100644 --- a/sem_timedwait.c +++ b/sem_timedwait.c @@ -108,6 +108,8 @@ sem_timedwait (sem_t * sem, const struct timespec * abstime) const DWORD NANOSEC_PER_MILLISEC = 1000000; const DWORD MILLISEC_PER_SEC = 1000; DWORD milliseconds; + DWORD tmpAbsMilliseconds; + DWORD tmpCurrMilliseconds; if (sem == NULL) { @@ -116,88 +118,70 @@ sem_timedwait (sem_t * sem, const struct timespec * abstime) else { if (abstime == NULL) - { - milliseconds = INFINITE; - } + { + milliseconds = INFINITE; + } else - { - milliseconds = 0; - - /* - * Calculate timeout as milliseconds from current system time. - */ - - /* get current system time */ - + { + /* + * Calculate timeout as milliseconds from current system time. + */ + + /* + * subtract current system time from abstime in a way that checks + * that abstime is never in the past, or is never equivalent to the + * defined INFINITE value (0xFFFFFFFF). + * + * Assume all integers are unsigned, i.e. cannot test if less than 0. + */ + tmpAbsMilliseconds = abstime->tv_sec * MILLISEC_PER_SEC; + tmpAbsMilliseconds += (abstime->tv_nsec + (NANOSEC_PER_MILLISEC/2)) / NANOSEC_PER_MILLISEC; + + /* get current system time */ + #ifdef NEED_FTIME - { - FILETIME ft; - SYSTEMTIME st; - - GetSystemTime(&st); - SystemTimeToFileTime(&st, &ft); - /* - * GetSystemTimeAsFileTime(&ft); would be faster, - * but it does not exist on WinCE - */ - - ptw32_filetime_to_timespec(&ft, &currSysTime); - } - - /* - * subtract current system time from abstime in a way that checks - * that abstime is never in the past, or is never equivalent to the - * defined INFINITE value (0xFFFFFFFF). - */ - if (abstime->tv_sec >= currSysTime.tv_sec) - { - DWORD tmpMilliseconds; - DWORD tmpCurrMilliseconds; - - tmpMilliseconds = (abstime->tv_sec - currSysTime.tv_sec) * MILLISEC_PER_SEC; - tmpMilliseconds += ((abstime->tv_nsec + (NANOSEC_PER_MILLISEC/2)) - / NANOSEC_PER_MILLISEC); - tmpCurrMilliseconds = ((currSysTime.tv_nsec + (NANOSEC_PER_MILLISEC/2)) - / NANOSEC_PER_MILLISEC); - if (tmpMilliseconds > tmpCurrMilliseconds) - { - milliseconds = tmpMilliseconds - tmpCurrMilliseconds; - if (milliseconds == INFINITE) - { - milliseconds--; - } - } - } + { + FILETIME ft; + SYSTEMTIME st; -#else /* NEED_FTIME */ - _ftime(&currSysTime); - - /* - * subtract current system time from abstime in a way that checks - * that abstime is never in the past, or is never equivalent to the - * defined INFINITE value (0xFFFFFFFF). - */ - if (abstime->tv_sec >= currSysTime.time) - { - DWORD tmpMilliseconds; - - tmpMilliseconds = (abstime->tv_sec - currSysTime.time) * MILLISEC_PER_SEC; - tmpMilliseconds += ((abstime->tv_nsec + (NANOSEC_PER_MILLISEC/2)) - / NANOSEC_PER_MILLISEC); - if (tmpMilliseconds > (DWORD) currSysTime.millitm) - { - milliseconds = tmpMilliseconds - currSysTime.millitm; - if (milliseconds == INFINITE) - { - milliseconds--; - } - } - } + GetSystemTime(&st); + SystemTimeToFileTime(&st, &ft); + /* + * GetSystemTimeAsFileTime(&ft); would be faster, + * but it does not exist on WinCE + */ + + ptw32_filetime_to_timespec(&ft, &currSysTime); + } + + tmpCurrMilliseconds = currSysTime.tv_sec * MILLISEC_PER_SEC; + tmpCurrMilliseconds += (currSysTime.tv_nsec + (NANOSEC_PER_MILLISEC/2)) / NANOSEC_PER_MILLISEC; + +#else /* ! NEED_FTIME */ + + _ftime(&currSysTime); + + tmpCurrMilliseconds = (DWORD) currSysTime.time * MILLISEC_PER_SEC; + tmpCurrMilliseconds += (DWORD) currSysTime.millitm; #endif /* NEED_FTIME */ - } + if (tmpAbsMilliseconds > tmpCurrMilliseconds) + { + milliseconds = tmpAbsMilliseconds - tmpCurrMilliseconds; + if (milliseconds == INFINITE) + { + /* Timeouts must be finite */ + milliseconds--; + } + } + else + { + /* The abstime given is in the past */ + milliseconds = 0; + } + } #ifdef NEED_SEM |