Line data Source code
1 :
2 : /* Copyright (c) 2007-2013, Stefan Eilemann <eile@equalizergraphics.com>
3 : * 2012, 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 12 : static lunchbox::Clock _clock;
31 : }
32 :
33 50 : CommandQueue::CommandQueue( const size_t maxSize )
34 : : co::CommandQueue( maxSize )
35 : , _messagePump( 0 )
36 50 : , _waitTime( 0 )
37 50 : {}
38 :
39 80 : CommandQueue::~CommandQueue()
40 : {
41 40 : LBASSERT( !_messagePump );
42 40 : delete _messagePump;
43 40 : _messagePump = 0;
44 40 : }
45 :
46 520 : void CommandQueue::push( const co::ICommand& command )
47 : {
48 520 : co::CommandQueue::push( command );
49 520 : if( _messagePump )
50 233 : _messagePump->postWakeup();
51 520 : }
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 169 : co::ICommand CommandQueue::pop( const uint32_t timeout )
61 : {
62 169 : int64_t start = -1;
63 : while( true )
64 : {
65 169 : if( _messagePump )
66 0 : _messagePump->dispatchAll(); // non-blocking
67 :
68 : // Poll for a command
69 169 : if( !isEmpty() || timeout == 0 )
70 : {
71 67 : if( start > -1 )
72 0 : _waitTime += ( _clock.getTime64() - start );
73 67 : return co::CommandQueue::pop( timeout );
74 : }
75 :
76 102 : if( _messagePump )
77 : {
78 0 : if( start == -1 )
79 0 : start = _clock.getTime64();
80 0 : _messagePump->dispatchOne( timeout ); // blocks - push sends wakeup
81 : }
82 : else
83 : {
84 102 : start = _clock.getTime64();
85 : // blocking
86 102 : const co::ICommand& command = co::CommandQueue::pop( timeout );
87 102 : _waitTime += ( _clock.getTime64() - start );
88 102 : return command;
89 : }
90 0 : }
91 : }
92 :
93 105 : co::ICommands CommandQueue::popAll( const uint32_t timeout )
94 : {
95 105 : int64_t start = -1;
96 : while( true )
97 : {
98 2763 : if( _messagePump )
99 2763 : _messagePump->dispatchAll(); // non-blocking
100 :
101 : // Poll for commands
102 2763 : if( !isEmpty() || timeout == 0 )
103 : {
104 105 : if( start > -1 )
105 37 : _waitTime += ( _clock.getTime64() - start );
106 105 : return co::CommandQueue::popAll( 0 );
107 : }
108 :
109 2658 : if( _messagePump )
110 : {
111 2658 : if( start == -1 )
112 37 : start = _clock.getTime64();
113 2658 : _messagePump->dispatchOne( timeout ); // blocks - push send swakeup
114 : }
115 : else
116 : {
117 0 : start = _clock.getTime64();
118 : // blocking
119 0 : const co::ICommands& commands = co::CommandQueue::popAll( timeout );
120 0 : _waitTime += ( _clock.getTime64() - start );
121 0 : return commands;
122 : }
123 2658 : }
124 : }
125 :
126 4667 : co::ICommand CommandQueue::tryPop()
127 : {
128 4667 : if( _messagePump )
129 0 : _messagePump->dispatchAll(); // non-blocking
130 :
131 4667 : return co::CommandQueue::tryPop();
132 : }
133 :
134 248 : void CommandQueue::pump()
135 : {
136 248 : if( _messagePump )
137 248 : _messagePump->dispatchAll(); // non-blocking
138 248 : }
139 :
140 36 : }
|