Example #1
0
void TaskQueue::pollProcessedTasks(long long maxAllowedTimeMilliseconds)
{
  long long startTime = Time::currentTimeMillis();
  TaskUnitQueue processedCopy;
  {
    boost::mutex::scoped_lock l(mProcessedQueueMutex);
    processedCopy = mProcessedTaskUnits;
    if (mProcessedTaskUnits.size()) {
      mProcessedTaskUnits = TaskUnitQueue();
    }
  }
  while (processedCopy.size()) {

    TaskUnit* taskUnit = processedCopy.front();
    processedCopy.pop();
    try {
      taskUnit->executeInMainThread();
    } catch (const std::exception& ex) {
      S_LOG_FAILURE("Error when executing task in main thread." << ex);
    } catch (...) {
      S_LOG_FAILURE("Unknown error when executing task in main thread.");
    }
    try {
      delete taskUnit;
    } catch (const std::exception& ex) {
      S_LOG_FAILURE("Error when deleting task in main thread." << ex);
    } catch (...) {
      S_LOG_FAILURE("Unknown error when deleting task in main thread.");
    }
    //Try to keep the time spent here each frame down, to keep the framerate up.
    if (maxAllowedTimeMilliseconds > 0 && Time::currentTimeMillis() - startTime > maxAllowedTimeMilliseconds) {
      break;
    }
  }
  //If there are any unprocessed tasks, put them back at the front of the queue.
  if (processedCopy.size()) {
    boost::mutex::scoped_lock l(mProcessedQueueMutex);
    TaskUnitQueue queueCopy(mProcessedTaskUnits);
    mProcessedTaskUnits = processedCopy;
    while (queueCopy.size()) {
      mProcessedTaskUnits.push(queueCopy.front());
      queueCopy.pop();
    }
  }
}
Example #2
0
void TaskQueue::pollProcessedTasks(TimeFrame timeFrame)
{
	TaskUnitQueue processedCopy;
	{
		std::unique_lock<std::mutex> l(mProcessedQueueMutex);
		processedCopy = mProcessedTaskUnits;
		if (!mProcessedTaskUnits.empty()) {
			mProcessedTaskUnits = TaskUnitQueue();
		}
	}
	while (!processedCopy.empty()) {

		TaskUnit* taskUnit = processedCopy.front();
		processedCopy.pop();
		try {
			taskUnit->executeInMainThread();
		} catch (const std::exception& ex) {
			S_LOG_FAILURE("Error when executing task in main thread." << ex);
		} catch (...) {
			S_LOG_FAILURE("Unknown error when executing task in main thread.");
		}
		try {
			delete taskUnit;
		} catch (const std::exception& ex) {
			S_LOG_FAILURE("Error when deleting task in main thread." << ex);
		} catch (...) {
			S_LOG_FAILURE("Unknown error when deleting task in main thread.");
		}
		//Try to keep the time spent here each frame down, to keep the framerate up.
		if (!timeFrame.isTimeLeft()) {
			break;
		}
	}
	//If there are any unprocessed tasks, put them back at the front of the queue.
	if (!processedCopy.empty()) {
		std::unique_lock<std::mutex> l(mProcessedQueueMutex);
		TaskUnitQueue queueCopy(mProcessedTaskUnits);
		mProcessedTaskUnits = processedCopy;
		while (!queueCopy.empty()) {
			mProcessedTaskUnits.push(queueCopy.front());
			queueCopy.pop();
		}
	}
}