From 20f77eda55f874b939719ac0abda4405ecd20bf8 Mon Sep 17 00:00:00 2001
From: rpj <rpj>
Date: Mon, 18 Jan 1999 23:50:07 +0000
Subject: Tue Jan 19 18:27:42 1999  Ross Johnson  <rpj@swan.canberra.edu.au>   
      * pthread.h (pthreadCancelableTimedWait): New prototype.        
 (pthreadCancelableWait): Remove second argument.         * misc.c
 (CancelableWait): New static function is         pthreadCancelableWait()
 renamed.         (pthreadCancelableWait): Now just calls CancelableWait()
 with         INFINITE timeout.         (pthreadCancelableTimedWait): Just
 calls CancelableWait()         with passed in timeout.         * private.c
 (_pthread_sem_timedwait): 'abstime' arg really is         absolute time.
 Calculate relative time to wait from current         time before passing
 timeout to new routine         pthreadCancelableTimedWait().         - Scott
 Lightner <scott@curriculum.com>

---
 ChangeLog | 18 ++++++++++++++++++
 misc.c    | 25 +++++++++++++++++++------
 private.c | 25 ++++++++++++++++++-------
 pthread.h |  8 +++++---
 4 files changed, 60 insertions(+), 16 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index d4da384..fde297e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,21 @@
+Tue Jan 19 18:27:42 1999  Ross Johnson  <rpj@swan.canberra.edu.au>
+
+	* pthread.h (pthreadCancelableTimedWait): New prototype.
+	(pthreadCancelableWait): Remove second argument.
+
+	* misc.c (CancelableWait): New static function is 
+	pthreadCancelableWait() renamed.
+	(pthreadCancelableWait): Now just calls CancelableWait() with
+	INFINITE timeout.
+	(pthreadCancelableTimedWait): Just calls CancelableWait()
+	with passed in timeout.
+
+	* private.c (_pthread_sem_timedwait): 'abstime' arg really is
+	absolute time. Calculate relative time to wait from current
+	time before passing timeout to new routine 
+	pthreadCancelableTimedWait().
+	- Scott Lightner <scott@curriculum.com>
+
 Sun Jan 17 12:01:26 1999  Ross Johnson  <rpj@ixobrychus.canberra.edu.au>
 
 	* private.c (_pthread_sem_timedwait): Move from semaphore.c.
diff --git a/misc.c b/misc.c
index d6c4b2f..49a71dd 100644
--- a/misc.c
+++ b/misc.c
@@ -129,8 +129,8 @@ pthread_equal (pthread_t t1, pthread_t t2)
 }				/* pthread_equal */
 
 
-int
-pthreadCancelableWait (HANDLE waitHandle, DWORD abstime)
+static int
+CancelableWait (HANDLE waitHandle, DWORD timeout)
      /*
       * -------------------------------------------------------------------
       * This provides an extra hook into the pthread_cancel
@@ -151,7 +151,6 @@ pthreadCancelableWait (HANDLE waitHandle, DWORD abstime)
   DWORD nHandles = 1;
   DWORD status;
 
-
   handles[0] = waitHandle;
 
   if ((self = pthread_getspecific (_pthread_selfThreadKey)) != NULL)
@@ -177,7 +176,7 @@ pthreadCancelableWait (HANDLE waitHandle, DWORD abstime)
                                     nHandles,
                                     handles,
                                     FALSE,
-                                    abstime);
+                                    timeout);
 
 
   if (status == WAIT_FAILED)
@@ -185,10 +184,13 @@ pthreadCancelableWait (HANDLE waitHandle, DWORD abstime)
       result = EINVAL;
 
     }
-  else if (status == WAIT_ABANDONED_0)
+  else if (status == WAIT_TIMEOUT)
     {
       result = ETIMEDOUT;
-
+    }
+  else if (status == WAIT_ABANDONED_0)
+    {
+      result = EINVAL;
     }
   else
     {
@@ -243,6 +245,17 @@ pthreadCancelableWait (HANDLE waitHandle, DWORD abstime)
 
 }                               /* pthreadCancelableWait */
 
+int
+pthreadCancelableWait (HANDLE waitHandle)
+{
+  return (CancelableWait(waitHandle, INFINITE));
+}
+
+int
+pthreadCancelableTimedWait (HANDLE waitHandle, DWORD timeout)
+{
+  return (CancelableWait(waitHandle, timeout));
+}
 
 /* </JEB> */
 
diff --git a/private.c b/private.c
index f20413c..1039d81 100644
--- a/private.c
+++ b/private.c
@@ -443,8 +443,8 @@ _pthread_sem_timedwait (sem_t * sem, const struct timespec * abstime)
      /*
       * ------------------------------------------------------
       * DOCPUBLIC
-      *      This function  waits on a semaphore for at most
-      *      'abstime'.
+      *      This function waits on a semaphore possibly until
+      *      'abstime' time.
       *
       * PARAMETERS
       *      sem
@@ -476,7 +476,10 @@ _pthread_sem_timedwait (sem_t * sem, const struct timespec * abstime)
       * ------------------------------------------------------
       */
 {
-  DWORD msecs;
+  struct _timeb currSysTime;
+  const DWORD NANOSEC_PER_MILLISEC = 1000000;
+  const DWORD MILLISEC_PER_SEC = 1000;
+  DWORD milliseconds;
 
   if (abstime == NULL)
     {
@@ -484,14 +487,22 @@ _pthread_sem_timedwait (sem_t * sem, const struct timespec * abstime)
     }
   else
     {
-      /* Calculate the number of milliseconds in abstime. */
-      msecs = abstime->tv_sec * 1000;
-      msecs += abstime->tv_nsec / 1000000;
+      /* 
+       * Calculate timeout as milliseconds from current system time. 
+       */
+
+      /* get current system time */
+      _ftime(&currSysTime);
+
+      /* subtract current system time from abstime */
+      milliseconds = (abstime->tv_sec - currSysTime.time) * MILLISEC_PER_SEC;
+      milliseconds += (abstime->tv_nsec / NANOSEC_PER_MILLISEC) -
+	currSysTime.millitm;
     }
 
   return ((sem == NULL)
 	  ? EINVAL
-	  : pthreadCancelableWait (*sem, msecs)
+	  : pthreadCancelableTimedWait (*sem, milliseconds)
     );
 
 }				/* _pthread_sem_timedwait */
diff --git a/pthread.h b/pthread.h
index 861a0e8..aa48b32 100644
--- a/pthread.h
+++ b/pthread.h
@@ -918,10 +918,12 @@ int pthread_attr_setschedparam (pthread_attr_t *attr,
  *              WaitForMultipleObjects
  *
  * on 'waitHandle' and a manually reset WIN32 Event
- * used to implement pthread_cancel. The 'abstime'
- * argument is simply passed to WaitForMultipleObjects.
+ * used to implement pthread_cancel. The 'timeout'
+ * argument to TimedWait is simply passed to
+ * WaitForMultipleObjects.
  */
-int pthreadCancelableWait (HANDLE waitHandle, DWORD abstime);
+int pthreadCancelableWait (HANDLE waitHandle);
+int pthreadCancelableTimedWait (HANDLE waitHandle, DWORD timeout);
 
 /*
  * Thread-Safe C Runtime Library Mappings
-- 
cgit v1.2.3