CHANGED: - async access queue has now blocking read
authorChristian Taedcke <Christian.Taedcke@ica-traffic.de>
Fri, 19 Feb 2010 15:51:40 +0100
changeset 643 8b67ee3f5363
parent 642 f77ad188c2b1
child 644 11b21e9a92f6
CHANGED: - async access queue has now blocking read
drivers/can_ixxat_win32/async_access_que.h
--- a/drivers/can_ixxat_win32/async_access_que.h	Tue Feb 23 08:12:19 2010 +0100
+++ b/drivers/can_ixxat_win32/async_access_que.h	Fri Feb 19 15:51:40 2010 +0100
@@ -35,26 +35,63 @@
       async_access_que()
          {
          ::InitializeCriticalSection(&m_cs);
+
+		 m_newObject = CreateEvent(NULL, FALSE, FALSE, NULL);
+		 m_stop = CreateEvent(NULL, FALSE, FALSE, NULL);
+		 m_stopped = CreateEvent(NULL, FALSE, FALSE, NULL);
+		 m_commands[0] = m_newObject;
+		 m_commands[1] = m_stop;
+
          }
       ~async_access_que()
          {
+		 SignalObjectAndWait(m_stop, m_stopped, 500, FALSE);
          ::DeleteCriticalSection(&m_cs);
+		 CloseHandle(m_stop);
+		 CloseHandle(m_stopped);
+		 CloseHandle(m_newObject);
          }
 
       void append(const type& data)
          {
          AutoReleaseCS acs(m_cs);
          m_data.push_back(data);
+	 SetEvent(m_newObject);
          }
 
       bool extract_top(type& data)
          {
-         AutoReleaseCS acs(m_cs);
-         if (m_data.empty())
-            return false;
-          data = m_data.front();
-          m_data.pop_front();
-         return true;
+			 bool empty = true;
+			 {
+				 AutoReleaseCS acs(m_cs);
+				 empty = m_data.empty();
+			 }
+
+			 if (empty)
+			 {
+				 DWORD objectIndex;
+				 do
+				 {
+					objectIndex = WaitForMultipleObjects(sizeof(m_commands) / sizeof(&(m_commands[0])), m_commands, FALSE, INFINITE);
+					if (objectIndex - WAIT_OBJECT_0 == 1)  //m_stop
+					{
+						SetEvent(m_stopped);
+						return false; //This will exit the canReceive-loop
+					}
+				} while (objectIndex - WAIT_OBJECT_0 != 0);
+			 }
+
+			 {
+				AutoReleaseCS acs(m_cs);
+				if (m_data.empty())
+				{
+					return false; //This will exit the canReceive-loop
+				}
+				data = m_data.front();
+				m_data.pop_front();
+				ResetEvent(m_newObject);
+				return true;
+			}
          }
          
       void clear()
@@ -66,5 +103,10 @@
    protected:
       std::deque<type> m_data;
       CRITICAL_SECTION m_cs;
+
+	  HANDLE m_newObject;
+	  HANDLE m_stop;
+	  HANDLE m_stopped;
+	  HANDLE m_commands[2];
    };
 #endif //__async_access_que_h__
\ No newline at end of file