00001
00002
00003
00004 #include <pthread.h>
00005 #include "commandQueue.h"
00006
00007 #include "command.h"
00008 #include "node.h"
00009 #include "packets.h"
00010
00011 using namespace std;
00012
00013 namespace eq
00014 {
00015 namespace net
00016 {
00017 CommandQueue::CommandQueue()
00018 {
00019 }
00020
00021 CommandQueue::~CommandQueue()
00022 {
00023 flush();
00024 }
00025
00026 void CommandQueue::flush()
00027 {
00028 _commandCacheLock.set();
00029
00030 if( !empty( ))
00031 EQWARN << "Flushing non-empty command queue" << endl;
00032
00033 while( !_commands.empty( ))
00034 {
00035 Command* command = _commands.pop();
00036 EQWARN << *command << endl;
00037 if( command )
00038 _commandCache.release( command );
00039 }
00040
00041 _commandCache.flush();
00042 _commandCacheLock.unset();
00043 }
00044
00045 void CommandQueue::push( Command& inCommand )
00046 {
00047 EQASSERT( inCommand.isValid( ));
00048
00049 _commandCacheLock.set();
00050 Command* outCommand = _commandCache.alloc( inCommand );
00051 _commandCacheLock.unset();
00052
00053 EQASSERT( outCommand->isValid( ));
00054 outCommand->_dispatched = true;
00055 _commands.push( outCommand );
00056 }
00057
00058 void CommandQueue::pushFront( Command& inCommand )
00059 {
00060 EQASSERT( inCommand.isValid( ));
00061
00062 _commandCacheLock.set();
00063 Command* outCommand = _commandCache.alloc( inCommand );
00064 _commandCacheLock.unset();
00065
00066 EQASSERT( outCommand->isValid( ));
00067 outCommand->_dispatched = true;
00068 _commands.pushFront( outCommand );
00069 }
00070
00071 Command* CommandQueue::pop()
00072 {
00073 CHECK_THREAD( _thread );
00074
00075 return _commands.pop();
00076 }
00077
00078 Command* CommandQueue::tryPop()
00079 {
00080 CHECK_THREAD( _thread );
00081 return _commands.tryPop();
00082 }
00083
00084 void CommandQueue::release( Command* command )
00085 {
00086 if( !command )
00087 return;
00088
00089 _commandCacheLock.set();
00090 _commandCache.release( command );
00091 _commandCacheLock.unset();
00092 }
00093
00094 Command* CommandQueue::back() const
00095 {
00096 return _commands.back();
00097 }
00098 }
00099 }
00100