123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- #include "TickQueue.h"
- #include "Block.h"
- TickQueue::TickQueue()
- : ReferenceCounter()
- {
- maxSize = 0;
- readPosition = 0;
- writePosition = 0;
- queue = 0;
- exit = 0;
- }
- TickQueue::~TickQueue()
- {
- delete[] queue;
- }
- void TickQueue::startNextTick(Framework::Array<Tickable*>* zSources)
- {
- std::unique_lock<std::mutex> lk(mutex);
- readPosition = 0;
- writePosition = 0;
- int count = zSources->getEintragAnzahl();
- if (count >= maxSize)
- {
- Tickable** temp = new Tickable*[count + 1000];
- memcpy(temp, queue, sizeof(Tickable*) * maxSize);
- memset(temp + maxSize, 0, sizeof(Tickable*) * (count + 1000 - maxSize));
- maxSize = count + 1000;
- delete[] queue;
- queue = temp;
- }
- for (Tickable* block : *zSources)
- queue[writePosition++] = block;
- lk.unlock();
- hasBlocks.notify_all();
- }
- void TickQueue::addToQueue(Tickable* zBlock)
- {
- std::unique_lock<std::mutex> lk(mutex);
- if (writePosition >= maxSize)
- {
- Tickable** temp = new Tickable*[maxSize + 1000];
- memcpy(temp, queue, sizeof(Tickable*) * maxSize);
- memset(temp + maxSize, 0, sizeof(Tickable*) * 1000);
- maxSize += 1000;
- delete[] queue;
- queue = temp;
- }
- queue[writePosition++] = zBlock;
- lk.unlock();
- hasBlocks.notify_one();
- }
- Tickable* TickQueue::zNext(bool& waiting)
- {
- std::unique_lock<std::mutex> lk(mutex);
- if (readPosition == writePosition && exit) return 0;
- if (readPosition == writePosition)
- {
- lk.unlock();
- hasNoBlocks.notify_all();
- lk.lock();
- waiting = 1;
- if (exit)
- {
- lk.unlock();
- return 0;
- }
- hasBlocks.wait(
- lk, [this] { return readPosition < writePosition || exit; });
- waiting = 0;
- }
- if (readPosition < writePosition) return queue[readPosition++];
- return 0;
- }
- void TickQueue::requestExit()
- {
- std::unique_lock<std::mutex> lk(mutex);
- exit = 1;
- lk.unlock();
- hasBlocks.notify_all();
- hasNoBlocks.notify_all();
- }
- void TickQueue::waitForEmpty()
- {
- std::unique_lock<std::mutex> lk(mutex);
- if (readPosition != writePosition)
- hasNoBlocks.wait(
- lk, [this] { return readPosition == writePosition || exit; });
- }
- void TickQueue::postTick()
- {
- for (int i = 0; i < writePosition; i++)
- queue[i]->postTick();
- }
|