early-access version 3466

This commit is contained in:
pineappleEA 2023-03-19 22:38:32 +01:00
parent 587f4c8b2f
commit 6cdaf33559
6 changed files with 224 additions and 127 deletions

View File

@ -1,7 +1,7 @@
yuzu emulator early access yuzu emulator early access
============= =============
This is the source code for early-access 3465. This is the source code for early-access 3466.
## Legal Notice ## Legal Notice

View File

@ -24,112 +24,53 @@ class SPSCQueue {
public: public:
bool TryPush(T&& t) { bool TryPush(T&& t) {
const size_t write_index = m_write_index.load(); return Push<PushMode::Try>(std::move(t));
// Check if we have free slots to write to.
if ((write_index - m_read_index.load()) == Capacity) {
return false;
}
// Determine the position to write to.
const size_t pos = write_index % Capacity;
// Push into the queue.
m_data[pos] = std::move(t);
// Increment the write index.
++m_write_index;
// Notify the consumer that we have pushed into the queue.
std::scoped_lock lock{cv_mutex};
cv.notify_one();
return true;
} }
template <typename... Args> template <typename... Args>
bool TryPush(Args&&... args) { bool TryEmplace(Args&&... args) {
const size_t write_index = m_write_index.load(); return Emplace<PushMode::Try>(std::forward<Args>(args)...);
// Check if we have free slots to write to.
if ((write_index - m_read_index.load()) == Capacity) {
return false;
}
// Determine the position to write to.
const size_t pos = write_index % Capacity;
// Emplace into the queue.
std::construct_at(std::addressof(m_data[pos]), std::forward<Args>(args)...);
// Increment the write index.
++m_write_index;
// Notify the consumer that we have pushed into the queue.
std::scoped_lock lock{cv_mutex};
cv.notify_one();
return true;
} }
void Push(T&& t) { void PushWait(T&& t) {
const size_t write_index = m_write_index.load(); Push<PushMode::Wait>(std::move(t));
// Wait until we have free slots to write to.
while ((write_index - m_read_index.load()) == Capacity) {
std::this_thread::yield();
}
// Determine the position to write to.
const size_t pos = write_index % Capacity;
// Push into the queue.
m_data[pos] = std::move(t);
// Increment the write index.
++m_write_index;
// Notify the consumer that we have pushed into the queue.
std::scoped_lock lock{cv_mutex};
cv.notify_one();
} }
template <typename... Args> template <typename... Args>
void Push(Args&&... args) { void EmplaceWait(Args&&... args) {
const size_t write_index = m_write_index.load(); Emplace<PushMode::Wait>(std::forward<Args>(args)...);
}
// Wait until we have free slots to write to. void PushOverwrite(T&& t) {
while ((write_index - m_read_index.load()) == Capacity) { Push<PushMode::Overwrite>(std::move(t));
std::this_thread::yield(); }
}
// Determine the position to write to. template <typename... Args>
const size_t pos = write_index % Capacity; void EmplaceOverwrite(Args&&... args) {
Emplace<PushMode::Overwrite>(std::forward<Args>(args)...);
// Emplace into the queue.
std::construct_at(std::addressof(m_data[pos]), std::forward<Args>(args)...);
// Increment the write index.
++m_write_index;
// Notify the consumer that we have pushed into the queue.
std::scoped_lock lock{cv_mutex};
cv.notify_one();
} }
bool TryPop(T& t) { bool TryPop(T& t) {
return Pop(t); return Pop<PopMode::Try>(t);
}
void PopWait(T& t) {
Pop<PopMode::Wait>(t);
} }
void PopWait(T& t, std::stop_token stop_token) { void PopWait(T& t, std::stop_token stop_token) {
Wait(stop_token); Pop<PopMode::WaitWithStopToken>(t, stop_token);
Pop(t); }
T PopWait() {
T t;
Pop<PopMode::Wait>(t);
return t;
} }
T PopWait(std::stop_token stop_token) { T PopWait(std::stop_token stop_token) {
Wait(stop_token);
T t; T t;
Pop(t); Pop<PopMode::WaitWithStopToken>(t, stop_token);
return t; return t;
} }
@ -148,6 +89,102 @@ public:
} }
private: private:
enum class PushMode {
Try,
Wait,
Overwrite,
Count,
};
enum class PopMode {
Try,
Wait,
WaitWithStopToken,
Count,
};
template <PushMode Mode>
bool Push(T&& t) {
const size_t write_index = m_write_index.load();
if constexpr (Mode == PushMode::Try) {
// Check if we have free slots to write to.
if ((write_index - m_read_index.load()) == Capacity) {
return false;
}
} else if constexpr (Mode == PushMode::Wait) {
// Wait until we have free slots to write to.
std::unique_lock lock{producer_cv_mutex};
producer_cv.wait(lock, [this, write_index] {
return (write_index - m_read_index.load()) < Capacity;
});
} else if constexpr (Mode == PushMode::Overwrite) {
// Check if we have free slots to write to.
if ((write_index - m_read_index.load()) == Capacity) {
// If we don't, increment the read index. This is effectively a pop operation.
++m_read_index;
}
} else {
static_assert(Mode < PushMode::Count, "Invalid PushMode.");
}
// Determine the position to write to.
const size_t pos = write_index % Capacity;
// Push into the queue.
m_data[pos] = std::move(t);
// Increment the write index.
++m_write_index;
// Notify the consumer that we have pushed into the queue.
std::scoped_lock lock{consumer_cv_mutex};
consumer_cv.notify_one();
return true;
}
template <PushMode Mode, typename... Args>
bool Emplace(Args&&... args) {
const size_t write_index = m_write_index.load();
if constexpr (Mode == PushMode::Try) {
// Check if we have free slots to write to.
if ((write_index - m_read_index.load()) == Capacity) {
return false;
}
} else if constexpr (Mode == PushMode::Wait) {
// Wait until we have free slots to write to.
std::unique_lock lock{producer_cv_mutex};
producer_cv.wait(lock, [this, write_index] {
return (write_index - m_read_index.load()) < Capacity;
});
} else if constexpr (Mode == PushMode::Overwrite) {
// Check if we have free slots to write to.
if ((write_index - m_read_index.load()) == Capacity) {
// If we don't, increment the read index. This is effectively a pop operation.
++m_read_index;
}
} else {
static_assert(Mode < PushMode::Count, "Invalid PushMode.");
}
// Determine the position to write to.
const size_t pos = write_index % Capacity;
// Emplace into the queue.
std::construct_at(std::addressof(m_data[pos]), std::forward<Args>(args)...);
// Increment the write index.
++m_write_index;
// Notify the consumer that we have pushed into the queue.
std::scoped_lock lock{consumer_cv_mutex};
consumer_cv.notify_one();
return true;
}
void Pop() { void Pop() {
const size_t read_index = m_read_index.load(); const size_t read_index = m_read_index.load();
@ -164,14 +201,33 @@ private:
// Increment the read index. // Increment the read index.
++m_read_index; ++m_read_index;
// Notify the producer that we have popped off the queue.
std::unique_lock lock{producer_cv_mutex};
producer_cv.notify_one();
} }
bool Pop(T& t) { template <PopMode Mode>
bool Pop(T& t, [[maybe_unused]] std::stop_token stop_token = {}) {
const size_t read_index = m_read_index.load(); const size_t read_index = m_read_index.load();
// Check if the queue is empty. if constexpr (Mode == PopMode::Try) {
if (read_index == m_write_index.load()) { // Check if the queue is empty.
return false; if (read_index == m_write_index.load()) {
return false;
}
} else if constexpr (Mode == PopMode::Wait) {
// Wait until the queue is not empty.
std::unique_lock lock{consumer_cv_mutex};
consumer_cv.wait(lock,
[this, read_index] { return read_index != m_write_index.load(); });
} else if constexpr (Mode == PopMode::WaitWithStopToken) {
// Wait until the queue is not empty.
std::unique_lock lock{consumer_cv_mutex};
Common::CondvarWait(consumer_cv, lock, stop_token,
[this, read_index] { return read_index != m_write_index.load(); });
} else {
static_assert(Mode < PopMode::Count, "Invalid PopMode.");
} }
// Determine the position to read from. // Determine the position to read from.
@ -183,12 +239,11 @@ private:
// Increment the read index. // Increment the read index.
++m_read_index; ++m_read_index;
return true; // Notify the producer that we have popped off the queue.
} std::unique_lock lock{producer_cv_mutex};
producer_cv.notify_one();
void Wait(std::stop_token stop_token) { return true;
std::unique_lock lock{cv_mutex};
Common::CondvarWait(cv, lock, stop_token, [this] { return !Empty(); });
} }
#ifdef __cpp_lib_hardware_interference_size #ifdef __cpp_lib_hardware_interference_size
@ -201,43 +256,64 @@ private:
std::array<T, Capacity> m_data; std::array<T, Capacity> m_data;
std::condition_variable_any cv; std::condition_variable_any producer_cv;
std::mutex cv_mutex; std::mutex producer_cv_mutex;
std::condition_variable_any consumer_cv;
std::mutex consumer_cv_mutex;
}; };
template <typename T, size_t Capacity = detail::DefaultCapacity> template <typename T, size_t Capacity = detail::DefaultCapacity>
class MPSCQueue { class MPSCQueue {
public: public:
void TryPush(T&& t) { bool TryPush(T&& t) {
std::scoped_lock lock{write_mutex}; std::scoped_lock lock{write_mutex};
spsc_queue.TryPush(std::move(t)); return spsc_queue.TryPush(std::move(t));
} }
template <typename... Args> template <typename... Args>
void TryPush(Args&&... args) { bool TryEmplace(Args&&... args) {
std::scoped_lock lock{write_mutex}; std::scoped_lock lock{write_mutex};
spsc_queue.TryPush(std::forward<Args>(args)...); return spsc_queue.TryEmplace(std::forward<Args>(args)...);
} }
void Push(T&& t) { void PushWait(T&& t) {
std::scoped_lock lock{write_mutex}; std::scoped_lock lock{write_mutex};
spsc_queue.Push(std::move(t)); spsc_queue.PushWait(std::move(t));
} }
template <typename... Args> template <typename... Args>
void Push(Args&&... args) { void EmplaceWait(Args&&... args) {
std::scoped_lock lock{write_mutex}; std::scoped_lock lock{write_mutex};
spsc_queue.Push(std::forward<Args>(args)...); spsc_queue.EmplaceWait(std::forward<Args>(args)...);
}
void PushOverwrite(T&& t) {
std::scoped_lock lock{write_mutex};
spsc_queue.PushOverwrite(std::move(t));
}
template <typename... Args>
void EmplaceOverwrite(Args&&... args) {
std::scoped_lock lock{write_mutex};
spsc_queue.EmplaceOverwrite(std::forward<Args>(args)...);
} }
bool TryPop(T& t) { bool TryPop(T& t) {
return spsc_queue.TryPop(t); return spsc_queue.TryPop(t);
} }
void PopWait(T& t) {
spsc_queue.PopWait(t);
}
void PopWait(T& t, std::stop_token stop_token) { void PopWait(T& t, std::stop_token stop_token) {
spsc_queue.PopWait(t, stop_token); spsc_queue.PopWait(t, stop_token);
} }
T PopWait() {
return spsc_queue.PopWait();
}
T PopWait(std::stop_token stop_token) { T PopWait(std::stop_token stop_token) {
return spsc_queue.PopWait(stop_token); return spsc_queue.PopWait(stop_token);
} }
@ -262,26 +338,37 @@ private:
template <typename T, size_t Capacity = detail::DefaultCapacity> template <typename T, size_t Capacity = detail::DefaultCapacity>
class MPMCQueue { class MPMCQueue {
public: public:
void TryPush(T&& t) { bool TryPush(T&& t) {
std::scoped_lock lock{write_mutex}; std::scoped_lock lock{write_mutex};
spsc_queue.TryPush(std::move(t)); return spsc_queue.TryPush(std::move(t));
} }
template <typename... Args> template <typename... Args>
void TryPush(Args&&... args) { bool TryEmplace(Args&&... args) {
std::scoped_lock lock{write_mutex}; std::scoped_lock lock{write_mutex};
spsc_queue.TryPush(std::forward<Args>(args)...); return spsc_queue.TryEmplace(std::forward<Args>(args)...);
} }
void Push(T&& t) { void PushWait(T&& t) {
std::scoped_lock lock{write_mutex}; std::scoped_lock lock{write_mutex};
spsc_queue.Push(std::move(t)); spsc_queue.PushWait(std::move(t));
} }
template <typename... Args> template <typename... Args>
void Push(Args&&... args) { void EmplaceWait(Args&&... args) {
std::scoped_lock lock{write_mutex}; std::scoped_lock lock{write_mutex};
spsc_queue.Push(std::forward<Args>(args)...); spsc_queue.EmplaceWait(std::forward<Args>(args)...);
}
void PushOverwrite(T&& t) {
std::scoped_lock lock{write_mutex};
spsc_queue.PushOverwrite(std::move(t));
}
template <typename... Args>
void EmplaceOverwrite(Args&&... args) {
std::scoped_lock lock{write_mutex};
spsc_queue.EmplaceOverwrite(std::forward<Args>(args)...);
} }
bool TryPop(T& t) { bool TryPop(T& t) {
@ -289,11 +376,21 @@ public:
return spsc_queue.TryPop(t); return spsc_queue.TryPop(t);
} }
void PopWait(T& t) {
std::scoped_lock lock{read_mutex};
spsc_queue.PopWait(t);
}
void PopWait(T& t, std::stop_token stop_token) { void PopWait(T& t, std::stop_token stop_token) {
std::scoped_lock lock{read_mutex}; std::scoped_lock lock{read_mutex};
spsc_queue.PopWait(t, stop_token); spsc_queue.PopWait(t, stop_token);
} }
T PopWait() {
std::scoped_lock lock{read_mutex};
return spsc_queue.PopWait();
}
T PopWait(std::stop_token stop_token) { T PopWait(std::stop_token stop_token) {
std::scoped_lock lock{read_mutex}; std::scoped_lock lock{read_mutex};
return spsc_queue.PopWait(stop_token); return spsc_queue.PopWait(stop_token);

View File

@ -207,7 +207,7 @@ public:
if (!filter.CheckMessage(log_class, log_level)) { if (!filter.CheckMessage(log_class, log_level)) {
return; return;
} }
message_queue.Push( message_queue.EmplaceWait(
CreateEntry(log_class, log_level, filename, line_num, function, std::move(message))); CreateEntry(log_class, log_level, filename, line_num, function, std::move(message)));
} }

View File

@ -125,18 +125,18 @@ std::string ReplaceAll(std::string result, const std::string& src, const std::st
return result; return result;
} }
std::string UTF16ToUTF8(const std::u16string& input) { std::string UTF16ToUTF8(std::u16string_view input) {
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert; std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
return convert.to_bytes(input); return convert.to_bytes(input.data(), input.data() + input.size());
} }
std::u16string UTF8ToUTF16(const std::string& input) { std::u16string UTF8ToUTF16(std::string_view input) {
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert; std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
return convert.from_bytes(input); return convert.from_bytes(input.data(), input.data() + input.size());
} }
#ifdef _WIN32 #ifdef _WIN32
static std::wstring CPToUTF16(u32 code_page, const std::string& input) { static std::wstring CPToUTF16(u32 code_page, std::string_view input) {
const auto size = const auto size =
MultiByteToWideChar(code_page, 0, input.data(), static_cast<int>(input.size()), nullptr, 0); MultiByteToWideChar(code_page, 0, input.data(), static_cast<int>(input.size()), nullptr, 0);
@ -154,7 +154,7 @@ static std::wstring CPToUTF16(u32 code_page, const std::string& input) {
return output; return output;
} }
std::string UTF16ToUTF8(const std::wstring& input) { std::string UTF16ToUTF8(std::wstring_view input) {
const auto size = WideCharToMultiByte(CP_UTF8, 0, input.data(), static_cast<int>(input.size()), const auto size = WideCharToMultiByte(CP_UTF8, 0, input.data(), static_cast<int>(input.size()),
nullptr, 0, nullptr, nullptr); nullptr, 0, nullptr, nullptr);
if (size == 0) { if (size == 0) {
@ -172,7 +172,7 @@ std::string UTF16ToUTF8(const std::wstring& input) {
return output; return output;
} }
std::wstring UTF8ToUTF16W(const std::string& input) { std::wstring UTF8ToUTF16W(std::string_view input) {
return CPToUTF16(CP_UTF8, input); return CPToUTF16(CP_UTF8, input);
} }

View File

@ -36,12 +36,12 @@ bool SplitPath(const std::string& full_path, std::string* _pPath, std::string* _
[[nodiscard]] std::string ReplaceAll(std::string result, const std::string& src, [[nodiscard]] std::string ReplaceAll(std::string result, const std::string& src,
const std::string& dest); const std::string& dest);
[[nodiscard]] std::string UTF16ToUTF8(const std::u16string& input); [[nodiscard]] std::string UTF16ToUTF8(std::u16string_view input);
[[nodiscard]] std::u16string UTF8ToUTF16(const std::string& input); [[nodiscard]] std::u16string UTF8ToUTF16(std::string_view input);
#ifdef _WIN32 #ifdef _WIN32
[[nodiscard]] std::string UTF16ToUTF8(const std::wstring& input); [[nodiscard]] std::string UTF16ToUTF8(std::wstring_view input);
[[nodiscard]] std::wstring UTF8ToUTF16W(const std::string& str); [[nodiscard]] std::wstring UTF8ToUTF16W(std::string_view str);
#endif #endif

View File

@ -121,7 +121,7 @@ u64 ThreadManager::PushCommand(CommandData&& command_data, bool block) {
std::unique_lock lk(state.write_lock); std::unique_lock lk(state.write_lock);
const u64 fence{++state.last_fence}; const u64 fence{++state.last_fence};
state.queue.Push(std::move(command_data), fence, block); state.queue.EmplaceWait(std::move(command_data), fence, block);
if (block) { if (block) {
Common::CondvarWait(state.cv, lk, thread.get_stop_token(), [this, fence] { Common::CondvarWait(state.cv, lk, thread.get_stop_token(), [this, fence] {