-
Notifications
You must be signed in to change notification settings - Fork 102
/
commandQueue.cpp
148 lines (127 loc) · 3.82 KB
/
commandQueue.cpp
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
/* Copyright (c) 2007-2015, Stefan Eilemann <eile@equalizergraphics.com>
* Daniel Nachbaur <danielnachbaur@gmail.com>
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License version 2.1 as published
* by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "commandQueue.h"
#include "messagePump.h"
#include <co/iCommand.h>
#include <lunchbox/clock.h>
namespace eq
{
namespace
{
static lunchbox::Clock _clock;
}
CommandQueue::CommandQueue( const size_t maxSize )
: co::CommandQueue( maxSize )
, _messagePump( 0 )
, _waitTime( 0 )
{}
CommandQueue::~CommandQueue()
{
LBASSERT( !_messagePump );
delete _messagePump;
_messagePump = 0;
}
void CommandQueue::push( const co::ICommand& command )
{
co::CommandQueue::push( command );
if( _messagePump )
_messagePump->postWakeup();
}
void CommandQueue::pushFront( const co::ICommand& command )
{
co::CommandQueue::pushFront( command );
if( _messagePump )
_messagePump->postWakeup();
}
co::ICommand CommandQueue::pop( const uint32_t timeout )
{
const int64_t start = _clock.getTime64();
int64_t waitBegin = -1;
while( true )
{
if( _messagePump )
_messagePump->dispatchAll(); // non-blocking
// Poll for a command
if( !isEmpty( ))
{
if( waitBegin > -1 )
_waitTime += ( _clock.getTime64() - waitBegin );
return co::CommandQueue::pop( 0 );
}
if( _messagePump )
{
if( waitBegin == -1 )
waitBegin = _clock.getTime64();
_messagePump->dispatchOne( timeout ); // blocks - push sends wakeup
}
else
{
waitBegin = _clock.getTime64();
// blocking
const co::ICommand& command = co::CommandQueue::pop( timeout );
_waitTime += ( _clock.getTime64() - waitBegin );
return command;
}
if( _clock.getTime64() - start > timeout )
return co::ICommand();
}
}
co::ICommands CommandQueue::popAll( const uint32_t timeout )
{
const int64_t start = _clock.getTime64();
int64_t waitBegin = -1;
while( true )
{
if( _messagePump )
_messagePump->dispatchAll(); // non-blocking
// Poll for commands
if( !isEmpty( ))
{
if( waitBegin > -1 )
_waitTime += ( _clock.getTime64() - waitBegin );
return co::CommandQueue::popAll( 0 );
}
if( _messagePump )
{
if( waitBegin == -1 )
waitBegin = _clock.getTime64();
_messagePump->dispatchOne( timeout ); // blocks - push sends wakeup
}
else
{
waitBegin = _clock.getTime64();
// blocking
const co::ICommands& commands = co::CommandQueue::popAll( timeout );
_waitTime += ( _clock.getTime64() - waitBegin );
return commands;
}
if( _clock.getTime64() - start > timeout )
return co::ICommands();
}
}
co::ICommand CommandQueue::tryPop()
{
if( _messagePump )
_messagePump->dispatchAll(); // non-blocking
return co::CommandQueue::tryPop();
}
void CommandQueue::pump()
{
if( _messagePump )
_messagePump->dispatchAll(); // non-blocking
}
}