mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-10-23 16:39:43 +00:00
Common: Add some utilities to a new UnixUtil header.
This commit is contained in:
parent
504ea99cfa
commit
b1e8de82a6
2 changed files with 63 additions and 0 deletions
|
@ -97,6 +97,7 @@ add_library(common
|
|||
JsonUtil.cpp
|
||||
Lazy.h
|
||||
LinearDiskCache.h
|
||||
UnixUtil.h
|
||||
Logging/ConsoleListener.h
|
||||
Logging/Log.h
|
||||
Logging/LogManager.cpp
|
||||
|
|
62
Source/Core/Common/UnixUtil.h
Normal file
62
Source/Core/Common/UnixUtil.h
Normal file
|
@ -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
|
Loading…
Add table
Add a link
Reference in a new issue