Debian Codegraph @

Source/Core/Common/UnixUtil.h

raw symbols SCIP
1 // Copyright 2025 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #pragma once
5
6 #include <pthread.h>
7
8 #if defined(__linux__)
9 #include <sys/eventfd.h>
10 #endif
11
12 #include "Common/CommonFuncs.h"
13 #include "Common/Logging/Log.h"
14
15 namespace UnixUtil
16 {
17
18 #if defined(__linux__)
19 inline int CreateEventFD(unsigned int count, int flags)
20 {
21 const int result = eventfd(count, flags);
22 if (result == -1)
23 {
24 ERROR_LOG_FMT(COMMON, "eventfd failed: {}", Common::LastStrerrorString());
25 std::abort();
26 }
27 return result;
28 }
29 #endif
30
31 // Repeatedly call a function that can erroneously produce EINTR.
32 auto RetryOnEINTR(auto func, auto... args)
33 {
34 while (true)
35 {
36 const int result = func(args...);
37 if (result >= 0 || errno != EINTR)
38 return result;
39 }
40 }
41
42 // This is a very low-effort wrapper for pthread.
43 // It allows creating a pthread from any callable (e.g. a lambda).
44 // The wrapper object must exist for the lifetime of the thread.
45 template <typename Func>
46 struct PThreadWrapper
47 {
48 Func func;
49 pthread_t handle{};
50
51 explicit PThreadWrapper(Func&& f) : func(std::move(f))
52 {
53 if (int result = pthread_create(
54 &handle, nullptr,
55 [](void* arg) -> void* {
56 static_cast<PThreadWrapper*>(arg)->func();
57 return nullptr;
58 },
59 this);
60 result != 0)
61 {
62 ERROR_LOG_FMT(COMMON, "pthread_create: {}", Common::StrerrorString(result));
63 std::abort();
64 }
65 }
66 };
67
68 } // namespace UnixUtil