ReXGlue SDK
Loading...
Searching...
No Matches
object.h
Go to the documentation of this file.
1
13
14#pragma once
15
16#include <algorithm>
17#include <atomic>
18#include <chrono>
19#include <concepts>
20#include <cstddef>
21#include <format>
22#include <memory>
23#include <span>
24#include <string>
25#include <vector>
26
28#include <rex/platform.h>
29#include <rex/types.h>
30#include <rex/assert.h>
31#include <rex/memory/endian.h>
32#include <rex/platform/thread.h>
33#include <rex/platform/sync.h>
34#include <rex/kernel/heap.h>
36#include <rex/memory/host.h>
37
38namespace rex {
39namespace kernel {
40class TitleRuntime;
41} // namespace kernel
42} // namespace rex
43
44namespace rex::kernel {
45
47constexpr memory::FourCCT kStashSignature = memory::make_fourcc('R', 'E', 'X', '\0');
48
49enum class ObjectType : u8 {
50 kEvent,
51 kSemaphore,
52 kMutant,
53 kTimer,
54 kFile,
55 kNotifyListener,
56 kIOCompletion,
57 kSymbolicLink,
58 kModule,
59 kThread,
60 kXAM,
61};
62
64inline constexpr u32 kHandleBase = 0xF8000000;
65
72REX_KERNEL_API std::chrono::milliseconds TimeoutFromTicks(i64 timeout_ticks);
73
81REX_KERNEL_API std::chrono::microseconds DelayFromTicks(i64 delay_ticks);
82
91 u32 obj_attributes_ptr);
92
93template <typename T>
94class Pinned;
95class ObjectPin;
96
105REX_KERNEL_API X_STATUS Wait(TitleRuntime& runtime, const ObjectPin& object, u32 wait_reason,
106 u32 processor_mode, u32 alertable, u64* opt_timeout);
107
117REX_KERNEL_API X_STATUS WaitMultiple(TitleRuntime& runtime, std::span<const ObjectPin> objects,
118 u32 wait_type, u32 wait_reason, u32 processor_mode,
119 u32 alertable, u64* opt_timeout);
120
131 const ObjectPin& wait_object, u32 wait_reason,
132 u32 processor_mode, u32 alertable, u64* opt_timeout);
133
134} // namespace rex::kernel
135
137template <>
138struct std::formatter<rex::kernel::ObjectType> : std::formatter<rex::u32> {
139 auto format(rex::kernel::ObjectType value, std::format_context& ctx) const {
140 return std::formatter<rex::u32>::format(static_cast<rex::u32>(value), ctx);
141 }
142};
143
144namespace rex::kernel {
145
150class PinCount {
151 public:
152 void Pin() noexcept { count_.fetch_add(1, std::memory_order_acquire); }
153
159 [[nodiscard]] bool Unpin() noexcept {
160 return count_.fetch_sub(1, std::memory_order_acq_rel) - 1 == kRetired;
161 }
162
168 [[nodiscard]] bool Retire() noexcept {
169 return count_.fetch_or(kRetired, std::memory_order_acq_rel) == 0;
170 }
171
173 [[nodiscard]] u32 GetPins() const noexcept {
174 return count_.load(std::memory_order_acquire) & ~kRetired;
175 }
176
177 private:
178 static constexpr u32 kRetired = 0x80000000u;
179 std::atomic<u32> count_{0};
180};
181
189template <typename T>
190class PublishedPtr {
191 public:
192 PublishedPtr() = default;
193
195 PublishedPtr(std::unique_ptr<T> owner) { Replace(std::move(owner)); }
196
197 PublishedPtr& operator=(std::unique_ptr<T> owner) {
198 Replace(std::move(owner));
199 return *this;
200 }
201
203 T* get() noexcept { return current_.load(std::memory_order_acquire); }
204
205 const T* get() const noexcept { return current_.load(std::memory_order_acquire); }
206
207 T* operator->() noexcept { return get(); }
208
209 const T* operator->() const noexcept { return get(); }
210
211 explicit operator bool() const noexcept { return get() != nullptr; }
212
213 friend bool operator==(const PublishedPtr& value, std::nullptr_t) noexcept {
214 return value.get() == nullptr;
215 }
216
222 void Replace(std::unique_ptr<T> next) {
223 if (owner_) {
224 retired_.push_back(std::move(owner_));
225 }
226 owner_ = std::move(next);
227 current_.store(owner_.get(), std::memory_order_release);
228 }
229
235 [[nodiscard]] std::unique_ptr<T> Take() noexcept {
236 current_.store(nullptr, std::memory_order_relaxed);
237 return std::move(owner_);
238 }
239
240 private:
241 std::atomic<T*> current_{nullptr};
242 std::unique_ptr<T> owner_;
243 std::vector<std::unique_ptr<T>> retired_;
244};
245
246} // namespace rex::kernel
247
248namespace rex::kernel {
249
250inline constexpr memory::Tag kRuntime{2};
251
252} // namespace rex::kernel
Definition heap.h:594
Definition object_pin.h:29
Definition object.h:150
bool Retire() noexcept
Definition object.h:168
u32 GetPins() const noexcept
Definition object.h:173
bool Unpin() noexcept
Definition object.h:159
Definition object_pin.h:122
Definition object.h:190
std::unique_ptr< T > Take() noexcept
Definition object.h:235
T * get() noexcept
Definition object.h:203
PublishedPtr(std::unique_ptr< T > owner)
Definition object.h:195
void Replace(std::unique_ptr< T > next)
Definition object.h:222
Definition title_runtime.h:85
Definition export.h:24
X_STATUS Wait(TitleRuntime &runtime, const ObjectPin &object, u32 wait_reason, u32 processor_mode, u32 alertable, u64 *opt_timeout)
u32 X_STATUS
Definition status.h:23
X_STATUS SignalAndWait(TitleRuntime &runtime, const ObjectPin &signal_object, const ObjectPin &wait_object, u32 wait_reason, u32 processor_mode, u32 alertable, u64 *opt_timeout)
std::chrono::microseconds DelayFromTicks(i64 delay_ticks)
X_STATUS WaitMultiple(TitleRuntime &runtime, std::span< const ObjectPin > objects, u32 wait_type, u32 wait_reason, u32 processor_mode, u32 alertable, u64 *opt_timeout)
constexpr memory::FourCCT kStashSignature
Definition object.h:47
std::string NameFromAttributes(kernel::GuestAddressSpace *memory, u32 obj_attributes_ptr)
std::chrono::milliseconds TimeoutFromTicks(i64 timeout_ticks)
constexpr u32 kHandleBase
Definition object.h:64
Definition endian.h:27
u32 FourCCT
Definition endian.h:264
constexpr FourCCT make_fourcc(char a, char b, char c, char d)
Definition endian.h:273
Tag
Definition host.h:22
Definition app.h:36
Platform flags, packing macros and export markers.
#define REX_KERNEL_API
Definition platform.h:297
Wait objects and wait functions.
Integer types, byte order wrappers and size literals.