TickOrganizer.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include "TickOrganizer.h"
  2. #include "Constants.h"
  3. TickOrganizer::TickOrganizer()
  4. : ReferenceCounter()
  5. {
  6. queue = new TickQueue();
  7. workerCount = NUM_TICK_WORKERS;
  8. workers = new TickWorker*[workerCount];
  9. for (int i = 0; i < workerCount; i++)
  10. workers[i] = new TickWorker(queue);
  11. }
  12. TickOrganizer::~TickOrganizer()
  13. {
  14. queue->release();
  15. delete[] workers;
  16. }
  17. void TickOrganizer::nextTick()
  18. {
  19. sourceCs.lock();
  20. queue->startNextTick(&tickSources);
  21. sourceCs.unlock();
  22. bool notWaiting = 0;
  23. do
  24. {
  25. queue->waitForEmpty();
  26. notWaiting = 0;
  27. for (int i = 0; i < workerCount; i++)
  28. notWaiting |= !workers[i]->isWaiting();
  29. } while (notWaiting);
  30. queue->postTick();
  31. }
  32. void TickOrganizer::addTickSource(Tickable* zObj)
  33. {
  34. sourceCs.lock();
  35. tickSources.add(zObj);
  36. sourceCs.unlock();
  37. }
  38. void TickOrganizer::removeTickSource(Tickable* zObj)
  39. {
  40. sourceCs.lock();
  41. for (Framework::ArrayIterator<Tickable*> obj = tickSources.begin(); obj; obj++)
  42. {
  43. if (obj.val() == zObj)
  44. {
  45. obj.remove();
  46. break;
  47. }
  48. }
  49. sourceCs.unlock();
  50. }
  51. void TickOrganizer::exitAndWait()
  52. {
  53. queue->requestExit();
  54. for (int i = 0; i < workerCount; i++)
  55. {
  56. workers[i]->warteAufThread(1000000);
  57. workers[i]->ende();
  58. workers[i]->release();
  59. }
  60. }