drivers/can_ixxat_win32/async_access_que.h
author Laurent Bessard
Wed, 24 Apr 2013 17:28:06 +0200
changeset 781 77fe158d6ccd
parent 643 8b67ee3f5363
permissions -rw-r--r--
Fixed Save As... function in Beremiz
255
7b9f36dbfe5f *** empty log message ***
leonid
parents: 252
diff changeset
     1
/*
7b9f36dbfe5f *** empty log message ***
leonid
parents: 252
diff changeset
     2
This file is part of CanFestival, a library implementing CanOpen Stack.
7b9f36dbfe5f *** empty log message ***
leonid
parents: 252
diff changeset
     3
7b9f36dbfe5f *** empty log message ***
leonid
parents: 252
diff changeset
     4
CanFestival Copyright (C): Edouard TISSERANT and Francis DUPIN
7b9f36dbfe5f *** empty log message ***
leonid
parents: 252
diff changeset
     5
CanFestival Win32 port Copyright (C) 2007 Leonid Tochinski, ChattenAssociates, Inc.
7b9f36dbfe5f *** empty log message ***
leonid
parents: 252
diff changeset
     6
7b9f36dbfe5f *** empty log message ***
leonid
parents: 252
diff changeset
     7
See COPYING file for copyrights details.
7b9f36dbfe5f *** empty log message ***
leonid
parents: 252
diff changeset
     8
7b9f36dbfe5f *** empty log message ***
leonid
parents: 252
diff changeset
     9
This library is free software; you can redistribute it and/or
7b9f36dbfe5f *** empty log message ***
leonid
parents: 252
diff changeset
    10
modify it under the terms of the GNU Lesser General Public
7b9f36dbfe5f *** empty log message ***
leonid
parents: 252
diff changeset
    11
License as published by the Free Software Foundation; either
7b9f36dbfe5f *** empty log message ***
leonid
parents: 252
diff changeset
    12
version 2.1 of the License, or (at your option) any later version.
7b9f36dbfe5f *** empty log message ***
leonid
parents: 252
diff changeset
    13
7b9f36dbfe5f *** empty log message ***
leonid
parents: 252
diff changeset
    14
This library is distributed in the hope that it will be useful,
7b9f36dbfe5f *** empty log message ***
leonid
parents: 252
diff changeset
    15
but WITHOUT ANY WARRANTY; without even the implied warranty of
7b9f36dbfe5f *** empty log message ***
leonid
parents: 252
diff changeset
    16
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
7b9f36dbfe5f *** empty log message ***
leonid
parents: 252
diff changeset
    17
Lesser General Public License for more details.
7b9f36dbfe5f *** empty log message ***
leonid
parents: 252
diff changeset
    18
7b9f36dbfe5f *** empty log message ***
leonid
parents: 252
diff changeset
    19
You should have received a copy of the GNU Lesser General Public
7b9f36dbfe5f *** empty log message ***
leonid
parents: 252
diff changeset
    20
License along with this library; if not, write to the Free Software
7b9f36dbfe5f *** empty log message ***
leonid
parents: 252
diff changeset
    21
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
7b9f36dbfe5f *** empty log message ***
leonid
parents: 252
diff changeset
    22
*/
7b9f36dbfe5f *** empty log message ***
leonid
parents: 252
diff changeset
    23
252
b0dd37421d28 *** empty log message ***
leonid
parents:
diff changeset
    24
// thread safe que
255
7b9f36dbfe5f *** empty log message ***
leonid
parents: 252
diff changeset
    25
#ifndef __async_access_que_h__
7b9f36dbfe5f *** empty log message ***
leonid
parents: 252
diff changeset
    26
#define __async_access_que_h__
7b9f36dbfe5f *** empty log message ***
leonid
parents: 252
diff changeset
    27
252
b0dd37421d28 *** empty log message ***
leonid
parents:
diff changeset
    28
#include <deque>
b0dd37421d28 *** empty log message ***
leonid
parents:
diff changeset
    29
#include "AutoReleaseCS.h"
b0dd37421d28 *** empty log message ***
leonid
parents:
diff changeset
    30
b0dd37421d28 *** empty log message ***
leonid
parents:
diff changeset
    31
template<typename type>
b0dd37421d28 *** empty log message ***
leonid
parents:
diff changeset
    32
class async_access_que
b0dd37421d28 *** empty log message ***
leonid
parents:
diff changeset
    33
   {
b0dd37421d28 *** empty log message ***
leonid
parents:
diff changeset
    34
   public:
b0dd37421d28 *** empty log message ***
leonid
parents:
diff changeset
    35
      async_access_que()
b0dd37421d28 *** empty log message ***
leonid
parents:
diff changeset
    36
         {
b0dd37421d28 *** empty log message ***
leonid
parents:
diff changeset
    37
         ::InitializeCriticalSection(&m_cs);
643
8b67ee3f5363 CHANGED: - async access queue has now blocking read
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents: 255
diff changeset
    38
8b67ee3f5363 CHANGED: - async access queue has now blocking read
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents: 255
diff changeset
    39
		 m_newObject = CreateEvent(NULL, FALSE, FALSE, NULL);
8b67ee3f5363 CHANGED: - async access queue has now blocking read
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents: 255
diff changeset
    40
		 m_stop = CreateEvent(NULL, FALSE, FALSE, NULL);
8b67ee3f5363 CHANGED: - async access queue has now blocking read
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents: 255
diff changeset
    41
		 m_stopped = CreateEvent(NULL, FALSE, FALSE, NULL);
8b67ee3f5363 CHANGED: - async access queue has now blocking read
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents: 255
diff changeset
    42
		 m_commands[0] = m_newObject;
8b67ee3f5363 CHANGED: - async access queue has now blocking read
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents: 255
diff changeset
    43
		 m_commands[1] = m_stop;
8b67ee3f5363 CHANGED: - async access queue has now blocking read
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents: 255
diff changeset
    44
252
b0dd37421d28 *** empty log message ***
leonid
parents:
diff changeset
    45
         }
b0dd37421d28 *** empty log message ***
leonid
parents:
diff changeset
    46
      ~async_access_que()
b0dd37421d28 *** empty log message ***
leonid
parents:
diff changeset
    47
         {
643
8b67ee3f5363 CHANGED: - async access queue has now blocking read
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents: 255
diff changeset
    48
		 SignalObjectAndWait(m_stop, m_stopped, 500, FALSE);
252
b0dd37421d28 *** empty log message ***
leonid
parents:
diff changeset
    49
         ::DeleteCriticalSection(&m_cs);
643
8b67ee3f5363 CHANGED: - async access queue has now blocking read
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents: 255
diff changeset
    50
		 CloseHandle(m_stop);
8b67ee3f5363 CHANGED: - async access queue has now blocking read
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents: 255
diff changeset
    51
		 CloseHandle(m_stopped);
8b67ee3f5363 CHANGED: - async access queue has now blocking read
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents: 255
diff changeset
    52
		 CloseHandle(m_newObject);
252
b0dd37421d28 *** empty log message ***
leonid
parents:
diff changeset
    53
         }
b0dd37421d28 *** empty log message ***
leonid
parents:
diff changeset
    54
b0dd37421d28 *** empty log message ***
leonid
parents:
diff changeset
    55
      void append(const type& data)
b0dd37421d28 *** empty log message ***
leonid
parents:
diff changeset
    56
         {
b0dd37421d28 *** empty log message ***
leonid
parents:
diff changeset
    57
         AutoReleaseCS acs(m_cs);
b0dd37421d28 *** empty log message ***
leonid
parents:
diff changeset
    58
         m_data.push_back(data);
643
8b67ee3f5363 CHANGED: - async access queue has now blocking read
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents: 255
diff changeset
    59
	 SetEvent(m_newObject);
252
b0dd37421d28 *** empty log message ***
leonid
parents:
diff changeset
    60
         }
b0dd37421d28 *** empty log message ***
leonid
parents:
diff changeset
    61
b0dd37421d28 *** empty log message ***
leonid
parents:
diff changeset
    62
      bool extract_top(type& data)
b0dd37421d28 *** empty log message ***
leonid
parents:
diff changeset
    63
         {
643
8b67ee3f5363 CHANGED: - async access queue has now blocking read
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents: 255
diff changeset
    64
			 bool empty = true;
8b67ee3f5363 CHANGED: - async access queue has now blocking read
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents: 255
diff changeset
    65
			 {
8b67ee3f5363 CHANGED: - async access queue has now blocking read
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents: 255
diff changeset
    66
				 AutoReleaseCS acs(m_cs);
8b67ee3f5363 CHANGED: - async access queue has now blocking read
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents: 255
diff changeset
    67
				 empty = m_data.empty();
8b67ee3f5363 CHANGED: - async access queue has now blocking read
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents: 255
diff changeset
    68
			 }
8b67ee3f5363 CHANGED: - async access queue has now blocking read
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents: 255
diff changeset
    69
8b67ee3f5363 CHANGED: - async access queue has now blocking read
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents: 255
diff changeset
    70
			 if (empty)
8b67ee3f5363 CHANGED: - async access queue has now blocking read
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents: 255
diff changeset
    71
			 {
8b67ee3f5363 CHANGED: - async access queue has now blocking read
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents: 255
diff changeset
    72
				 DWORD objectIndex;
8b67ee3f5363 CHANGED: - async access queue has now blocking read
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents: 255
diff changeset
    73
				 do
8b67ee3f5363 CHANGED: - async access queue has now blocking read
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents: 255
diff changeset
    74
				 {
8b67ee3f5363 CHANGED: - async access queue has now blocking read
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents: 255
diff changeset
    75
					objectIndex = WaitForMultipleObjects(sizeof(m_commands) / sizeof(&(m_commands[0])), m_commands, FALSE, INFINITE);
8b67ee3f5363 CHANGED: - async access queue has now blocking read
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents: 255
diff changeset
    76
					if (objectIndex - WAIT_OBJECT_0 == 1)  //m_stop
8b67ee3f5363 CHANGED: - async access queue has now blocking read
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents: 255
diff changeset
    77
					{
8b67ee3f5363 CHANGED: - async access queue has now blocking read
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents: 255
diff changeset
    78
						SetEvent(m_stopped);
8b67ee3f5363 CHANGED: - async access queue has now blocking read
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents: 255
diff changeset
    79
						return false; //This will exit the canReceive-loop
8b67ee3f5363 CHANGED: - async access queue has now blocking read
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents: 255
diff changeset
    80
					}
8b67ee3f5363 CHANGED: - async access queue has now blocking read
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents: 255
diff changeset
    81
				} while (objectIndex - WAIT_OBJECT_0 != 0);
8b67ee3f5363 CHANGED: - async access queue has now blocking read
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents: 255
diff changeset
    82
			 }
8b67ee3f5363 CHANGED: - async access queue has now blocking read
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents: 255
diff changeset
    83
8b67ee3f5363 CHANGED: - async access queue has now blocking read
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents: 255
diff changeset
    84
			 {
8b67ee3f5363 CHANGED: - async access queue has now blocking read
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents: 255
diff changeset
    85
				AutoReleaseCS acs(m_cs);
8b67ee3f5363 CHANGED: - async access queue has now blocking read
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents: 255
diff changeset
    86
				if (m_data.empty())
8b67ee3f5363 CHANGED: - async access queue has now blocking read
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents: 255
diff changeset
    87
				{
8b67ee3f5363 CHANGED: - async access queue has now blocking read
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents: 255
diff changeset
    88
					return false; //This will exit the canReceive-loop
8b67ee3f5363 CHANGED: - async access queue has now blocking read
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents: 255
diff changeset
    89
				}
8b67ee3f5363 CHANGED: - async access queue has now blocking read
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents: 255
diff changeset
    90
				data = m_data.front();
8b67ee3f5363 CHANGED: - async access queue has now blocking read
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents: 255
diff changeset
    91
				m_data.pop_front();
8b67ee3f5363 CHANGED: - async access queue has now blocking read
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents: 255
diff changeset
    92
				ResetEvent(m_newObject);
8b67ee3f5363 CHANGED: - async access queue has now blocking read
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents: 255
diff changeset
    93
				return true;
8b67ee3f5363 CHANGED: - async access queue has now blocking read
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents: 255
diff changeset
    94
			}
252
b0dd37421d28 *** empty log message ***
leonid
parents:
diff changeset
    95
         }
b0dd37421d28 *** empty log message ***
leonid
parents:
diff changeset
    96
         
b0dd37421d28 *** empty log message ***
leonid
parents:
diff changeset
    97
      void clear()
b0dd37421d28 *** empty log message ***
leonid
parents:
diff changeset
    98
         {
b0dd37421d28 *** empty log message ***
leonid
parents:
diff changeset
    99
         AutoReleaseCS acs(m_cs);
b0dd37421d28 *** empty log message ***
leonid
parents:
diff changeset
   100
         m_data.clear();
b0dd37421d28 *** empty log message ***
leonid
parents:
diff changeset
   101
         }
b0dd37421d28 *** empty log message ***
leonid
parents:
diff changeset
   102
         
b0dd37421d28 *** empty log message ***
leonid
parents:
diff changeset
   103
   protected:
b0dd37421d28 *** empty log message ***
leonid
parents:
diff changeset
   104
      std::deque<type> m_data;
b0dd37421d28 *** empty log message ***
leonid
parents:
diff changeset
   105
      CRITICAL_SECTION m_cs;
643
8b67ee3f5363 CHANGED: - async access queue has now blocking read
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents: 255
diff changeset
   106
8b67ee3f5363 CHANGED: - async access queue has now blocking read
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents: 255
diff changeset
   107
	  HANDLE m_newObject;
8b67ee3f5363 CHANGED: - async access queue has now blocking read
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents: 255
diff changeset
   108
	  HANDLE m_stop;
8b67ee3f5363 CHANGED: - async access queue has now blocking read
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents: 255
diff changeset
   109
	  HANDLE m_stopped;
8b67ee3f5363 CHANGED: - async access queue has now blocking read
Christian Taedcke <Christian.Taedcke@ica-traffic.de>
parents: 255
diff changeset
   110
	  HANDLE m_commands[2];
252
b0dd37421d28 *** empty log message ***
leonid
parents:
diff changeset
   111
   };
255
7b9f36dbfe5f *** empty log message ***
leonid
parents: 252
diff changeset
   112
#endif //__async_access_que_h__