mirror of
https://github.com/RPCSX/rpcsx.git
synced 2026-04-05 22:47:03 +00:00
move IPC utilities from orbis-kernel to rx
This commit is contained in:
parent
30469f7fb9
commit
e73a0b962d
41 changed files with 558 additions and 172 deletions
|
|
@ -1,12 +1,13 @@
|
|||
#pragma once
|
||||
|
||||
#include "orbis-config.hpp"
|
||||
#include "rx/SharedMutex.hpp"
|
||||
#include "utils/BitSet.hpp"
|
||||
#include "utils/Rc.hpp"
|
||||
#include "utils/SharedMutex.hpp"
|
||||
#include <array>
|
||||
#include <cstring>
|
||||
#include <mutex>
|
||||
#include <span>
|
||||
#include <string_view>
|
||||
|
||||
namespace orbis {
|
||||
|
|
@ -132,7 +133,7 @@ public:
|
|||
[[nodiscard]] ProcessType processType() const { return mProcessType; }
|
||||
|
||||
private:
|
||||
mutable shared_mutex mMtx;
|
||||
mutable rx::shared_mutex mMtx;
|
||||
orbis::BitSet<static_cast<int>(BudgetResource::_count)> mUsed;
|
||||
ProcessType mProcessType{};
|
||||
BudgetList mList;
|
||||
|
|
|
|||
|
|
@ -6,11 +6,11 @@
|
|||
#include "ipmi.hpp"
|
||||
#include "orbis/note.hpp"
|
||||
#include "osem.hpp"
|
||||
#include "rx/SharedCV.hpp"
|
||||
#include "rx/SharedMutex.hpp"
|
||||
#include "thread/types.hpp"
|
||||
#include "utils/IdMap.hpp"
|
||||
#include "utils/LinkedNode.hpp"
|
||||
#include "utils/SharedCV.hpp"
|
||||
#include "utils/SharedMutex.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <mutex>
|
||||
|
|
@ -32,13 +32,13 @@ struct UmtxKey {
|
|||
|
||||
struct UmtxCond {
|
||||
Thread *thr;
|
||||
utils::shared_cv cv;
|
||||
rx::shared_cv cv;
|
||||
|
||||
UmtxCond(Thread *thr) : thr(thr) {}
|
||||
};
|
||||
|
||||
struct UmtxChain {
|
||||
utils::shared_mutex mtx;
|
||||
rx::shared_mutex mtx;
|
||||
using queue_type = utils::kmultimap<UmtxKey, UmtxCond>;
|
||||
queue_type sleep_queue;
|
||||
queue_type spare_queue;
|
||||
|
|
@ -158,7 +158,7 @@ public:
|
|||
}
|
||||
|
||||
std::tuple<utils::kmap<utils::kstring, char[128]> &,
|
||||
std::unique_lock<shared_mutex>>
|
||||
std::unique_lock<rx::shared_mutex>>
|
||||
getKernelEnv() {
|
||||
std::unique_lock lock(m_kenv_mtx);
|
||||
return {m_kenv, std::move(lock)};
|
||||
|
|
@ -178,7 +178,7 @@ public:
|
|||
};
|
||||
|
||||
// Use getUmtxChain0 or getUmtxChain1
|
||||
std::tuple<UmtxChain &, UmtxKey, std::unique_lock<shared_mutex>>
|
||||
std::tuple<UmtxChain &, UmtxKey, std::unique_lock<rx::shared_mutex>>
|
||||
getUmtxChainIndexed(int i, Thread *t, uint32_t flags, void *ptr);
|
||||
|
||||
// Internal Umtx: Wait/Cv/Sem
|
||||
|
|
@ -197,7 +197,7 @@ public:
|
|||
Ref<RcBase> blockpoolDevice;
|
||||
Ref<RcBase> gpuDevice;
|
||||
Ref<RcBase> dceDevice;
|
||||
shared_mutex gpuDeviceMtx;
|
||||
rx::shared_mutex gpuDeviceMtx;
|
||||
uint sdkVersion{};
|
||||
uint fwSdkVersion{};
|
||||
uint safeMode{};
|
||||
|
|
@ -206,7 +206,7 @@ public:
|
|||
RcIdMap<Budget, sint, 4097, 1> budgets;
|
||||
Ref<Budget> processTypeBudgets[4];
|
||||
|
||||
shared_mutex regMgrMtx;
|
||||
rx::shared_mutex regMgrMtx;
|
||||
kmap<std::uint32_t, std::uint32_t> regMgrInt;
|
||||
std::vector<std::tuple<std::uint8_t *, size_t>> dialogs{};
|
||||
|
||||
|
|
@ -227,8 +227,8 @@ public:
|
|||
}
|
||||
|
||||
private:
|
||||
shared_mutex m_heap_mtx;
|
||||
shared_mutex m_heap_map_mtx;
|
||||
rx::shared_mutex m_heap_mtx;
|
||||
rx::shared_mutex m_heap_map_mtx;
|
||||
void *m_heap_next = this + 1;
|
||||
|
||||
utils::kmultimap<std::size_t, void *> m_free_heap;
|
||||
|
|
@ -238,21 +238,21 @@ private:
|
|||
|
||||
std::atomic<long> m_tsc_freq{0};
|
||||
|
||||
shared_mutex m_thread_id_mtx;
|
||||
rx::shared_mutex m_thread_id_mtx;
|
||||
OwningIdMap<char, long, 256, 0> m_thread_id_map;
|
||||
mutable shared_mutex m_proc_mtx;
|
||||
mutable rx::shared_mutex m_proc_mtx;
|
||||
utils::LinkedNode<Process> *m_processes = nullptr;
|
||||
|
||||
shared_mutex m_evf_mtx;
|
||||
rx::shared_mutex m_evf_mtx;
|
||||
utils::kmap<utils::kstring, Ref<EventFlag>> m_event_flags;
|
||||
|
||||
shared_mutex m_sem_mtx;
|
||||
rx::shared_mutex m_sem_mtx;
|
||||
utils::kmap<utils::kstring, Ref<Semaphore>> m_semaphores;
|
||||
|
||||
shared_mutex mIpmiServerMtx;
|
||||
rx::shared_mutex mIpmiServerMtx;
|
||||
utils::kmap<utils::kstring, Ref<IpmiServer>> mIpmiServers;
|
||||
|
||||
shared_mutex m_kenv_mtx;
|
||||
rx::shared_mutex m_kenv_mtx;
|
||||
utils::kmap<utils::kstring, char[128]> m_kenv; // max size: 127 + '\0'
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -1,12 +1,12 @@
|
|||
#pragma once
|
||||
#include "file.hpp"
|
||||
#include "note.hpp"
|
||||
#include "utils/SharedCV.hpp"
|
||||
#include "rx/SharedCV.hpp"
|
||||
#include <list>
|
||||
|
||||
namespace orbis {
|
||||
struct KQueue : orbis::File {
|
||||
shared_cv cv;
|
||||
rx::shared_cv cv;
|
||||
kstring name;
|
||||
kvector<KEvent> triggeredEvents;
|
||||
std::list<KNote, kallocator<KNote>> notes;
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
#pragma once
|
||||
#include "KernelAllocator.hpp"
|
||||
#include "thread/Thread.hpp"
|
||||
#include "utils/SharedMutex.hpp"
|
||||
#include "rx/SharedMutex.hpp"
|
||||
#include <atomic>
|
||||
|
||||
namespace orbis {
|
||||
|
|
@ -56,7 +56,7 @@ struct EventFlag final {
|
|||
}
|
||||
};
|
||||
|
||||
utils::shared_mutex queueMtx;
|
||||
rx::shared_mutex queueMtx;
|
||||
utils::kvector<WaitingThread> waitingThreads;
|
||||
|
||||
enum class NotifyType { Set, Cancel, Destroy };
|
||||
|
|
@ -80,7 +80,7 @@ struct EventFlag final {
|
|||
std::size_t set(std::uint64_t bits) { return notify(NotifyType::Set, bits); }
|
||||
|
||||
void clear(std::uint64_t bits) {
|
||||
writer_lock lock(queueMtx);
|
||||
rx::writer_lock lock(queueMtx);
|
||||
value.fetch_and(bits, std::memory_order::relaxed);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -3,9 +3,9 @@
|
|||
#include "KernelAllocator.hpp"
|
||||
#include "error/ErrorCode.hpp"
|
||||
#include "note.hpp"
|
||||
#include "rx/SharedMutex.hpp"
|
||||
#include "stat.hpp"
|
||||
#include "utils/Rc.hpp"
|
||||
#include "utils/SharedMutex.hpp"
|
||||
#include <cstdint>
|
||||
|
||||
namespace orbis {
|
||||
|
|
@ -74,7 +74,7 @@ struct FileOps {
|
|||
};
|
||||
|
||||
struct File : RcBase {
|
||||
shared_mutex mtx;
|
||||
rx::shared_mutex mtx;
|
||||
Ref<EventEmitter> event;
|
||||
const FileOps *ops = nullptr;
|
||||
Ref<RcBase> device;
|
||||
|
|
|
|||
|
|
@ -3,8 +3,8 @@
|
|||
#include "KernelAllocator.hpp"
|
||||
#include "evf.hpp"
|
||||
#include "orbis-config.hpp"
|
||||
#include "orbis/utils/SharedCV.hpp"
|
||||
#include "orbis/utils/SharedMutex.hpp"
|
||||
#include "rx/SharedCV.hpp"
|
||||
#include "rx/SharedMutex.hpp"
|
||||
#include "utils/Rc.hpp"
|
||||
#include <list>
|
||||
#include <optional>
|
||||
|
|
@ -43,8 +43,8 @@ struct IpmiServer : RcBase {
|
|||
ptr<void> serverImpl;
|
||||
ptr<void> eventHandler;
|
||||
ptr<void> userData;
|
||||
shared_mutex mutex;
|
||||
shared_cv receiveCv;
|
||||
rx::shared_mutex mutex;
|
||||
rx::shared_cv receiveCv;
|
||||
sint pid;
|
||||
kdeque<Packet> packets;
|
||||
std::list<ConnectionRequest, kallocator<ConnectionRequest>>
|
||||
|
|
@ -55,7 +55,7 @@ struct IpmiServer : RcBase {
|
|||
|
||||
struct IpmiClient : RcBase {
|
||||
struct MessageQueue {
|
||||
shared_cv messageCv;
|
||||
rx::shared_cv messageCv;
|
||||
kdeque<kvector<std::byte>> messages;
|
||||
};
|
||||
|
||||
|
|
@ -69,10 +69,10 @@ struct IpmiClient : RcBase {
|
|||
ptr<void> clientImpl;
|
||||
ptr<void> userData;
|
||||
Ref<IpmiSession> session;
|
||||
shared_mutex mutex;
|
||||
shared_cv sessionCv;
|
||||
shared_cv asyncResponseCv;
|
||||
shared_cv connectCv;
|
||||
rx::shared_mutex mutex;
|
||||
rx::shared_cv sessionCv;
|
||||
rx::shared_cv asyncResponseCv;
|
||||
rx::shared_cv connectCv;
|
||||
std::optional<sint> connectionStatus{};
|
||||
Process *process;
|
||||
kdeque<MessageQueue> messageQueues;
|
||||
|
|
@ -93,8 +93,8 @@ struct IpmiSession : RcBase {
|
|||
ptr<void> userData;
|
||||
Ref<IpmiClient> client;
|
||||
Ref<IpmiServer> server;
|
||||
shared_mutex mutex;
|
||||
shared_cv responseCv;
|
||||
rx::shared_mutex mutex;
|
||||
rx::shared_cv responseCv;
|
||||
kdeque<SyncResponse> syncResponses;
|
||||
uint expectedOutput{0};
|
||||
};
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
#include "KernelAllocator.hpp"
|
||||
#include "orbis-config.hpp"
|
||||
#include "orbis/utils/Rc.hpp"
|
||||
#include "utils/SharedMutex.hpp"
|
||||
#include "rx/SharedMutex.hpp"
|
||||
#include <limits>
|
||||
#include <set>
|
||||
|
||||
|
|
@ -75,7 +75,7 @@ struct KEvent {
|
|||
struct EventEmitter;
|
||||
struct KQueue;
|
||||
struct KNote {
|
||||
shared_mutex mutex;
|
||||
rx::shared_mutex mutex;
|
||||
KQueue *queue;
|
||||
Ref<File> file;
|
||||
KEvent event{};
|
||||
|
|
@ -88,7 +88,7 @@ struct KNote {
|
|||
};
|
||||
|
||||
struct EventEmitter : orbis::RcBase {
|
||||
shared_mutex mutex;
|
||||
rx::shared_mutex mutex;
|
||||
std::set<KNote *, std::less<>, kallocator<KNote *>> notes;
|
||||
|
||||
void emit(sshort filter, uint fflags = 0, intptr_t data = 0,
|
||||
|
|
|
|||
|
|
@ -1,11 +1,10 @@
|
|||
#pragma once
|
||||
|
||||
#include "KernelAllocator.hpp"
|
||||
#include "thread/Thread.hpp"
|
||||
#include "utils/SharedCV.hpp"
|
||||
#include "utils/SharedMutex.hpp"
|
||||
#include "orbis-config.hpp"
|
||||
#include "rx/SharedCV.hpp"
|
||||
#include "rx/SharedMutex.hpp"
|
||||
#include <atomic>
|
||||
#include <condition_variable>
|
||||
|
||||
namespace orbis {
|
||||
enum {
|
||||
|
|
@ -22,8 +21,8 @@ struct Semaphore final {
|
|||
std::atomic<unsigned> references{0};
|
||||
std::atomic<sint> value;
|
||||
const sint maxValue;
|
||||
utils::shared_mutex mtx;
|
||||
utils::shared_cv cond;
|
||||
rx::shared_mutex mtx;
|
||||
rx::shared_cv cond;
|
||||
|
||||
Semaphore(uint attrs, sint value, sint max)
|
||||
: attrs(attrs), value(value), maxValue(max) {}
|
||||
|
|
|
|||
|
|
@ -2,14 +2,14 @@
|
|||
|
||||
#include "KernelAllocator.hpp"
|
||||
#include "file.hpp"
|
||||
#include "rx/SharedCV.hpp"
|
||||
#include "rx/SharedMutex.hpp"
|
||||
#include "utils/Rc.hpp"
|
||||
#include "utils/SharedCV.hpp"
|
||||
#include "utils/SharedMutex.hpp"
|
||||
#include <utility>
|
||||
|
||||
namespace orbis {
|
||||
struct Pipe final : File {
|
||||
shared_cv cv;
|
||||
rx::shared_cv cv;
|
||||
kvector<std::byte> data;
|
||||
Ref<Pipe> other;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@
|
|||
#include "orbis/file.hpp"
|
||||
#include "orbis/module/Module.hpp"
|
||||
#include "orbis/utils/IdMap.hpp"
|
||||
#include "orbis/utils/SharedMutex.hpp"
|
||||
#include "rx/SharedMutex.hpp"
|
||||
#include <optional>
|
||||
|
||||
namespace orbis {
|
||||
|
|
@ -59,7 +59,7 @@ struct Process final {
|
|||
sysentvec *sysent = nullptr;
|
||||
ProcessState state = ProcessState::NEW;
|
||||
Process *parentProcess = nullptr;
|
||||
shared_mutex mtx;
|
||||
rx::shared_mutex mtx;
|
||||
int vmId = -1;
|
||||
ProcessType type = ProcessType::FreeBsd;
|
||||
void (*onSysEnter)(Thread *thread, int id, uint64_t *args,
|
||||
|
|
@ -92,14 +92,14 @@ struct Process final {
|
|||
utils::RcIdMap<orbis::File, sint> fileDescriptors;
|
||||
|
||||
// Named objects for debugging
|
||||
utils::shared_mutex namedObjMutex;
|
||||
rx::shared_mutex namedObjMutex;
|
||||
utils::kmap<void *, utils::kstring> namedObjNames;
|
||||
utils::OwningIdMap<NamedObjInfo, uint, 65535, 1> namedObjIds;
|
||||
|
||||
utils::kmap<std::int32_t, SigAction> sigActions;
|
||||
|
||||
// Named memory ranges for debugging
|
||||
utils::shared_mutex namedMemMutex;
|
||||
rx::shared_mutex namedMemMutex;
|
||||
utils::kmap<NamedMemoryRange, utils::kstring> namedMem;
|
||||
};
|
||||
} // namespace orbis
|
||||
|
|
|
|||
|
|
@ -7,9 +7,9 @@
|
|||
|
||||
#include "../KernelAllocator.hpp"
|
||||
#include "../ucontext.hpp"
|
||||
#include "../utils/SharedAtomic.hpp"
|
||||
#include "../utils/SharedCV.hpp"
|
||||
#include "../utils/SharedMutex.hpp"
|
||||
#include "rx/SharedAtomic.hpp"
|
||||
#include "rx/SharedCV.hpp"
|
||||
#include "rx/SharedMutex.hpp"
|
||||
#include <thread>
|
||||
|
||||
namespace orbis {
|
||||
|
|
@ -17,7 +17,7 @@ struct Process;
|
|||
|
||||
static constexpr std::uint32_t kThreadSuspendFlag = 1 << 31;
|
||||
struct Thread {
|
||||
utils::shared_mutex mtx;
|
||||
rx::shared_mutex mtx;
|
||||
Process *tproc = nullptr;
|
||||
uint64_t retval[2]{};
|
||||
void *context{};
|
||||
|
|
@ -34,14 +34,14 @@ struct Thread {
|
|||
.type = 2,
|
||||
.prio = 10,
|
||||
};
|
||||
utils::shared_mutex suspend_mtx;
|
||||
utils::shared_cv suspend_cv;
|
||||
rx::shared_mutex suspend_mtx;
|
||||
rx::shared_cv suspend_cv;
|
||||
kvector<UContext> sigReturns;
|
||||
kvector<SigInfo> blockedSignals;
|
||||
kvector<SigInfo> queuedSignals;
|
||||
shared_atomic32 suspendFlags{0};
|
||||
rx::shared_atomic32 suspendFlags{0};
|
||||
|
||||
utils::shared_atomic32 interruptedMtx{0};
|
||||
rx::shared_atomic32 interruptedMtx{0};
|
||||
|
||||
std::int64_t hostTid = -1;
|
||||
lwpid_t tid = -1;
|
||||
|
|
@ -51,7 +51,7 @@ struct Thread {
|
|||
std::thread::native_handle_type nativeHandle;
|
||||
|
||||
// Used to wake up thread in sleep queue
|
||||
utils::shared_cv sync_cv;
|
||||
rx::shared_cv sync_cv;
|
||||
uint64_t evfResultPattern;
|
||||
uint64_t evfIsCancelled;
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
#include "BitSet.hpp"
|
||||
#include "Rc.hpp"
|
||||
#include "orbis/utils/SharedMutex.hpp"
|
||||
#include "rx/SharedMutex.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <bit>
|
||||
|
|
@ -66,7 +66,7 @@ class RcIdMap {
|
|||
public:
|
||||
static constexpr auto npos = static_cast<IdT>(~static_cast<std::size_t>(0));
|
||||
|
||||
mutable shared_mutex mutex;
|
||||
mutable rx::shared_mutex mutex;
|
||||
|
||||
struct end_iterator {};
|
||||
|
||||
|
|
|
|||
|
|
@ -1,167 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <limits>
|
||||
#include <system_error>
|
||||
#include <thread>
|
||||
#include <type_traits>
|
||||
|
||||
namespace orbis {
|
||||
inline void yield() { std::this_thread::yield(); }
|
||||
inline void relax() {
|
||||
#if defined(__GNUC__) && (defined __i386__ || defined __x86_64__)
|
||||
__builtin_ia32_pause();
|
||||
#else
|
||||
yield();
|
||||
#endif
|
||||
}
|
||||
|
||||
static constexpr auto kRelaxSpinCount = 12;
|
||||
static constexpr auto kSpinCount = 16;
|
||||
|
||||
inline namespace utils {
|
||||
inline thread_local bool (*g_scopedUnblock)(bool) = nullptr;
|
||||
|
||||
bool try_spin_wait(auto &&pred) {
|
||||
for (std::size_t i = 0; i < kSpinCount; ++i) {
|
||||
if (pred()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (i < kRelaxSpinCount) {
|
||||
relax();
|
||||
} else {
|
||||
yield();
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool spin_wait(auto &&pred, auto &&spinCond) {
|
||||
if (try_spin_wait(pred)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
while (spinCond()) {
|
||||
if (pred()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
struct shared_atomic32 : std::atomic<std::uint32_t> {
|
||||
using atomic::atomic;
|
||||
using atomic::operator=;
|
||||
|
||||
template <typename Clock, typename Dur>
|
||||
std::errc wait(std::uint32_t oldValue,
|
||||
std::chrono::time_point<Clock, Dur> timeout) {
|
||||
if (try_spin_wait(
|
||||
[&] { return load(std::memory_order::acquire) != oldValue; })) {
|
||||
return {};
|
||||
}
|
||||
|
||||
auto now = Clock::now();
|
||||
|
||||
if (timeout < now) {
|
||||
return std::errc::timed_out;
|
||||
}
|
||||
|
||||
return wait_impl(
|
||||
oldValue,
|
||||
std::chrono::duration_cast<std::chrono::microseconds>(timeout - now));
|
||||
}
|
||||
|
||||
std::errc wait(std::uint32_t oldValue,
|
||||
std::chrono::microseconds usec_timeout) {
|
||||
return wait_impl(oldValue, usec_timeout);
|
||||
}
|
||||
|
||||
std::errc wait(std::uint32_t oldValue) {
|
||||
if (try_spin_wait(
|
||||
[&] { return load(std::memory_order::acquire) != oldValue; })) {
|
||||
return {};
|
||||
}
|
||||
|
||||
return wait_impl(oldValue);
|
||||
}
|
||||
|
||||
auto wait(auto &fn) -> decltype(fn(std::declval<std::uint32_t &>())) {
|
||||
while (true) {
|
||||
std::uint32_t lastValue;
|
||||
if (try_spin_wait([&] {
|
||||
lastValue = load(std::memory_order::acquire);
|
||||
return fn(lastValue);
|
||||
})) {
|
||||
return;
|
||||
}
|
||||
|
||||
while (wait_impl(lastValue) != std::errc{}) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int notify_one() const { return notify_n(1); }
|
||||
int notify_all() const { return notify_n(std::numeric_limits<int>::max()); }
|
||||
|
||||
int notify_n(int count) const;
|
||||
|
||||
// Atomic operation; returns old value, or pair of old value and return value
|
||||
// (cancel op if evaluates to false)
|
||||
template <typename F, typename RT = std::invoke_result_t<F, std::uint32_t &>>
|
||||
std::conditional_t<std::is_void_v<RT>, std::uint32_t,
|
||||
std::pair<std::uint32_t, RT>>
|
||||
fetch_op(F &&func) {
|
||||
std::uint32_t _new;
|
||||
std::uint32_t old = load(std::memory_order::relaxed);
|
||||
while (true) {
|
||||
_new = old;
|
||||
if constexpr (std::is_void_v<RT>) {
|
||||
std::invoke(std::forward<F>(func), _new);
|
||||
if (compare_exchange_strong(old, _new)) [[likely]] {
|
||||
return old;
|
||||
}
|
||||
} else {
|
||||
RT ret = std::invoke(std::forward<F>(func), _new);
|
||||
if (!ret || compare_exchange_strong(old, _new)) [[likely]] {
|
||||
return {old, std::move(ret)};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Atomic operation; returns function result value
|
||||
template <typename F, typename RT = std::invoke_result_t<F, std::uint32_t &>>
|
||||
RT op(F &&func) {
|
||||
std::uint32_t _new;
|
||||
std::uint32_t old = load(std::memory_order::relaxed);
|
||||
|
||||
while (true) {
|
||||
_new = old;
|
||||
if constexpr (std::is_void_v<RT>) {
|
||||
std::invoke(std::forward<F>(func), _new);
|
||||
if (compare_exchange_strong(old, _new)) [[likely]] {
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
RT result = std::invoke(std::forward<F>(func), _new);
|
||||
if (compare_exchange_strong(old, _new)) [[likely]] {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
[[nodiscard]] std::errc wait_impl(std::uint32_t oldValue,
|
||||
std::chrono::microseconds usec_timeout =
|
||||
std::chrono::microseconds::max());
|
||||
};
|
||||
} // namespace utils
|
||||
} // namespace orbis
|
||||
|
|
@ -1,92 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include "orbis/utils/SharedAtomic.hpp"
|
||||
#include <chrono>
|
||||
#include <cstdint>
|
||||
#include <mutex>
|
||||
#include <orbis/utils/AtomicOp.hpp>
|
||||
#include <orbis/utils/SharedMutex.hpp>
|
||||
#include <system_error>
|
||||
|
||||
namespace orbis {
|
||||
inline namespace utils {
|
||||
// IPC-ready lightweight condition variable
|
||||
class shared_cv final {
|
||||
enum : unsigned {
|
||||
c_waiter_mask = 0xffff,
|
||||
c_signal_mask = 0x7fff0000,
|
||||
#ifdef ORBIS_HAS_FUTEX
|
||||
c_locked_mask = 0x80000000,
|
||||
#endif
|
||||
c_signal_one = c_waiter_mask + 1,
|
||||
};
|
||||
|
||||
shared_atomic32 m_value{0};
|
||||
|
||||
protected:
|
||||
// Increment waiter count
|
||||
unsigned add_waiter() noexcept {
|
||||
return m_value.op([](unsigned &value) -> unsigned {
|
||||
if ((value & c_signal_mask) == c_signal_mask ||
|
||||
(value & c_waiter_mask) == c_waiter_mask) {
|
||||
// Signal or waiter overflow, return immediately
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Add waiter (c_waiter_mask)
|
||||
value += 1;
|
||||
return value;
|
||||
});
|
||||
}
|
||||
|
||||
// Internal waiting function
|
||||
std::errc impl_wait(shared_mutex &mutex, unsigned _val,
|
||||
std::uint64_t usec_timeout) noexcept;
|
||||
|
||||
// Try to notify up to _count threads
|
||||
void impl_wake(shared_mutex &mutex, int _count) noexcept;
|
||||
|
||||
public:
|
||||
constexpr shared_cv() = default;
|
||||
|
||||
std::errc
|
||||
wait(std::unique_lock<shared_mutex> &lock,
|
||||
std::chrono::microseconds timeout = std::chrono::microseconds::max()) {
|
||||
return wait(*lock.mutex(), timeout.count());
|
||||
}
|
||||
|
||||
template <typename Rep, typename Period>
|
||||
std::errc wait(std::unique_lock<shared_mutex> &lock,
|
||||
std::chrono::duration<Rep, Period> timeout) {
|
||||
return wait(
|
||||
lock,
|
||||
std::chrono::duration_cast<std::chrono::microseconds>(timeout).count());
|
||||
}
|
||||
|
||||
std::errc wait(shared_mutex &mutex,
|
||||
std::uint64_t usec_timeout = -1) noexcept {
|
||||
const unsigned _val = add_waiter();
|
||||
if (!_val) {
|
||||
return {};
|
||||
}
|
||||
|
||||
mutex.unlock();
|
||||
return impl_wait(mutex, _val, usec_timeout);
|
||||
}
|
||||
|
||||
// Wake one thread
|
||||
void notify_one(shared_mutex &mutex) noexcept {
|
||||
if (m_value) {
|
||||
impl_wake(mutex, 1);
|
||||
}
|
||||
}
|
||||
|
||||
// Wake all threads
|
||||
void notify_all(shared_mutex &mutex) noexcept {
|
||||
if (m_value) {
|
||||
impl_wake(mutex, INT_MAX);
|
||||
}
|
||||
}
|
||||
};
|
||||
} // namespace utils
|
||||
} // namespace orbis
|
||||
|
|
@ -1,151 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <atomic>
|
||||
#include <orbis/utils/AtomicOp.hpp>
|
||||
#include <orbis/utils/SharedAtomic.hpp>
|
||||
#include <system_error>
|
||||
|
||||
namespace orbis {
|
||||
inline namespace utils {
|
||||
// IPC-ready shared mutex, using only writer lock is recommended
|
||||
class shared_mutex final {
|
||||
friend class shared_cv;
|
||||
|
||||
enum : unsigned {
|
||||
c_one = 1u << 14, // Fixed-point 1.0 value (one writer)
|
||||
c_sig = 1u << 30,
|
||||
c_err = 1u << 31,
|
||||
};
|
||||
|
||||
shared_atomic32 m_value{};
|
||||
|
||||
void impl_lock_shared(unsigned val);
|
||||
void impl_unlock_shared(unsigned old);
|
||||
std::errc impl_wait();
|
||||
void impl_signal();
|
||||
void impl_lock(unsigned val);
|
||||
void impl_unlock(unsigned old);
|
||||
void impl_lock_upgrade();
|
||||
|
||||
public:
|
||||
constexpr shared_mutex() = default;
|
||||
|
||||
bool try_lock_shared() {
|
||||
// Conditional increment
|
||||
unsigned value = m_value.load();
|
||||
return value < c_one - 1 &&
|
||||
m_value.compare_exchange_strong(value, value + 1);
|
||||
}
|
||||
|
||||
// Lock with HLE acquire hint
|
||||
void lock_shared() {
|
||||
unsigned value = m_value.load();
|
||||
if (value < c_one - 1) [[likely]] {
|
||||
unsigned old = value;
|
||||
if (compare_exchange_hle_acq(m_value, old, value + 1)) [[likely]] {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
impl_lock_shared(value + 1);
|
||||
}
|
||||
|
||||
// Unlock with HLE release hint
|
||||
void unlock_shared() {
|
||||
const unsigned value = fetch_add_hle_rel(m_value, -1u);
|
||||
if (value >= c_one) [[unlikely]] {
|
||||
impl_unlock_shared(value);
|
||||
}
|
||||
}
|
||||
|
||||
bool try_lock() {
|
||||
unsigned value = 0;
|
||||
return m_value.compare_exchange_strong(value, c_one);
|
||||
}
|
||||
|
||||
// Lock with HLE acquire hint
|
||||
void lock() {
|
||||
unsigned value = 0;
|
||||
if (!compare_exchange_hle_acq(m_value, value, +c_one)) [[unlikely]] {
|
||||
impl_lock(value);
|
||||
}
|
||||
}
|
||||
|
||||
// Unlock with HLE release hint
|
||||
void unlock() {
|
||||
const unsigned value = fetch_add_hle_rel(m_value, 0u - c_one);
|
||||
if (value != c_one) [[unlikely]] {
|
||||
impl_unlock(value);
|
||||
}
|
||||
}
|
||||
|
||||
bool try_lock_upgrade() {
|
||||
unsigned value = m_value.load();
|
||||
|
||||
// Conditional increment, try to convert a single reader into a writer,
|
||||
// ignoring other writers
|
||||
return (value + c_one - 1) % c_one == 0 &&
|
||||
m_value.compare_exchange_strong(value, value + c_one - 1);
|
||||
}
|
||||
|
||||
void lock_upgrade() {
|
||||
if (!try_lock_upgrade()) [[unlikely]] {
|
||||
impl_lock_upgrade();
|
||||
}
|
||||
}
|
||||
|
||||
void lock_downgrade() {
|
||||
// Convert to reader lock (can result in broken state)
|
||||
m_value -= c_one - 1;
|
||||
}
|
||||
|
||||
// Check whether can immediately obtain an exclusive (writer) lock
|
||||
[[nodiscard]] bool is_free() const { return m_value.load() == 0; }
|
||||
|
||||
// Check whether can immediately obtain a shared (reader) lock
|
||||
[[nodiscard]] bool is_lockable() const { return m_value.load() < c_one - 1; }
|
||||
|
||||
private:
|
||||
// For CV
|
||||
bool lock_forced(int count = 1);
|
||||
};
|
||||
|
||||
// Simplified shared (reader) lock implementation.
|
||||
class reader_lock final {
|
||||
shared_mutex &m_mutex;
|
||||
bool m_upgraded = false;
|
||||
|
||||
public:
|
||||
reader_lock(const reader_lock &) = delete;
|
||||
reader_lock &operator=(const reader_lock &) = delete;
|
||||
explicit reader_lock(shared_mutex &mutex) : m_mutex(mutex) {
|
||||
m_mutex.lock_shared();
|
||||
}
|
||||
|
||||
// One-way lock upgrade; note that the observed state could have been changed
|
||||
void upgrade() {
|
||||
if (!m_upgraded) {
|
||||
m_mutex.lock_upgrade();
|
||||
m_upgraded = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Try to upgrade; if it succeeds, the observed state has NOT been changed
|
||||
bool try_upgrade() {
|
||||
return m_upgraded || (m_upgraded = m_mutex.try_lock_upgrade());
|
||||
}
|
||||
|
||||
~reader_lock() { m_upgraded ? m_mutex.unlock() : m_mutex.unlock_shared(); }
|
||||
};
|
||||
|
||||
class writer_lock final {
|
||||
shared_mutex &m_mutex;
|
||||
|
||||
public:
|
||||
writer_lock(const writer_lock &) = delete;
|
||||
writer_lock &operator=(const writer_lock &) = delete;
|
||||
explicit writer_lock(shared_mutex &mutex) : m_mutex(mutex) { m_mutex.lock(); }
|
||||
~writer_lock() { m_mutex.unlock(); }
|
||||
};
|
||||
} // namespace utils
|
||||
} // namespace orbis
|
||||
Loading…
Add table
Add a link
Reference in a new issue