Revision: 6450
          http://playerstage.svn.sourceforge.net/playerstage/?rev=6450&view=rev
Author:   thjc
Date:     2008-05-20 18:08:44 -0700 (Tue, 20 May 2008)

Log Message:
-----------
added ability to have a timeout on the message queue wait, default is wait 
forever

Modified Paths:
--------------
    code/player/branches/release-2-1-patches/libplayercore/driver.h
    code/player/branches/release-2-1-patches/libplayercore/message.cc
    code/player/branches/release-2-1-patches/libplayercore/message.h

Modified: code/player/branches/release-2-1-patches/libplayercore/driver.h
===================================================================
--- code/player/branches/release-2-1-patches/libplayercore/driver.h     
2008-05-20 00:33:20 UTC (rev 6449)
+++ code/player/branches/release-2-1-patches/libplayercore/driver.h     
2008-05-21 01:08:44 UTC (rev 6450)
@@ -153,9 +153,13 @@
     /** @brief Wait for new data to arrive on the driver's queue.
 
     Call this method to block until a new message arrives on
-    Driver::InQueue.  This method will return immediately if at least
-    one message is already waiting. */
-    void Wait() { this->InQueue->Wait(); }
+    Driver::InQueue.  This method will return true immediately if at least
+    one message is already waiting. 
+    
+    If TimeOut is set to a positive value this method will return false if the
+    timeout occurs before and update is recieved.
+    */
+    bool Wait(double TimeOut=0.0) { return this->InQueue->Wait(); }
 
   public:
     /** @brief The driver's thread.

Modified: code/player/branches/release-2-1-patches/libplayercore/message.cc
===================================================================
--- code/player/branches/release-2-1-patches/libplayercore/message.cc   
2008-05-20 00:33:20 UTC (rev 6449)
+++ code/player/branches/release-2-1-patches/libplayercore/message.cc   
2008-05-21 01:08:44 UTC (rev 6450)
@@ -47,6 +47,8 @@
 #include <assert.h>
 #include <string.h>
 #include <stdio.h>
+#include <math.h>
+#include <time.h>
 
 #include <libplayercore/message.h>
 #include <libplayercore/player.h>
@@ -303,9 +305,10 @@
 }
 
 // Waits on the condition variable associated with this queue.
-void
-MessageQueue::Wait(void)
+bool
+MessageQueue::Wait(double TimeOut)
 {
+  bool result = true;
   MessageQueueElement* el;
 
   // don't wait if there's data on the queue
@@ -319,7 +322,7 @@
   }
   this->Unlock();
   if(el)
-    return;
+    return result;
 
   // need to push this cleanup function, cause if a thread is cancelled while
   // in pthread_cond_wait(), it will immediately relock the mutex.  thus we
@@ -327,9 +330,25 @@
   pthread_cleanup_push((void(*)(void*))pthread_mutex_unlock,
                        (void*)&this->condMutex);
   pthread_mutex_lock(&this->condMutex);
-  pthread_cond_wait(&this->cond,&this->condMutex);
+  if (TimeOut > 0)
+  {
+    struct timespec tp;
+    clock_gettime(CLOCK_REALTIME, &tp);
+    tp.tv_sec += static_cast<int> (floor(TimeOut));
+    tp.tv_nsec += static_cast<int> ((TimeOut - floor(TimeOut))*1e9);
+    int ret = pthread_cond_timedwait(&this->cond, &this->condMutex, &tp);
+    // if we got an error or timed out
+    if (ret != 0)
+      result = false;
+  }
+  else
+  {
+    pthread_cond_wait(&this->cond,&this->condMutex);  
+  }
+  
   pthread_mutex_unlock(&this->condMutex);
   pthread_cleanup_pop(0);
+  return result;
 }
 
 bool

Modified: code/player/branches/release-2-1-patches/libplayercore/message.h
===================================================================
--- code/player/branches/release-2-1-patches/libplayercore/message.h    
2008-05-20 00:33:20 UTC (rev 6449)
+++ code/player/branches/release-2-1-patches/libplayercore/message.h    
2008-05-21 01:08:44 UTC (rev 6450)
@@ -374,8 +374,12 @@
     /// any existing message of the same signature, be ignored or accepted.
     int CheckReplace(player_msghdr_t* hdr);
     /** Wait on this queue.  This method blocks until new data is available
-    (as indicated by a call to DataAvailable()). */
-    void Wait(void);
+    (as indicated by a call to DataAvailable()). 
+            
+    If TimeOut is set to a positive value this method will return false if the
+    timeout occurs before and update is recieved.
+     */
+    bool Wait(double TimeOut=0.0);
     /** Signal that new data is available.  Calling this method will
      release any threads currently waiting on this queue. */
     void DataAvailable(void);


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft 
Defy all challenges. Microsoft(R) Visual Studio 2008. 
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Playerstage-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/playerstage-commit

Reply via email to