Ignore:
Timestamp:
Jan 30, 2015, 1:54:37 PM (10 years ago)
Author:
Frederik Heber <heber@…>
Parents:
419fa2
Message:

tempcommit: Trying to make run_thread relaunchable whenever new Action is queued and not running.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Actions/ActionQueue.cpp

    r419fa2 r6bbdfb  
    6767    lastActionOk(true),
    6868    CurrentAction(0),
    69     run_thread_isIdle(true),
    70     run_thread_running(false),
    71     run_thread(boost::bind(&ActionQueue::run, this))
     69    run_thread_isIdle(true)
    7270#endif
    7371{
     
    105103  newaction->prepare(state);
    106104#ifdef HAVE_ACTION_THREAD
     105  {
     106    boost::mutex::scoped_lock trylock(mtx_run_thread_running, boost::try_to_lock);
     107    if (trylock) {
     108      trylock.unlock();
     109      run_thread = boost::thread(&ActionQueue::run, this);
     110    }
     111  }
     112
    107113  mtx_actionqueue.lock();
    108114#endif
     
    144150}
    145151
     152void ActionQueue::setActionQueueDone()
     153{
     154  boost::lock_guard<boost::mutex> lock(mtx_actionqueue);
     155  CurrentAction = actionqueue.size();
     156}
     157
    146158bool ActionQueue::isTempQueueDone() const
    147159{
     
    173185void ActionQueue::run()
    174186{
    175   {
    176     boost::lock_guard<boost::mutex> lock(mtx_run_thread_isIdle);
    177     run_thread_running = true;
    178   }
     187  // only a single thread may run this
     188  boost::mutex::scoped_lock trylock(mtx_run_thread_running, boost::try_to_lock);
     189  if (trylock) {
    179190  bool Interrupted = false;
    180191  do {
     
    207218        lastActionOk = false;
    208219        std::cerr << "Remaining Actions cleared from queue." << std::endl;
    209       } catch (std::exception &e) {
     220      } /* catch (std::exception &e) {
    210221        pushStatus("FAIL: General exception caught, aborting.");
    211222        World::getInstance().setExitFlag(134);
     223        lastActionOk = false;
    212224        std::cerr << "Remaining Actions cleared from queue." << std::endl;
    213       }
    214       // remember action we juse executed
     225      }*/
     226      // remember action we just executed
    215227      const Action *lastaction = actionqueue[CurrentAction];
    216       // step on to next action and check for end
     228      // step on to next action if last ok
    217229      if (lastActionOk)
    218230        CurrentAction++;
     231
     232      mtx_actionqueue.unlock();
     233
     234      // remove following Actions outside mutex if current failed
     235      if (!lastActionOk) {
     236        clearQueue(CurrentAction);  // was not incremented
     237        clearTempQueue();
     238        setActionQueueDone();
     239     }
     240
    219241      // insert new actions (before [CurrentAction]) if they have been spawned
    220242      // we must have an extra vector for this, as we cannot change actionqueue
    221243      // while an action instance is "in-use"
    222       mtx_actionqueue.unlock();
    223 
    224       if (!lastActionOk) {
    225         clearQueue(CurrentAction);
    226         clearTempQueue();
    227       }
    228244
    229245      insertTempQueue();
    230246
    231       // set last action
     247      // set last action when all is done for this Action call
    232248      if (lastActionOk) {
    233249        OBSERVE;
     
    243259    cond_idle.notify_one();
    244260//    LOG(1, "DEBUG: End of ActionQueue's run() loop.");
    245   } while (!Interrupted);
    246   {
    247     boost::lock_guard<boost::mutex> lock(mtx_run_thread_isIdle);
    248     run_thread_running = false;
     261  } while ((!Interrupted) && (!isActionQueueDone()));
     262  trylock.unlock();
    249263  }
    250264}
     
    277291void ActionQueue::wait()
    278292{
    279   if (run_thread_running) {
     293  boost::mutex::scoped_lock trylock(mtx_run_thread_running, boost::try_to_lock);
     294  if (!trylock) {
    280295    boost::unique_lock<boost::mutex> lock(mtx_run_thread_isIdle);
    281296    while(!run_thread_isIdle)
     
    374389    actionqueue.erase(inititer, actionqueue.end());
    375390    LOG(1, "There are " << actionqueue.size() << " remaining Actions.");
    376 #ifdef HAVE_ACTION_THREAD
    377     CurrentAction = actionqueue.size();
    378 #endif
    379391  }
    380392}
Note: See TracChangeset for help on using the changeset viewer.