2016-12-23 13:21:01 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop version of Telegram messaging app, see https://telegram.org
|
|
|
|
|
|
|
|
Telegram Desktop is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
In addition, as a special exception, the copyright holders give permission
|
|
|
|
to link the code of portions of this program with the OpenSSL library.
|
|
|
|
|
|
|
|
Full license: https://github.com/telegramdesktop/tdesktop/blob/master/LICENSE
|
2017-01-11 18:31:31 +00:00
|
|
|
Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org
|
2016-12-23 13:21:01 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
2017-03-04 19:36:59 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2016-12-23 13:21:01 +00:00
|
|
|
namespace base {
|
|
|
|
|
2017-02-26 11:32:13 +00:00
|
|
|
using Task = lambda_once<void()>;
|
2016-12-23 13:21:01 +00:00
|
|
|
|
|
|
|
// An attempt to create/use a TaskQueue or one of the default queues
|
|
|
|
// after the main() has returned leads to an undefined behaviour.
|
|
|
|
class TaskQueue {
|
|
|
|
enum class Type {
|
|
|
|
Main, // Unique queue for main thread tasks.
|
|
|
|
Serial,
|
|
|
|
Concurrent,
|
|
|
|
Special, // Unique special queue for thread pool lists terminal item.
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
|
|
|
enum class Priority {
|
|
|
|
Normal,
|
|
|
|
Background,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Creating custom serial queues.
|
|
|
|
TaskQueue(Priority priority) : TaskQueue(Type::Serial, priority) {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default main and two concurrent queues.
|
|
|
|
static TaskQueue &Main();
|
|
|
|
static TaskQueue &Normal();
|
|
|
|
static TaskQueue &Background();
|
|
|
|
|
|
|
|
void Put(Task &&task);
|
|
|
|
|
|
|
|
static void ProcessMainTasks();
|
|
|
|
static void ProcessMainTasks(TimeMs max_time_spent);
|
|
|
|
|
|
|
|
~TaskQueue();
|
|
|
|
|
|
|
|
private:
|
|
|
|
static bool ProcessOneMainTask();
|
|
|
|
|
|
|
|
TaskQueue(Type type, Priority priority);
|
|
|
|
|
|
|
|
bool IsMyThread() const;
|
|
|
|
bool SerialTaskInProcess() const {
|
|
|
|
return (destroyed_flag_ != nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
const Type type_;
|
|
|
|
const Priority priority_;
|
|
|
|
|
2017-02-26 11:32:13 +00:00
|
|
|
std::deque<Task> tasks_;
|
2017-05-11 14:21:43 +00:00
|
|
|
QMutex tasks_mutex_; // Only for the main queue.
|
2016-12-23 13:21:01 +00:00
|
|
|
|
|
|
|
// Only for the other queues, not main.
|
|
|
|
class TaskThreadPool;
|
2017-03-04 19:36:59 +00:00
|
|
|
std::weak_ptr<TaskThreadPool> weak_thread_pool_;
|
2016-12-23 13:21:01 +00:00
|
|
|
|
|
|
|
class TaskQueueList;
|
|
|
|
|
|
|
|
struct TaskQueueListEntry {
|
|
|
|
TaskQueue *before = nullptr;
|
|
|
|
TaskQueue *after = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Thread pool queues linked list.
|
|
|
|
static constexpr int kAllQueuesList = 0;
|
|
|
|
|
|
|
|
// Thread pool queues linked list with excluded Background queues.
|
|
|
|
static constexpr int kOnlyNormalQueuesList = 1;
|
|
|
|
|
|
|
|
static constexpr int kQueuesListsCount = 2;
|
|
|
|
TaskQueueListEntry list_entries_[kQueuesListsCount];
|
|
|
|
|
|
|
|
// Only for Serial queues: non-null value means a task is currently processed.
|
|
|
|
bool *destroyed_flag_ = nullptr;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace base
|