Line data Source code
1 :
2 : /* Copyright (c) 2007-2015, Stefan Eilemann <eile@equalizergraphics.com>
3 : * Daniel Nachbaur <danielnachbaur@gmail.com>
4 : *
5 : * This library is free software; you can redistribute it and/or modify it under
6 : * the terms of the GNU Lesser General Public License version 2.1 as published
7 : * by the Free Software Foundation.
8 : *
9 : * This library is distributed in the hope that it will be useful, but WITHOUT
10 : * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 : * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12 : * details.
13 : *
14 : * You should have received a copy of the GNU Lesser General Public License
15 : * along with this library; if not, write to the Free Software Foundation, Inc.,
16 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 : */
18 :
19 : #include "commandQueue.h"
20 :
21 : #include "messagePump.h"
22 :
23 : #include <co/iCommand.h>
24 : #include <lunchbox/clock.h>
25 :
26 : namespace eq
27 : {
28 : namespace
29 : {
30 14 : static lunchbox::Clock _clock;
31 : }
32 :
33 25 : CommandQueue::CommandQueue( const size_t maxSize )
34 : : co::CommandQueue( maxSize )
35 : , _messagePump( 0 )
36 25 : , _waitTime( 0 )
37 25 : {}
38 :
39 30 : CommandQueue::~CommandQueue()
40 : {
41 15 : LBASSERT( !_messagePump );
42 15 : delete _messagePump;
43 15 : _messagePump = 0;
44 15 : }
45 :
46 64 : void CommandQueue::push( const co::ICommand& command )
47 : {
48 64 : co::CommandQueue::push( command );
49 64 : if( _messagePump )
50 9 : _messagePump->postWakeup();
51 64 : }
52 :
53 0 : void CommandQueue::pushFront( const co::ICommand& command )
54 : {
55 0 : co::CommandQueue::pushFront( command );
56 0 : if( _messagePump )
57 0 : _messagePump->postWakeup();
58 0 : }
59 :
60 37 : co::ICommand CommandQueue::pop( const uint32_t timeout )
61 : {
62 37 : const int64_t start = _clock.getTime64();
63 37 : int64_t waitBegin = -1;
64 : while( true )
65 : {
66 37 : if( _messagePump )
67 0 : _messagePump->dispatchAll(); // non-blocking
68 :
69 : // Poll for a command
70 37 : if( !isEmpty( ))
71 : {
72 4 : if( waitBegin > -1 )
73 0 : _waitTime += ( _clock.getTime64() - waitBegin );
74 4 : return co::CommandQueue::pop( 0 );
75 : }
76 :
77 33 : if( _messagePump )
78 : {
79 0 : if( waitBegin == -1 )
80 0 : waitBegin = _clock.getTime64();
81 0 : _messagePump->dispatchOne( timeout ); // blocks - push sends wakeup
82 : }
83 : else
84 : {
85 33 : waitBegin = _clock.getTime64();
86 : // blocking
87 33 : const co::ICommand& command = co::CommandQueue::pop( timeout );
88 33 : _waitTime += ( _clock.getTime64() - waitBegin );
89 33 : return command;
90 : }
91 :
92 0 : if( _clock.getTime64() - start > timeout )
93 0 : return co::ICommand();
94 0 : }
95 : }
96 :
97 8 : co::ICommands CommandQueue::popAll( const uint32_t timeout )
98 : {
99 8 : const int64_t start = _clock.getTime64();
100 8 : int64_t waitBegin = -1;
101 : while( true )
102 : {
103 10 : if( _messagePump )
104 10 : _messagePump->dispatchAll(); // non-blocking
105 :
106 : // Poll for commands
107 10 : if( !isEmpty( ))
108 : {
109 8 : if( waitBegin > -1 )
110 1 : _waitTime += ( _clock.getTime64() - waitBegin );
111 8 : return co::CommandQueue::popAll( 0 );
112 : }
113 :
114 2 : if( _messagePump )
115 : {
116 2 : if( waitBegin == -1 )
117 1 : waitBegin = _clock.getTime64();
118 2 : _messagePump->dispatchOne( timeout ); // blocks - push sends wakeup
119 : }
120 : else
121 : {
122 0 : waitBegin = _clock.getTime64();
123 : // blocking
124 0 : const co::ICommands& commands = co::CommandQueue::popAll( timeout );
125 0 : _waitTime += ( _clock.getTime64() - waitBegin );
126 0 : return commands;
127 : }
128 :
129 2 : if( _clock.getTime64() - start > timeout )
130 0 : return co::ICommands();
131 2 : }
132 : }
133 :
134 5 : co::ICommand CommandQueue::tryPop()
135 : {
136 5 : if( _messagePump )
137 0 : _messagePump->dispatchAll(); // non-blocking
138 :
139 5 : return co::CommandQueue::tryPop();
140 : }
141 :
142 10 : void CommandQueue::pump()
143 : {
144 10 : if( _messagePump )
145 10 : _messagePump->dispatchAll(); // non-blocking
146 10 : }
147 :
148 42 : }
|