Exemple #1
0
    void ThreadPool::executeOneTaskIfPossible_CRITICAL_SECTION()
    {
      if ( m_tasks.empty() && m_idleTasks.empty() && (!m_isMainThread.get() || m_mainThreadTasks.empty()) )
        m_stateCond.wait( m_stateMutex );
      else
      {
        std::vector<Task *> *taskQueue;
        if ( m_isMainThread.get() && !m_mainThreadTasks.empty() )
          taskQueue = &m_mainThreadTasks;
        else if ( !m_tasks.empty() )
          taskQueue = &m_tasks;
        else taskQueue = &m_idleTasks;
        
        Task *task = taskQueue->back();
        
        size_t index;
        bool keep;
        task->preExecute_CRITICAL_SECTION( index, keep );
        if ( !keep )
          taskQueue->pop_back();

        m_stateMutex.release();
        try
        {
          task->execute( index );
        }
        catch ( Exception e )
        {
          Util::SimpleString prefixedException = "Exception: " + e.getDesc();
          RC::Handle<LogCollector> logCollector = task->getLogCollector();
          if ( logCollector )
            logCollector->add( prefixedException.data(), prefixedException.length() );
        }
        catch ( ... )
        {
          static Util::SimpleString const genericException = "Exception (unknown)";
          RC::Handle<LogCollector> logCollector = task->getLogCollector();
          if ( logCollector )
            logCollector->add( genericException.data(), genericException.length() );
        }
        m_stateMutex.acquire();
        
        task->postExecute_CRITICAL_SECTION();
        if ( task->completed_CRITICAL_SECTION() )
        {
          void (*finishedCallback)( void * ) = task->getFinishedCallback();
          if ( finishedCallback )
            finishedCallback( task->getFinishedUserdata() );

          task->dispose();
          
          // [pzion 20101108] Must wake waiter because they might be
          // waiting on the task completion
          m_stateCond.broadcast();
        }
      }
    }