author | Edouard Tisserant |
Wed, 13 Apr 2011 16:29:59 +0200 | |
changeset 659 | 1041153c5fd2 |
parent 643 | 8b67ee3f5363 |
permissions | -rw-r--r-- |
255 | 1 |
/* |
2 |
This file is part of CanFestival, a library implementing CanOpen Stack. |
|
3 |
||
4 |
CanFestival Copyright (C): Edouard TISSERANT and Francis DUPIN |
|
5 |
CanFestival Win32 port Copyright (C) 2007 Leonid Tochinski, ChattenAssociates, Inc. |
|
6 |
||
7 |
See COPYING file for copyrights details. |
|
8 |
||
9 |
This library is free software; you can redistribute it and/or |
|
10 |
modify it under the terms of the GNU Lesser General Public |
|
11 |
License as published by the Free Software Foundation; either |
|
12 |
version 2.1 of the License, or (at your option) any later version. |
|
13 |
||
14 |
This library is distributed in the hope that it will be useful, |
|
15 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
17 |
Lesser General Public License for more details. |
|
18 |
||
19 |
You should have received a copy of the GNU Lesser General Public |
|
20 |
License along with this library; if not, write to the Free Software |
|
21 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
22 |
*/ |
|
23 |
||
252 | 24 |
// thread safe que |
255 | 25 |
#ifndef __async_access_que_h__ |
26 |
#define __async_access_que_h__ |
|
27 |
||
252 | 28 |
#include <deque> |
29 |
#include "AutoReleaseCS.h" |
|
30 |
||
31 |
template<typename type> |
|
32 |
class async_access_que |
|
33 |
{ |
|
34 |
public: |
|
35 |
async_access_que() |
|
36 |
{ |
|
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 | 45 |
} |
46 |
~async_access_que() |
|
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 | 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 | 53 |
} |
54 |
||
55 |
void append(const type& data) |
|
56 |
{ |
|
57 |
AutoReleaseCS acs(m_cs); |
|
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 | 60 |
} |
61 |
||
62 |
bool extract_top(type& data) |
|
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 | 95 |
} |
96 |
||
97 |
void clear() |
|
98 |
{ |
|
99 |
AutoReleaseCS acs(m_cs); |
|
100 |
m_data.clear(); |
|
101 |
} |
|
102 |
||
103 |
protected: |
|
104 |
std::deque<type> m_data; |
|
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 | 111 |
}; |
255 | 112 |
#endif //__async_access_que_h__ |