void ThreadManager::taskExecutor(int id)
{
    // pick a task and pop it from the list
    // if we have some other tasks pending, wake the next thread to handle it
    // execute the task
    // wait for notification

    TaskExecutor& t = m_taskExecutor[id];
    TaskExecutor& tNext = m_taskExecutor[(id + 1) % 4];

    while (!t.m_stop)
    {
        std::unique_lock<std::mutex> execLock(t.m_lock);
        t.m_cond.wait(execLock);
        std::unique_lock<std::mutex> lock(m_tasksLock);
        if (m_tasks.empty())
            continue;
        std::function<void()> taskFunc = m_tasks.back();
        m_tasks.pop_back();
        if (!m_tasks.empty())
        {
            // notify another thread as we still have tasks pending
            tNext.m_cond.notify_one();
        }
        lock.unlock();
        taskFunc();
    }
}
Beispiel #2
0
KILLENUM Runtime::executeCrunch()
{
  accept( Token::CRUNCH );
  Token::Code code = tokenizer.token();

  zdebug() << __FILE__ << ":" << __LINE__ << code << tokenizer.string();

  KILLENUM ret = PROCEED;
  switch ( code ) {
    case Token::BECOME: ret = execBecome(); break;
    case Token::BIND: ret = execBind(); break;
    case Token::CHANGE: ret = execChange(); break;
    case Token::CHAR: ret = execChar(); break;
    case Token::CLEAR: ret = execClear(); break;
    case Token::CYCLE: ret = execCycle(); break;
    case Token::DIE: ret = execDie(); break;
    case Token::END: ret = execEnd(); break;
    case Token::ENDGAME: ret = execEndgame(); break;
    case Token::GIVE: ret = execGive(); break;
    case Token::GO: ret = execGo(); break;
    case Token::IDLE: ret = execIdle(); break;
    case Token::IF: ret = execIf(); break;
    case Token::LOCK: ret = execLock(); break;
    case Token::PLAY: ret = execPlay(); break;
    case Token::PUT: ret = execPut(); break;
    case Token::RESTART: ret = execRestart(); break;
    case Token::RESTORE: ret = execRestore(); break;
    case Token::SEND: ret = execSend(); break;
    case Token::SET: ret = execSet(); break;
    case Token::SHOOT: ret = execShoot(); break;
    case Token::TAKE: ret = execTake(); break;
    case Token::THROWSTAR: ret = execThrowstar(); break;
    case Token::TRY: ret = execTry(); break;
    case Token::UNLOCK: ret = execUnlock(); break;
    case Token::WALK: ret = execWalk(); break;
    case Token::ZAP: ret = execZap(); break;
    case Token::ENDOFLINE: ret = throwError("COMMAND ERROR");
    default: {
      sendMessage( tokenizer.string() );
      break;
    }
  }

  return ret;
}