Line data Source code
1 :
2 : /* Copyright (c) 2005-2013, Stefan Eilemann <eile@equalizergraphics.com>
3 : *
4 : * This file is part of Collage <https://github.com/Eyescale/Collage>
5 : *
6 : * This library is free software; you can redistribute it and/or modify it under
7 : * the terms of the GNU Lesser General Public License version 2.1 as published
8 : * by the Free Software Foundation.
9 : *
10 : * This library is distributed in the hope that it will be useful, but WITHOUT
11 : * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 : * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 : * details.
14 : *
15 : * You should have received a copy of the GNU Lesser General Public License
16 : * along with this library; if not, write to the Free Software Foundation, Inc.,
17 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 : */
19 :
20 : #include "commandQueue.h"
21 :
22 : #include "iCommand.h"
23 : #include "exception.h"
24 : #include "node.h"
25 :
26 : #include <lunchbox/mtQueue.h>
27 :
28 : namespace co
29 : {
30 : namespace detail
31 : {
32 64 : class CommandQueue
33 : {
34 : public:
35 66 : CommandQueue( const size_t maxSize ) : commands( maxSize ) {}
36 :
37 : /** Thread-safe buffer queue. */
38 : lunchbox::MTQueue< co::ICommand > commands;
39 : };
40 : }
41 :
42 66 : CommandQueue::CommandQueue( const size_t maxSize )
43 66 : : _impl( new detail::CommandQueue( maxSize ))
44 : {
45 66 : }
46 :
47 128 : CommandQueue::~CommandQueue()
48 : {
49 64 : flush();
50 64 : delete _impl;
51 64 : }
52 :
53 109 : void CommandQueue::flush()
54 : {
55 109 : if( !isEmpty( ))
56 0 : LBWARN << "Flushing non-empty command queue" << std::endl;
57 :
58 109 : _impl->commands.clear();
59 109 : }
60 :
61 51729 : bool CommandQueue::isEmpty() const
62 : {
63 51729 : return _impl->commands.isEmpty();
64 : }
65 :
66 5 : size_t CommandQueue::getSize() const
67 : {
68 5 : return _impl->commands.getSize();
69 : }
70 :
71 1114769 : void CommandQueue::push( const ICommand& command )
72 : {
73 1114769 : _impl->commands.push( command );
74 1114769 : }
75 :
76 0 : void CommandQueue::pushFront( const ICommand& command )
77 : {
78 0 : LBASSERT( command.isValid( ));
79 0 : _impl->commands.pushFront( command );
80 0 : }
81 :
82 1063059 : ICommand CommandQueue::pop( const uint32_t timeout )
83 : {
84 1063059 : LB_TS_THREAD( _thread );
85 :
86 1063012 : ICommand command;
87 1062908 : if( !_impl->commands.timedPop( timeout, command ))
88 0 : throw Exception( Exception::TIMEOUT_COMMANDQUEUE );
89 :
90 1063083 : return command;
91 : }
92 :
93 51607 : ICommands CommandQueue::popAll( const uint32_t timeout )
94 : {
95 51607 : const ICommands& result = _impl->commands.timedPopRange( timeout );
96 :
97 51608 : if( result.empty( ))
98 0 : throw Exception( Exception::TIMEOUT_COMMANDQUEUE );
99 51606 : return result;
100 : }
101 :
102 2 : ICommand CommandQueue::tryPop()
103 : {
104 2 : LB_TS_THREAD( _thread );
105 2 : ICommand command;
106 2 : _impl->commands.tryPop( command );
107 2 : return command;
108 : }
109 :
110 60 : }
|