drivers/can_ixxat_win32/async_access_que.h
changeset 643 8b67ee3f5363
parent 255 7b9f36dbfe5f
equal deleted inserted replaced
642:f77ad188c2b1 643:8b67ee3f5363
    33    {
    33    {
    34    public:
    34    public:
    35       async_access_que()
    35       async_access_que()
    36          {
    36          {
    37          ::InitializeCriticalSection(&m_cs);
    37          ::InitializeCriticalSection(&m_cs);
       
    38 
       
    39 		 m_newObject = CreateEvent(NULL, FALSE, FALSE, NULL);
       
    40 		 m_stop = CreateEvent(NULL, FALSE, FALSE, NULL);
       
    41 		 m_stopped = CreateEvent(NULL, FALSE, FALSE, NULL);
       
    42 		 m_commands[0] = m_newObject;
       
    43 		 m_commands[1] = m_stop;
       
    44 
    38          }
    45          }
    39       ~async_access_que()
    46       ~async_access_que()
    40          {
    47          {
       
    48 		 SignalObjectAndWait(m_stop, m_stopped, 500, FALSE);
    41          ::DeleteCriticalSection(&m_cs);
    49          ::DeleteCriticalSection(&m_cs);
       
    50 		 CloseHandle(m_stop);
       
    51 		 CloseHandle(m_stopped);
       
    52 		 CloseHandle(m_newObject);
    42          }
    53          }
    43 
    54 
    44       void append(const type& data)
    55       void append(const type& data)
    45          {
    56          {
    46          AutoReleaseCS acs(m_cs);
    57          AutoReleaseCS acs(m_cs);
    47          m_data.push_back(data);
    58          m_data.push_back(data);
       
    59 	 SetEvent(m_newObject);
    48          }
    60          }
    49 
    61 
    50       bool extract_top(type& data)
    62       bool extract_top(type& data)
    51          {
    63          {
    52          AutoReleaseCS acs(m_cs);
    64 			 bool empty = true;
    53          if (m_data.empty())
    65 			 {
    54             return false;
    66 				 AutoReleaseCS acs(m_cs);
    55           data = m_data.front();
    67 				 empty = m_data.empty();
    56           m_data.pop_front();
    68 			 }
    57          return true;
    69 
       
    70 			 if (empty)
       
    71 			 {
       
    72 				 DWORD objectIndex;
       
    73 				 do
       
    74 				 {
       
    75 					objectIndex = WaitForMultipleObjects(sizeof(m_commands) / sizeof(&(m_commands[0])), m_commands, FALSE, INFINITE);
       
    76 					if (objectIndex - WAIT_OBJECT_0 == 1)  //m_stop
       
    77 					{
       
    78 						SetEvent(m_stopped);
       
    79 						return false; //This will exit the canReceive-loop
       
    80 					}
       
    81 				} while (objectIndex - WAIT_OBJECT_0 != 0);
       
    82 			 }
       
    83 
       
    84 			 {
       
    85 				AutoReleaseCS acs(m_cs);
       
    86 				if (m_data.empty())
       
    87 				{
       
    88 					return false; //This will exit the canReceive-loop
       
    89 				}
       
    90 				data = m_data.front();
       
    91 				m_data.pop_front();
       
    92 				ResetEvent(m_newObject);
       
    93 				return true;
       
    94 			}
    58          }
    95          }
    59          
    96          
    60       void clear()
    97       void clear()
    61          {
    98          {
    62          AutoReleaseCS acs(m_cs);
    99          AutoReleaseCS acs(m_cs);
    64          }
   101          }
    65          
   102          
    66    protected:
   103    protected:
    67       std::deque<type> m_data;
   104       std::deque<type> m_data;
    68       CRITICAL_SECTION m_cs;
   105       CRITICAL_SECTION m_cs;
       
   106 
       
   107 	  HANDLE m_newObject;
       
   108 	  HANDLE m_stop;
       
   109 	  HANDLE m_stopped;
       
   110 	  HANDLE m_commands[2];
    69    };
   111    };
    70 #endif //__async_access_que_h__
   112 #endif //__async_access_que_h__