Switch base::TaskQueue to Qt sync primitives.

std::mutex in the current build environment is not supported
by Windows XP, I guess because Ninja uses v140 platform toolset.
This commit is contained in:
John Preston 2017-05-11 17:21:43 +03:00
parent c438692d49
commit 0557eeddbd
2 changed files with 12 additions and 13 deletions

View File

@ -71,12 +71,12 @@ private:
void ThreadFunction(); void ThreadFunction();
std::vector<std::thread> threads_; std::vector<std::thread> threads_;
std::mutex queues_mutex_; QMutex queues_mutex_;
// queues_mutex_ must be locked when working with the list. // queues_mutex_ must be locked when working with the list.
TaskQueueList queue_list_; TaskQueueList queue_list_;
std::condition_variable thread_condition_; QWaitCondition thread_condition_;
bool stopped_ = false; bool stopped_ = false;
int tasks_in_process_ = 0; int tasks_in_process_ = 0;
int background_tasks_in_process_ = 0; int background_tasks_in_process_ = 0;
@ -175,7 +175,7 @@ TaskQueue *TaskQueue::TaskQueueList::TakeFirst(int list_index_) {
} }
void TaskQueue::TaskThreadPool::AddQueueTask(TaskQueue *queue, Task &&task) { void TaskQueue::TaskThreadPool::AddQueueTask(TaskQueue *queue, Task &&task) {
std::unique_lock<std::mutex> lock(queues_mutex_); QMutexLocker lock(&queues_mutex_);
queue->tasks_.push_back(std::move(task)); queue->tasks_.push_back(std::move(task));
auto list_was_empty = queue_list_.Empty(kAllQueuesList); auto list_was_empty = queue_list_.Empty(kAllQueuesList);
@ -195,12 +195,12 @@ void TaskQueue::TaskThreadPool::AddQueueTask(TaskQueue *queue, Task &&task) {
}); });
} else if (some_threads_are_vacant) { } else if (some_threads_are_vacant) {
t_assert(threads_count > tasks_in_process_); t_assert(threads_count > tasks_in_process_);
thread_condition_.notify_one(); thread_condition_.wakeOne();
} }
} }
void TaskQueue::TaskThreadPool::RemoveQueue(TaskQueue *queue) { void TaskQueue::TaskThreadPool::RemoveQueue(TaskQueue *queue) {
std::unique_lock<std::mutex> lock(queues_mutex_); QMutexLocker lock(&queues_mutex_);
if (queue_list_.IsInList(queue)) { if (queue_list_.IsInList(queue)) {
queue_list_.Unregister(queue); queue_list_.Unregister(queue);
} }
@ -211,11 +211,11 @@ void TaskQueue::TaskThreadPool::RemoveQueue(TaskQueue *queue) {
TaskQueue::TaskThreadPool::~TaskThreadPool() { TaskQueue::TaskThreadPool::~TaskThreadPool() {
{ {
std::unique_lock<std::mutex> lock(queues_mutex_); QMutexLocker lock(&queues_mutex_);
queue_list_.Clear(); queue_list_.Clear();
stopped_ = true; stopped_ = true;
} }
thread_condition_.notify_all(); thread_condition_.wakeAll();
for (auto &thread : threads_) { for (auto &thread : threads_) {
thread.join(); thread.join();
} }
@ -241,7 +241,7 @@ void TaskQueue::TaskThreadPool::ThreadFunction() {
while (true) { while (true) {
Task task; Task task;
{ {
std::unique_lock<std::mutex> lock(queues_mutex_); QMutexLocker lock(&queues_mutex_);
// Finish the previous task processing. // Finish the previous task processing.
if (task_was_processed) { if (task_was_processed) {
@ -267,7 +267,7 @@ void TaskQueue::TaskThreadPool::ThreadFunction() {
if (stopped_) { if (stopped_) {
return; return;
} }
thread_condition_.wait(lock); thread_condition_.wait(&queues_mutex_);
} }
// Select a task we will be processing. // Select a task we will be processing.
@ -321,7 +321,7 @@ TaskQueue::~TaskQueue() {
void TaskQueue::Put(Task &&task) { void TaskQueue::Put(Task &&task) {
if (type_ == Type::Main) { if (type_ == Type::Main) {
std::unique_lock<std::mutex> lock(tasks_mutex_); QMutexLocker lock(&tasks_mutex_);
tasks_.push_back(std::move(task)); tasks_.push_back(std::move(task));
Sandbox::MainThreadTaskAdded(); Sandbox::MainThreadTaskAdded();
@ -352,7 +352,7 @@ void TaskQueue::ProcessMainTasks(TimeMs max_time_spent) { // static
bool TaskQueue::ProcessOneMainTask() { // static bool TaskQueue::ProcessOneMainTask() { // static
Task task; Task task;
{ {
std::unique_lock<std::mutex> lock(Main().tasks_mutex_); QMutexLocker lock(&Main().tasks_mutex_);
auto &tasks = Main().tasks_; auto &tasks = Main().tasks_;
if (tasks.empty()) { if (tasks.empty()) {
return false; return false;

View File

@ -20,7 +20,6 @@ Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
*/ */
#pragma once #pragma once
#include <mutex>
#include <memory> #include <memory>
namespace base { namespace base {
@ -73,7 +72,7 @@ private:
const Priority priority_; const Priority priority_;
std::deque<Task> tasks_; std::deque<Task> tasks_;
std::mutex tasks_mutex_; // Only for the main queue. QMutex tasks_mutex_; // Only for the main queue.
// Only for the other queues, not main. // Only for the other queues, not main.
class TaskThreadPool; class TaskThreadPool;