changeset 127:f1944fbcf419 main

Make the logging optional. Use a thread-safe mechanism to signal quit.
author Bob Cook <bob@bobcookdev.com>
date Thu, 15 Dec 2011 06:59:06 -0800
parents b2a2c22951fb
children 44140fb96102
files main/packages/linux/can/CANMsgProcessor.cpp main/packages/linux/can/CANMsgProcessor.h
diffstat 2 files changed, 20 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/main/packages/linux/can/CANMsgProcessor.cpp	Thu Dec 15 06:57:02 2011 -0800
+++ b/main/packages/linux/can/CANMsgProcessor.cpp	Thu Dec 15 06:59:06 2011 -0800
@@ -45,6 +45,18 @@
 
 // ----------------------------------------------------------------------------------------
 
+CANMsgProcessor::CANMsgProcessor( const std::string& canInterface )
+    : Poco::Runnable(),
+      m_canInterfaceName( canInterface ),
+      m_loggerName(),
+      m_quitEvent( false /* not auto-reset */ ),
+      m_logIncoming( false ),
+      m_logOutgoing( false )
+{
+}
+
+// ----------------------------------------------------------------------------------------
+
 CANMsgProcessor::CANMsgProcessor
 (
     const std::string& canInterface,
@@ -53,9 +65,9 @@
     : Poco::Runnable(),
       m_canInterfaceName( canInterface ),
       m_loggerName( logger ),
+      m_quitEvent( false /* not auto-reset */ ),
       m_logIncoming( false ),
-      m_logOutgoing( false ),
-      m_quitNow( false )
+      m_logOutgoing( false )
 {
 }
 
@@ -83,7 +95,7 @@
 
 void CANMsgProcessor::timeToQuit()
 {
-    m_quitNow = true;
+    m_quitEvent.set();
 }
 
 // ----------------------------------------------------------------------------------------
@@ -94,9 +106,6 @@
 
     for ( ;; )
     {
-        log.information( std::string( "CANMsgProcessor::run() starting for " )
-                + m_canInterfaceName );
-
         try
         {
             CANSocket sock;
@@ -127,7 +136,7 @@
 
                 Poco::AutoPtr<CANMessage> msg( CANMessage::WaitDequeueToSend( 100 ) ); // 100ms
 
-                if ( msg.get() != 0 && ! m_quitNow )
+                if ( msg.get() != 0 )
                 {
                     if ( sock.poll( writeWaitTime, Poco::Net::Socket::SELECT_WRITE ) )
                     {
@@ -155,9 +164,8 @@
 
                 // time to quit?
 
-                if ( m_quitNow )
+                if ( m_quitEvent.tryWait( 0 ) && CANMessage::IsSendQueueEmpty() )
                 {
-                    log.information(std::string( "CANMsgProcessor::run() stopping" ) );
                     return;
                 }
             }
--- a/main/packages/linux/can/CANMsgProcessor.h	Thu Dec 15 06:57:02 2011 -0800
+++ b/main/packages/linux/can/CANMsgProcessor.h	Thu Dec 15 06:59:06 2011 -0800
@@ -36,6 +36,7 @@
 
 #include <string>
 
+#include <Poco/Event.h>
 #include <Poco/Runnable.h>
 
 // ----------------------------------------------------------------------------------------
@@ -43,6 +44,7 @@
 class CANMsgProcessor : public Poco::Runnable
 {
     public:
+        CANMsgProcessor( const std::string& canInterface );
         CANMsgProcessor( const std::string& canInterface, const std::string& logger );
         virtual ~CANMsgProcessor();
         virtual void run();
@@ -53,9 +55,9 @@
     private:
         std::string m_canInterfaceName;
         std::string m_loggerName;
+        Poco::Event m_quitEvent;
         bool        m_logIncoming;
         bool        m_logOutgoing;
-        bool        m_quitNow;
 };
 
 // ----------------------------------------------------------------------------------------