Make the workqueue slightly safer. Put g_gameInfoCache on the heap.

Cherry-picked from #8592
This commit is contained in:
Henrik Rydgard committed 2016-02-28 12:39:47 +01:00
1 parent 5f761c346b
commit da1a04be8d
18 files changed
+73 -64

No files matched your search

+6 -3
View File
@@ -23,13 +23,14 @@ void PrioritizedWorkQueue::Stop() {
}
void PrioritizedWorkQueue::Flush() {
if (queue_.empty())
return;
lock_guard guard(mutex_);
int flush_count = 0;
for (auto iter = queue_.begin(); iter != queue_.end(); ++iter) {
delete *iter;
flush_count++;
}
queue_.clear();
ILOG("Flushed %d un-executed tasks", flush_count);
}
void PrioritizedWorkQueue::WaitUntilDone() {
@@ -40,7 +41,7 @@ void PrioritizedWorkQueue::WaitUntilDone() {
bool empty;
{
lock_guard guard(mutex_);
empty = queue_.empty();
empty = queue_.empty() && !working_;
}
if (empty) {
break;
@@ -53,6 +54,7 @@ void PrioritizedWorkQueue::WaitUntilDone() {
// The worker should simply call this in a loop. Will block when appropriate.
PrioritizedWorkQueueItem *PrioritizedWorkQueue::Pop() {
lock_guard guard(mutex_);
working_ = false; // The thread only calls Pop if it's done.
if (done_) {
return 0;
}
@@ -77,6 +79,7 @@ PrioritizedWorkQueueItem *PrioritizedWorkQueue::Pop() {
if (best != queue_.end()) {
PrioritizedWorkQueueItem *poppedItem = *best;
queue_.erase(best);
working_ = true; // This will be worked on.
return poppedItem;
} else {
// Not really sure how this can happen, but let's be safe.