2014-05-15 23:19:34 +01:00
|
|
|
// Copyright 2014 Citra Emulator Project / PPSSPP Project
|
2014-12-17 05:38:14 +00:00
|
|
|
// Licensed under GPLv2 or any later version
|
2014-11-19 08:49:13 +00:00
|
|
|
// Refer to the license.txt file included.
|
2014-05-15 23:19:34 +01:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2020-03-26 03:23:39 +00:00
|
|
|
#include <algorithm>
|
2015-01-05 07:48:36 +00:00
|
|
|
#include <array>
|
|
|
|
#include <deque>
|
2019-08-12 17:01:33 +01:00
|
|
|
#include <boost/serialization/deque.hpp>
|
|
|
|
#include <boost/serialization/split_member.hpp>
|
|
|
|
#include "common/common_types.h"
|
2015-01-05 07:48:36 +00:00
|
|
|
|
2014-05-15 23:19:34 +01:00
|
|
|
namespace Common {
|
|
|
|
|
2016-09-18 01:38:01 +01:00
|
|
|
template <class T, unsigned int N>
|
2014-05-15 23:19:34 +01:00
|
|
|
struct ThreadQueueList {
|
2015-01-05 07:48:36 +00:00
|
|
|
// TODO(yuriks): If performance proves to be a problem, the std::deques can be replaced with
|
|
|
|
// (dynamically resizable) circular buffers to remove their overhead when
|
|
|
|
// inserting and popping.
|
2014-11-19 08:49:13 +00:00
|
|
|
|
2020-08-31 20:06:16 +01:00
|
|
|
using Priority = unsigned int;
|
2014-05-15 23:19:34 +01:00
|
|
|
|
2015-01-05 07:48:36 +00:00
|
|
|
// Number of priority levels. (Valid levels are [0..NUM_QUEUES).)
|
2020-08-31 20:06:16 +01:00
|
|
|
static constexpr Priority NUM_QUEUES = N;
|
2014-05-15 23:19:34 +01:00
|
|
|
|
|
|
|
ThreadQueueList() {
|
2015-01-05 07:48:36 +00:00
|
|
|
first = nullptr;
|
2014-05-15 23:19:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Only for debugging, returns priority level.
|
2020-08-31 20:06:16 +01:00
|
|
|
[[nodiscard]] Priority contains(const T& uid) const {
|
2015-01-05 07:48:36 +00:00
|
|
|
for (Priority i = 0; i < NUM_QUEUES; ++i) {
|
2020-08-31 20:06:16 +01:00
|
|
|
const Queue& cur = queues[i];
|
2015-01-05 07:48:36 +00:00
|
|
|
if (std::find(cur.data.cbegin(), cur.data.cend(), uid) != cur.data.cend()) {
|
|
|
|
return i;
|
2014-05-15 23:19:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-08-31 20:06:16 +01:00
|
|
|
[[nodiscard]] T get_first() const {
|
|
|
|
const Queue* cur = first;
|
2015-03-24 03:55:21 +00:00
|
|
|
while (cur != nullptr) {
|
|
|
|
if (!cur->data.empty()) {
|
|
|
|
return cur->data.front();
|
|
|
|
}
|
|
|
|
cur = cur->next_nonempty;
|
|
|
|
}
|
|
|
|
|
|
|
|
return T();
|
|
|
|
}
|
|
|
|
|
2015-01-05 07:48:36 +00:00
|
|
|
T pop_first() {
|
2016-09-18 01:38:01 +01:00
|
|
|
Queue* cur = first;
|
2015-01-05 07:48:36 +00:00
|
|
|
while (cur != nullptr) {
|
|
|
|
if (!cur->data.empty()) {
|
|
|
|
auto tmp = std::move(cur->data.front());
|
|
|
|
cur->data.pop_front();
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
cur = cur->next_nonempty;
|
2014-05-15 23:19:34 +01:00
|
|
|
}
|
|
|
|
|
2015-01-05 07:48:36 +00:00
|
|
|
return T();
|
2014-05-15 23:19:34 +01:00
|
|
|
}
|
|
|
|
|
2015-01-05 07:48:36 +00:00
|
|
|
T pop_first_better(Priority priority) {
|
2016-09-18 01:38:01 +01:00
|
|
|
Queue* cur = first;
|
|
|
|
Queue* stop = &queues[priority];
|
2015-01-05 07:48:36 +00:00
|
|
|
while (cur < stop) {
|
|
|
|
if (!cur->data.empty()) {
|
|
|
|
auto tmp = std::move(cur->data.front());
|
|
|
|
cur->data.pop_front();
|
|
|
|
return tmp;
|
|
|
|
}
|
|
|
|
cur = cur->next_nonempty;
|
2014-05-15 23:19:34 +01:00
|
|
|
}
|
|
|
|
|
2015-01-05 07:48:36 +00:00
|
|
|
return T();
|
2014-05-15 23:19:34 +01:00
|
|
|
}
|
|
|
|
|
2015-01-05 07:48:36 +00:00
|
|
|
void push_front(Priority priority, const T& thread_id) {
|
2016-09-18 01:38:01 +01:00
|
|
|
Queue* cur = &queues[priority];
|
2015-01-05 07:48:36 +00:00
|
|
|
cur->data.push_front(thread_id);
|
2014-05-15 23:19:34 +01:00
|
|
|
}
|
|
|
|
|
2015-01-05 07:48:36 +00:00
|
|
|
void push_back(Priority priority, const T& thread_id) {
|
2016-09-18 01:38:01 +01:00
|
|
|
Queue* cur = &queues[priority];
|
2015-01-05 07:48:36 +00:00
|
|
|
cur->data.push_back(thread_id);
|
2014-05-15 23:19:34 +01:00
|
|
|
}
|
|
|
|
|
2015-03-24 03:55:21 +00:00
|
|
|
void move(const T& thread_id, Priority old_priority, Priority new_priority) {
|
|
|
|
remove(old_priority, thread_id);
|
|
|
|
prepare(new_priority);
|
|
|
|
push_back(new_priority, thread_id);
|
|
|
|
}
|
|
|
|
|
2015-01-05 07:48:36 +00:00
|
|
|
void remove(Priority priority, const T& thread_id) {
|
2019-03-16 08:41:34 +00:00
|
|
|
Queue* const cur = &queues[priority];
|
|
|
|
const auto iter = std::remove(cur->data.begin(), cur->data.end(), thread_id);
|
|
|
|
cur->data.erase(iter, cur->data.end());
|
2014-05-15 23:19:34 +01:00
|
|
|
}
|
|
|
|
|
2015-01-05 07:48:36 +00:00
|
|
|
void rotate(Priority priority) {
|
2016-09-18 01:38:01 +01:00
|
|
|
Queue* cur = &queues[priority];
|
2014-05-15 23:19:34 +01:00
|
|
|
|
2015-01-05 07:48:36 +00:00
|
|
|
if (cur->data.size() > 1) {
|
|
|
|
cur->data.push_back(std::move(cur->data.front()));
|
|
|
|
cur->data.pop_front();
|
2014-05-15 23:19:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-05 07:48:36 +00:00
|
|
|
void clear() {
|
|
|
|
queues.fill(Queue());
|
|
|
|
first = nullptr;
|
2014-05-15 23:19:34 +01:00
|
|
|
}
|
|
|
|
|
2020-08-31 20:06:16 +01:00
|
|
|
[[nodiscard]] bool empty(Priority priority) const {
|
2016-09-18 01:38:01 +01:00
|
|
|
const Queue* cur = &queues[priority];
|
2015-01-05 07:48:36 +00:00
|
|
|
return cur->data.empty();
|
2014-05-15 23:19:34 +01:00
|
|
|
}
|
|
|
|
|
2015-01-05 07:48:36 +00:00
|
|
|
void prepare(Priority priority) {
|
|
|
|
Queue* cur = &queues[priority];
|
|
|
|
if (cur->next_nonempty == UnlinkedTag())
|
|
|
|
link(priority);
|
2014-05-15 23:19:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-01-05 07:48:36 +00:00
|
|
|
struct Queue {
|
|
|
|
// Points to the next active priority, skipping over ones that have never been used.
|
|
|
|
Queue* next_nonempty = UnlinkedTag();
|
|
|
|
// Double-ended queue of threads in this priority level
|
|
|
|
std::deque<T> data;
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Special tag used to mark priority levels that have never been used.
|
|
|
|
static Queue* UnlinkedTag() {
|
|
|
|
return reinterpret_cast<Queue*>(1);
|
2014-05-15 23:19:34 +01:00
|
|
|
}
|
|
|
|
|
2015-01-05 07:48:36 +00:00
|
|
|
void link(Priority priority) {
|
2016-09-18 01:38:01 +01:00
|
|
|
Queue* cur = &queues[priority];
|
2015-01-05 07:48:36 +00:00
|
|
|
|
|
|
|
for (int i = priority - 1; i >= 0; --i) {
|
|
|
|
if (queues[i].next_nonempty != UnlinkedTag()) {
|
|
|
|
cur->next_nonempty = queues[i].next_nonempty;
|
|
|
|
queues[i].next_nonempty = cur;
|
2014-05-15 23:19:34 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-05 07:48:36 +00:00
|
|
|
cur->next_nonempty = first;
|
2014-05-15 23:19:34 +01:00
|
|
|
first = cur;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The first queue that's ever been used.
|
2015-01-05 07:48:36 +00:00
|
|
|
Queue* first;
|
2014-05-15 23:19:34 +01:00
|
|
|
// The priority level queues of thread ids.
|
2015-01-05 07:48:36 +00:00
|
|
|
std::array<Queue, NUM_QUEUES> queues;
|
2019-08-12 17:01:33 +01:00
|
|
|
|
2020-03-29 16:14:36 +01:00
|
|
|
s64 ToIndex(const Queue* q) const {
|
2020-01-12 00:24:44 +00:00
|
|
|
if (q == nullptr) {
|
|
|
|
return -2;
|
|
|
|
} else if (q == UnlinkedTag()) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
2020-03-29 11:39:46 +01:00
|
|
|
return q - queues.data();
|
2020-01-12 00:24:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-29 16:14:36 +01:00
|
|
|
Queue* ToPointer(s64 idx) {
|
2020-01-12 00:24:44 +00:00
|
|
|
if (idx == -1) {
|
|
|
|
return UnlinkedTag();
|
|
|
|
} else if (idx < 0) {
|
|
|
|
return nullptr;
|
|
|
|
} else {
|
|
|
|
return &queues[idx];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-12 17:01:33 +01:00
|
|
|
friend class boost::serialization::access;
|
|
|
|
template <class Archive>
|
2019-12-27 21:07:29 +00:00
|
|
|
void save(Archive& ar, const unsigned int file_version) const {
|
2020-03-29 16:14:36 +01:00
|
|
|
const s64 idx = ToIndex(first);
|
2019-08-12 17:01:33 +01:00
|
|
|
ar << idx;
|
2020-03-31 17:54:28 +01:00
|
|
|
for (std::size_t i = 0; i < NUM_QUEUES; i++) {
|
2020-03-29 16:14:36 +01:00
|
|
|
const s64 idx1 = ToIndex(queues[i].next_nonempty);
|
2019-08-12 17:01:33 +01:00
|
|
|
ar << idx1;
|
|
|
|
ar << queues[i].data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Archive>
|
2019-12-27 21:07:29 +00:00
|
|
|
void load(Archive& ar, const unsigned int file_version) {
|
2020-03-29 16:14:36 +01:00
|
|
|
s64 idx;
|
2019-08-12 17:01:33 +01:00
|
|
|
ar >> idx;
|
2020-01-12 00:24:44 +00:00
|
|
|
first = ToPointer(idx);
|
2020-03-31 17:54:28 +01:00
|
|
|
for (std::size_t i = 0; i < NUM_QUEUES; i++) {
|
2019-08-12 17:01:33 +01:00
|
|
|
ar >> idx;
|
2020-01-12 00:24:44 +00:00
|
|
|
queues[i].next_nonempty = ToPointer(idx);
|
2019-08-12 17:01:33 +01:00
|
|
|
ar >> queues[i].data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_SERIALIZATION_SPLIT_MEMBER()
|
2014-05-15 23:19:34 +01:00
|
|
|
};
|
|
|
|
|
2018-03-09 17:54:43 +00:00
|
|
|
} // namespace Common
|