summaryrefslogtreecommitdiff
path: root/sem_timedwait.c
diff options
context:
space:
mode:
Diffstat (limited to 'sem_timedwait.c')
-rw-r--r--sem_timedwait.c134
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