]> git.tdb.fi Git - r2c2.git/commitdiff
Use a common implementation for the three different queues
authorMikko Rasa <tdb@tdb.fi>
Fri, 15 Nov 2013 23:26:27 +0000 (01:26 +0200)
committerMikko Rasa <tdb@tdb.fi>
Fri, 15 Nov 2013 23:26:27 +0000 (01:26 +0200)
source/libr2c2/arducontrol.cpp
source/libr2c2/arducontrol.h

index 921526341b1c320de0928c847a88cbb7ff154343..e29614f6646a5c5fd3068d819c7e89c071a0c154 100644 (file)
@@ -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<typename T>
+void ArduControl::Queue<T>::push(const T &item)
+{
+       MutexLock lock(mutex);
+       items.push_back(item);
+}
+
+template<typename T>
+bool ArduControl::Queue<T>::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<cmd.repeat_count); ++i)
                                success = (do_command(cmd)==COMMAND_OK);
                        if(success && cmd.tag)
-                               control.push_completed_tag(cmd.tag);
+                               control.completed_commands.push(cmd.tag);
                }
                else
                        Time::sleep(10*Time::msec);
@@ -971,7 +949,7 @@ void ArduControl::ControlThread::init_baud_rate()
 
 bool ArduControl::ControlThread::get_work(PendingCommand &cmd)
 {
-       if(control.pop_command(cmd))
+       if(control.command_queue.pop(cmd))
                return true;
 
        for(vector<Task *>::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
        {
index 69311f8280fecbb94ce0671b636a0ec4bf9fcf0d..c9a88aa56f4c30f068bda6fc40c9f673337d2284 100644 (file)
@@ -188,6 +188,18 @@ private:
                PendingCommand(Accessory &, Accessory::Command, unsigned = 0);
        };
 
+       template<typename T>
+       class Queue
+       {
+       private:
+               std::list<T> 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<MfxInfo> queue;
-               Msp::Mutex mutex;
+               Queue<MfxInfo> 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<PendingCommand> command_queue;
-       std::list<Tag> completed_commands;
 
        SensorMap sensors;
 
        Msp::Mutex mutex;
+       Queue<PendingCommand> command_queue;
+       Queue<Tag> 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