-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queue.h
167 lines (134 loc) · 2.03 KB
/
Queue.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#pragma once
class Event;
class Mutex;
#include "ace/unbounded_queue.h"
class Mutex
{
public:
Mutex()
{
#ifdef _MSC_VER
InitializeCriticalSection( &m_mutex );
#endif
}
~Mutex()
{
#ifdef _MSC_VER
DeleteCriticalSection( &m_mutex );
#endif
}
void lock()
{
#ifdef _MSC_VER
EnterCriticalSection( &m_mutex );
#endif
}
void unlock()
{
#ifdef _MSC_VER
LeaveCriticalSection( &m_mutex );
#endif
}
private:
#ifdef _MSC_VER
CRITICAL_SECTION m_mutex;
#endif
};
class Locker
{
public:
Locker( Mutex& mutex ) : m_mutex( mutex )
{
m_mutex.lock();
}
~Locker()
{
m_mutex.unlock();
}
private:
Mutex& m_mutex;
};
class Event
{
public:
Event()
{
#ifdef _MSC_VER
m_event = CreateEvent( 0, false, false, 0 );
#endif
}
~Event()
{
#ifdef _MSC_VER
CloseHandle( m_event );
#endif
}
void signal()
{
#ifdef _MSC_VER
SetEvent( m_event );
#endif
}
void wait()
{
#ifdef _MSC_VER
WaitForSingleObject(m_event, INFINITE );
#endif
}
void wait(unsigned long ulWaitTime)
{
#ifdef _MSC_VER
WaitForSingleObject( m_event, ulWaitTime );
#endif
}
private:
#ifdef _MSC_VER
HANDLE m_event;
#endif
};
template < typename T > class CQueue
{
public:
void push( const T& value )
{
Locker locker( m_mutex );
m_queue.enqueue_tail( value );
if( m_queue.size() == 1 )
signal();
}
void push_front(const T& value)
{
Locker locker( m_mutex );
m_queue.enqueue_head( value );
if (m_queue.size() == 1)
signal();
}
bool pop( T& value )
{
Locker locker( m_mutex );
if ( !m_queue.size() ) return false;
m_queue.dequeue_head( value );
return true;
}
int size()
{
Locker locker( m_mutex );
return m_queue.size();
}
void wait()
{
m_event.wait();
}
void wait(unsigned long ulWaitTime)
{
m_event.wait(ulWaitTime);
}
void signal()
{
m_event.signal();
}
private:
Event m_event;
Mutex m_mutex;
ACE_Unbounded_Queue<T> m_queue;
};