From: Mikko Rasa Date: Fri, 15 Nov 2013 23:26:27 +0000 (+0200) Subject: Use a common implementation for the three different queues X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=59b3cebecfd8f1462c95bab447be70dfb5f38e92;p=r2c2.git Use a common implementation for the three different queues --- diff --git a/source/libr2c2/arducontrol.cpp b/source/libr2c2/arducontrol.cpp index 9215263..e29614f 100644 --- a/source/libr2c2/arducontrol.cpp +++ b/source/libr2c2/arducontrol.cpp @@ -27,7 +27,7 @@ ArduControl::ArduControl(const string &dev): PendingCommand cmd; cmd.command[0] = READ_POWER_STATE; cmd.length = 1; - push_command(cmd); + command_queue.push(cmd); cmd.command[0] = MFX_SET_STATION_ID; cmd.command[1] = 'R'; @@ -35,7 +35,7 @@ ArduControl::ArduControl(const string &dev): cmd.command[3] = 'C'; cmd.command[4] = '2'; cmd.length = 5; - push_command(cmd); + command_queue.push(cmd); } ArduControl::~ArduControl() @@ -51,7 +51,7 @@ void ArduControl::set_power(bool p) cmd.tag.serial = power.serial; cmd.command[0] = (p ? POWER_ON : POWER_OFF); cmd.length = 1; - push_command(cmd); + command_queue.push(cmd); } } @@ -123,7 +123,7 @@ void ArduControl::set_loco_speed(unsigned id, unsigned speed) if(loco.speed.set(speed)) { PendingCommand cmd(loco, Locomotive::SPEED); - push_command(cmd); + command_queue.push(cmd); refresh.add_loco(loco); } @@ -135,7 +135,7 @@ void ArduControl::set_loco_reverse(unsigned id, bool rev) if(loco.reverse.set(rev)) { PendingCommand cmd(loco, Locomotive::REVERSE); - push_command(cmd); + command_queue.push(cmd); refresh.add_loco(loco); } @@ -153,7 +153,7 @@ void ArduControl::set_loco_function(unsigned id, unsigned func, bool state) if(func>0 || loco.proto!=MM) { PendingCommand cmd(loco, Locomotive::FUNCTIONS, func); - push_command(cmd); + command_queue.push(cmd); } refresh.add_loco(loco); @@ -273,7 +273,8 @@ bool ArduControl::get_sensor(unsigned addr) const void ArduControl::tick() { - while(Tag tag = pop_completed_tag()) + Tag tag; + while(completed_commands.pop(tag)) { if(tag.type==Tag::GENERAL) { @@ -385,7 +386,7 @@ void ArduControl::tick() for(i=0; (lowest_bit>>i)>1; ++i) ; acc.state.set(acc.state^lowest_bit); PendingCommand cmd(acc, Accessory::ACTIVATE, i); - push_command(cmd); + command_queue.push(cmd); } else accessory_queue.pop_front(); @@ -398,7 +399,7 @@ void ArduControl::tick() { off_timeout = Time::TimeStamp(); PendingCommand cmd(*active_accessory, Accessory::DEACTIVATE); - push_command(cmd); + command_queue.push(cmd); } } } @@ -407,38 +408,6 @@ void ArduControl::flush() { } -void ArduControl::push_command(const PendingCommand &cmd) -{ - MutexLock lock(mutex); - command_queue.push_back(cmd); -} - -bool ArduControl::pop_command(PendingCommand &cmd) -{ - MutexLock lock(mutex); - if(command_queue.empty()) - return false; - cmd = command_queue.front(); - command_queue.pop_front(); - return true; -} - -void ArduControl::push_completed_tag(const Tag &tag) -{ - MutexLock lock(mutex); - completed_commands.push_back(tag); -} - -ArduControl::Tag ArduControl::pop_completed_tag() -{ - MutexLock lock(mutex); - if(completed_commands.empty()) - return Tag(); - Tag tag = completed_commands.front(); - completed_commands.pop_front(); - return tag; -} - ArduControl::Tag::Tag(): type(NONE), @@ -600,6 +569,26 @@ ArduControl::PendingCommand::PendingCommand(Accessory &acc, Accessory::Command c } +template +void ArduControl::Queue::push(const T &item) +{ + MutexLock lock(mutex); + items.push_back(item); +} + +template +bool ArduControl::Queue::pop(T &item) +{ + MutexLock lock(mutex); + if(items.empty()) + return false; + + item = items.front(); + items.pop_front(); + return true; +} + + ArduControl::RefreshTask::RefreshTask(): next(cycle.end()), round(0), @@ -733,7 +722,7 @@ void ArduControl::S88Task::process_reply(const char *reply, unsigned length) tag.command = Sensor::STATE; tag.serial = i->second.state.serial; tag.id = i->first; - control.push_completed_tag(tag); + control.completed_commands.push(tag); } if(count>octets_remaining) @@ -800,7 +789,7 @@ bool ArduControl::MfxSearchTask::get_work(PendingCommand &cmd) info.address = next_address; info.name = format("%08X", bits); info.id = bits; - push_info(info); + queue.push(info); cmd.command[0] = MFX_ASSIGN_ADDRESS; cmd.command[1] = next_address>>8; @@ -863,20 +852,9 @@ void ArduControl::MfxSearchTask::process_reply(const char *reply, unsigned lengt } } -void ArduControl::MfxSearchTask::push_info(const MfxInfo &info) -{ - MutexLock lock(mutex); - queue.push_back(info); -} - bool ArduControl::MfxSearchTask::pop_info(MfxInfo &info) { - MutexLock lock(mutex); - if(queue.empty()) - return false; - info = queue.back(); - queue.pop_back(); - return true; + return queue.pop(info); } @@ -911,7 +889,7 @@ void ArduControl::ControlThread::main() for(unsigned i=0; (success && i::iterator i=tasks.begin(); i!=tasks.end(); ++i) @@ -1058,7 +1036,7 @@ unsigned ArduControl::ControlThread::process_reply(const char *reply, unsigned r tag.type = Tag::GENERAL; tag.command = POWER; tag.serial = control.power.serial; - control.push_completed_tag(tag); + control.completed_commands.push(tag); } else { diff --git a/source/libr2c2/arducontrol.h b/source/libr2c2/arducontrol.h index 69311f8..c9a88aa 100644 --- a/source/libr2c2/arducontrol.h +++ b/source/libr2c2/arducontrol.h @@ -188,6 +188,18 @@ private: PendingCommand(Accessory &, Accessory::Command, unsigned = 0); }; + template + class Queue + { + private: + std::list items; + Msp::Mutex mutex; + + public: + void push(const T &); + bool pop(T &); + }; + class Task { protected: @@ -263,8 +275,7 @@ private: unsigned size; unsigned bits; unsigned misses; - std::list queue; - Msp::Mutex mutex; + Queue queue; public: MfxSearchTask(ArduControl &); @@ -272,9 +283,6 @@ private: virtual bool get_work(PendingCommand &); virtual void process_reply(const char *, unsigned); - private: - void push_info(const MfxInfo &); - public: bool pop_info(MfxInfo &); }; @@ -314,12 +322,12 @@ private: AccessoryPtrList accessory_queue; Accessory *active_accessory; Msp::Time::TimeStamp off_timeout; - std::list command_queue; - std::list completed_commands; SensorMap sensors; Msp::Mutex mutex; + Queue command_queue; + Queue completed_commands; RefreshTask refresh; S88Task s88; MfxAnnounceTask mfx_announce; @@ -374,12 +382,6 @@ public: virtual void tick(); virtual void flush(); - -private: - void push_command(const PendingCommand &); - bool pop_command(PendingCommand &); - void push_completed_tag(const Tag &); - Tag pop_completed_tag(); }; } // namespace R2C2