diff options
| author | Jordan Woyak <jordan.woyak@gmail.com> | 2025-09-19 02:03:40 -0500 |
|---|---|---|
| committer | Jordan Woyak <jordan.woyak@gmail.com> | 2025-10-04 14:51:17 -0500 |
| commit | b1e8de82a632f38424fd8acff36cf3db48e9802b (patch) | |
| tree | edfdbc3518c3f51dd2d0dc96993a105176557b1b /Source/Core/Common | |
| parent | 504ea99cfa2b083965b427121c3f2a273542c7df (diff) | |
Common: Add some utilities to a new UnixUtil header.
Diffstat (limited to 'Source/Core/Common')
| -rw-r--r-- | Source/Core/Common/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | Source/Core/Common/UnixUtil.h | 62 |
2 files changed, 63 insertions, 0 deletions
diff --git a/Source/Core/Common/CMakeLists.txt b/Source/Core/Common/CMakeLists.txt index ee738ba4e3..f4e3489f1f 100644 --- a/Source/Core/Common/CMakeLists.txt +++ b/Source/Core/Common/CMakeLists.txt @@ -97,6 +97,7 @@ add_library(common JsonUtil.cpp Lazy.h LinearDiskCache.h + UnixUtil.h Logging/ConsoleListener.h Logging/Log.h Logging/LogManager.cpp diff --git a/Source/Core/Common/UnixUtil.h b/Source/Core/Common/UnixUtil.h new file mode 100644 index 0000000000..f1b5fc136f --- /dev/null +++ b/Source/Core/Common/UnixUtil.h @@ -0,0 +1,62 @@ +// Copyright 2025 Dolphin Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include <pthread.h> +#include <sys/eventfd.h> + +#include "Common/CommonFuncs.h" +#include "Common/Logging/Log.h" + +namespace UnixUtil +{ +inline int CreateEventFD(unsigned int count, int flags) +{ + const int result = eventfd(count, flags); + if (result == -1) + { + ERROR_LOG_FMT(COMMON, "eventfd failed: {}", Common::LastStrerrorString()); + std::abort(); + } + return result; +} + +// Repeatedly call a function that can erroneously produce EINTR. +auto RetryOnEINTR(auto func, auto... args) +{ + while (true) + { + const int result = func(args...); + if (result >= 0 || errno != EINTR) + return result; + } +} + +// This is a very low-effort wrapper for pthread. +// It allows creating a pthread from any callable (e.g. a lambda). +// The wrapper object must exist for the lifetime of the thread. +template <typename Func> +struct PThreadWrapper +{ + Func func; + pthread_t handle{}; + + explicit PThreadWrapper(Func&& f) : func(std::move(f)) + { + if (int result = pthread_create( + &handle, nullptr, + [](void* arg) -> void* { + static_cast<PThreadWrapper*>(arg)->func(); + return nullptr; + }, + this); + result != 0) + { + ERROR_LOG_FMT(COMMON, "pthread_create: {}", Common::StrerrorString(result)); + std::abort(); + } + } +}; + +} // namespace UnixUtil |
