ReXGlue SDK
Loading...
Searching...
No Matches
thread_registry.h
Go to the documentation of this file.
1
10
11#pragma once
12
13#include <chrono>
14#include <memory>
15#include <optional>
16#include <span>
17#include <vector>
18
19#include <rex/platform.h>
20#include <rex/platform/socket.h>
21#include <rex/platform/sync.h>
22#include <rex/platform/thread.h>
23#include <rex/ppc/context.h>
24#include <rex/result.h>
27#include <rex/types.h>
28#include <rex/module.h>
29#include <rex/utility.h>
30
31namespace rex::kernel {
32
34class ThreadPin : public rex::MoveOnly {
35 public:
36 ThreadPin() = default;
38 explicit ThreadPin(KernelThread* thread) noexcept : thread_(thread) {}
39 ThreadPin(ThreadPin&& other) noexcept : thread_(other.thread_) { other.thread_ = nullptr; }
40 ThreadPin& operator=(ThreadPin&& other) noexcept {
41 if (this != &other) {
42 Reset();
43 thread_ = other.thread_;
44 other.thread_ = nullptr;
45 }
46 return *this;
47 }
48 ~ThreadPin() { Reset(); }
49
50 explicit operator bool() const noexcept { return thread_ != nullptr; }
51 KernelThread* operator->() const noexcept { return thread_; }
52 KernelThread& operator*() const noexcept { return *thread_; }
53 [[nodiscard]] KernelThread* GetThread() const noexcept { return thread_; }
54
55 void Reset() noexcept {
56 if (thread_ != nullptr) {
57 if (thread_->pins.Unpin()) {
58 delete thread_;
59 }
60 thread_ = nullptr;
61 }
62 }
63
64 private:
65 KernelThread* thread_ = nullptr;
66};
67
69struct GuestFiber {
70 rex::platform::Fiber* host_fiber;
71 u32 uid;
72 u32 guest_context_addr;
75};
76
80 public:
81 virtual ~IObjectWaiter() = default;
82
90 std::chrono::milliseconds timeout) = 0;
91
101 virtual X_STATUS WaitGuestObject(X_HANDLE handle, u32 wait_reason, u32 processor_mode,
102 u32 alertable, u64* opt_timeout) = 0;
103
114 virtual X_STATUS WaitGuestObjects(std::span<const X_HANDLE> handles, u32 wait_type,
115 u32 wait_reason, u32 processor_mode, u32 alertable,
116 u64* opt_timeout) = 0;
117
125 std::span<const rex::platform::SocketWait> sockets, std::chrono::milliseconds timeout) = 0;
126};
127
130 virtual ~IThreadRegistry() = default;
131
137 [[nodiscard]] virtual Result<X_HANDLE> Launch(std::unique_ptr<KernelThread> thread) = 0;
138
146 [[nodiscard]] virtual Result<X_HANDLE> CreateTaskThread(u32 start_address, u32 start_context) = 0;
147
149 [[nodiscard]] virtual ICallingThread* GetCallingThread() = 0;
150
151 [[nodiscard]] virtual IObjectWaiter& GetObjectWaiter() = 0;
152
154 virtual void SleepCurrentThread(std::chrono::microseconds duration) = 0;
155
161 [[nodiscard]] virtual ThreadPin Find(u32 thread_id) = 0;
162
169 [[nodiscard]] virtual ThreadPin FindByHandle(X_HANDLE handle) = 0;
170
171 [[nodiscard]] virtual std::vector<ThreadPin> All() = 0;
172
178 [[nodiscard]] virtual std::optional<u32> AllocateTLS(PPCContext* context) = 0;
179
181 virtual void FreeTLS(PPCContext* context, u32 slot) = 0;
182
189 [[nodiscard]] virtual std::optional<GuestFiber> FindFiber(u32 address) = 0;
190
196 virtual void RegisterFiber(u32 address, const GuestFiber& info) = 0;
197
198 virtual void UnregisterFiber(u32 address) = 0;
199
206 [[nodiscard]] virtual const char* GetOrCreateFiberName(u32 fiber_uid,
207 const char* thread_name) = 0;
208
214 virtual void RegisterThreadNotify(u32 notification) = 0;
215
221 virtual void UnregisterThreadNotify(u32 notification) = 0;
222};
223
224} // namespace rex::kernel
225
226namespace rex::kernel {
227
233 X_RESULT result;
234 std::optional<u32> extended_error =
235 std::nullopt;
236 std::optional<u32> length = std::nullopt;
237};
238
241 virtual ~IOverlappedDispatcher() = default;
242
249 virtual void Complete(u32 overlapped, const CompletionOptions& options) = 0;
250
257 virtual void Publish(rex::MoveOnlyFunction<void()> publish) = 0;
258
264 virtual u32 Observe(rex::MoveOnlyFunction<u32()> observe) = 0;
265
276 virtual void CompleteDeferred(u32 overlapped, rex::MoveOnlyFunction<void()> work,
277 const CompletionOptions& options,
278 rex::MoveOnlyFunction<void()> pre = {},
279 rex::MoveOnlyFunction<void()> post = {}) = 0;
280
289 virtual void CompleteDeferred(u32 overlapped, rex::MoveOnlyFunction<CompletionOptions()> work,
290 rex::MoveOnlyFunction<void()> pre = {},
291 rex::MoveOnlyFunction<void()> post = {}) = 0;
292};
293
296 virtual ~IDPCQueue() = default;
297
306 virtual bool Queue(u32 dpc, u32 arg1, u32 arg2) = 0;
307
314 virtual bool Remove(u32 dpc) = 0;
315
320 virtual void Drain() = 0;
321};
322
323} // namespace rex::kernel
Definition utility.h:28
Definition kernel_thread.h:59
Definition thread_registry.h:295
Definition thread_registry.h:79
virtual rex::platform::WaitResult WaitHostSockets(std::span< const rex::platform::SocketWait > sockets, std::chrono::milliseconds timeout)=0
virtual X_STATUS WaitGuestObject(X_HANDLE handle, u32 wait_reason, u32 processor_mode, u32 alertable, u64 *opt_timeout)=0
virtual rex::platform::WaitResult WaitHostObject(rex::platform::WaitHandle *handle, std::chrono::milliseconds timeout)=0
virtual X_STATUS WaitGuestObjects(std::span< const X_HANDLE > handles, u32 wait_type, u32 wait_reason, u32 processor_mode, u32 alertable, u64 *opt_timeout)=0
Definition thread_registry.h:240
Definition thread_registry.h:129
Definition kernel_thread.h:72
Definition thread_registry.h:34
ThreadPin(KernelThread *thread) noexcept
Definition thread_registry.h:38
Definition sync.h:58
Interfaces, factory and module declaration.
#define REX_DECLARE_INTERFACE(Name, version)
Definition module.h:114
Definition export.h:24
u32 X_STATUS
Definition status.h:23
u32 X_RESULT
Definition status.h:92
WaitResult
Definition sync.h:49
Definition app.h:36
typename detail::MoveOnlyFunctionFor< Signature >::type MoveOnlyFunction
Definition utility.h:254
std::expected< T, Error > Result
Definition result.h:98
Platform flags, packing macros and export markers.
Definition context.h:387
Definition thread_registry.h:232
std::optional< u32 > length
Definition thread_registry.h:236
std::optional< u32 > extended_error
Definition thread_registry.h:234
Definition thread_registry.h:69
u32 guest_stack_bottom
Definition thread_registry.h:73
bool is_thread_fiber
Definition thread_registry.h:74
u32 uid
Definition thread_registry.h:71
Definition thread.h:307
Wait objects and wait functions.
Integer types, byte order wrappers and size literals.
Move only function, const propagating pointer and scope guard.