Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Actions/ActionQueue.cpp

    rcfb9c5 r601ef8  
    4444#include <boost/date_time/posix_time/posix_time.hpp>
    4545#include <boost/version.hpp>
     46#include <iterator>
    4647#include <string>
    4748#include <sstream>
     
    6162    AR(new ActionRegistry()),
    6263    history(new ActionHistory),
    63     CurrentAction(0),
    6464#ifndef HAVE_ACTION_THREAD
    6565    lastActionOk(true)
    6666#else
     67    CurrentAction(0),
    6768    lastActionOk(true),
    6869    run_thread(boost::bind(&ActionQueue::run, this)),
     
    8283#ifdef HAVE_ACTION_THREAD
    8384  stop();
     85
     86  clearTempQueue();
    8487#endif
    8588
     
    111114    std::cerr << "Action " << *boost::get_error_info<ActionNameString>(e) << " has failed." << std::endl;
    112115    World::getInstance().setExitFlag(5);
    113     clearQueue();
     116    clearQueue(actionqueue.size()-1);
    114117    lastActionOk = false;
    115     std::cerr << "ActionQueue cleared." << std::endl;
     118    std::cerr << "Remaining Actions cleared from queue." << std::endl;
    116119  } catch (std::exception &e) {
    117120    pushStatus("FAIL: General exception caught, aborting.");
    118121    World::getInstance().setExitFlag(134);
    119     clearQueue();
     122    clearQueue(actionqueue.size()-1);
    120123    lastActionOk = false;
    121     std::cerr << "ActionQueue cleared." << std::endl;
     124    std::cerr << "Remaining Actions cleared from queue." << std::endl;
    122125  }
    123126  if (lastActionOk) {
     
    127130  }
    128131#else
    129   {
    130     boost::lock_guard<boost::mutex> lock(mtx_idle);
    131     run_thread_isIdle = (CurrentAction == actionqueue.size());
    132   }
     132  setRunThreadIdle(CurrentAction == actionqueue.size());
    133133  mtx_queue.unlock();
    134134#endif
     
    144144  mtx_queue.lock();
    145145  tempqueue.push_back( newaction );
    146   {
    147     boost::lock_guard<boost::mutex> lock(mtx_idle);
    148     run_thread_isIdle = !((CurrentAction != actionqueue.size()) || !tempqueue.empty());
    149   }
     146  setRunThreadIdle( !((CurrentAction != actionqueue.size()) || !tempqueue.empty()) );
    150147  mtx_queue.unlock();
    151148#endif
     
    184181        pushStatus("FAIL: Action "+*boost::get_error_info<ActionNameString>(e)+" has failed.");
    185182        World::getInstance().setExitFlag(5);
    186         clearQueue();
     183        clearQueue(CurrentAction);
     184        clearTempQueue();
    187185        lastActionOk = false;
    188         std::cerr << "ActionQueue cleared." << std::endl;
    189         CurrentAction = (size_t)-1;
     186        std::cerr << "Remaining Actions cleared from queue." << std::endl;
    190187      } catch (std::exception &e) {
    191188        pushStatus("FAIL: General exception caught, aborting.");
    192189        World::getInstance().setExitFlag(134);
    193         clearQueue();
    194         std::cerr << "ActionQueue cleared." << std::endl;
    195         CurrentAction = (size_t)-1;
     190        clearQueue(CurrentAction);
     191        clearTempQueue();
     192        std::cerr << "Remaining Actions cleared from queue." << std::endl;
    196193      }
    197194      if (lastActionOk) {
     
    199196        NOTIFY(ActionQueued);
    200197        _lastchangedaction = actionqueue[CurrentAction];
     198        mtx_queue.lock();
     199        CurrentAction++;
     200        mtx_queue.unlock();
    201201      }
    202202      // access actionqueue, hence using mutex
    203203      mtx_queue.lock();
    204       // step on to next action and check for end
    205       CurrentAction++;
    206204      // insert new actions (before [CurrentAction]) if they have been spawned
    207205      // we must have an extra vector for this, as we cannot change actionqueue
     
    211209      mtx_queue.unlock();
    212210    }
    213     {
    214       boost::lock_guard<boost::mutex> lock(mtx_idle);
    215       run_thread_isIdle = !((CurrentAction != actionqueue.size()) || !tempqueue.empty());
    216     }
     211    setRunThreadIdle( !((CurrentAction != actionqueue.size()) || !tempqueue.empty()) );
    217212    cond_idle.notify_one();
    218213//    LOG(1, "DEBUG: End of ActionQueue's run() loop.");
    219214  } while (!Interrupted);
    220215}
    221 #endif
    222216
    223217void ActionQueue::insertTempQueue()
     
    231225}
    232226
    233 #ifdef HAVE_ACTION_THREAD
    234227void ActionQueue::wait()
    235228{
     
    316309}
    317310
    318 void ActionQueue::clearQueue()
    319 {
    320   // free all actions contained in actionqueue
    321   for (ActionQueue_t::iterator iter = actionqueue.begin();
    322       !actionqueue.empty(); iter = actionqueue.begin()) {
     311void ActionQueue::clearQueue(const size_t _fromAction)
     312{
     313#ifdef HAVE_ACTION_THREAD
     314  mtx_queue.lock();
     315#endif
     316  LOG(1, "Removing all Actions from position " << _fromAction << " onward.");
     317  // free all actions still to be called contained in actionqueue
     318  ActionQueue_t::iterator inititer = actionqueue.begin();
     319  std::advance(inititer, _fromAction);
     320  for (ActionQueue_t::iterator iter = inititer; iter != actionqueue.end(); ++iter)
    323321    delete *iter;
    324     actionqueue.erase(iter);
    325   }
     322  actionqueue.erase(inititer, actionqueue.end());
     323  LOG(1, "There are " << actionqueue.size() << " remaining Actions.");
     324#ifdef HAVE_ACTION_THREAD
     325  CurrentAction = actionqueue.size();
     326  mtx_queue.unlock();
     327#endif
     328}
     329
     330#ifdef HAVE_ACTION_THREAD
     331void ActionQueue::clearTempQueue()
     332{
    326333  // free all actions contained in tempqueue
    327334  for (ActionQueue_t::iterator iter = tempqueue.begin();
     
    330337    tempqueue.erase(iter);
    331338  }
    332 #ifdef HAVE_ACTION_THREAD
     339}
     340
     341void ActionQueue::setRunThreadIdle(const bool _flag)
     342{
    333343  {
    334344    boost::unique_lock<boost::mutex> lock(mtx_idle);
    335     run_thread_isIdle = true;
    336   }
    337 #endif
    338 }
     345    run_thread_isIdle = _flag;
     346  }
     347}
     348#endif
    339349
    340350const ActionQueue::ActionTokens_t ActionQueue::getListOfActions() const
     
    355365}
    356366
     367bool ActionQueue::canUndo() const
     368{
     369  return history->hasUndo();
     370}
     371
    357372void ActionQueue::redoLast()
    358373{
     
    360375}
    361376
     377bool ActionQueue::canRedo() const
     378{
     379  return history->hasRedo();
     380}
     381
    362382
    363383CONSTRUCT_SINGLETON(ActionQueue)
Note: See TracChangeset for help on using the changeset viewer.