mirror of
https://github.com/RPCSX/rpcsx.git
synced 2026-04-20 22:05:12 +00:00
orbis-kernel: Add SharedAtomic utility
Initial shared atomic implementation for Darwin
This commit is contained in:
parent
d83a0723a7
commit
7d0f277ad5
10 changed files with 578 additions and 97 deletions
|
|
@ -1,35 +1,30 @@
|
|||
#include "orbis/utils/SharedCV.hpp"
|
||||
#include "orbis/utils/Logs.hpp"
|
||||
#include <chrono>
|
||||
|
||||
#ifdef ORBIS_HAS_FUTEX
|
||||
#include <linux/futex.h>
|
||||
#include <syscall.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
namespace orbis::utils {
|
||||
int shared_cv::impl_wait(shared_mutex &mutex, unsigned _val,
|
||||
std::uint64_t usec_timeout) noexcept {
|
||||
std::errc shared_cv::impl_wait(shared_mutex &mutex, unsigned _val,
|
||||
std::uint64_t usec_timeout) noexcept {
|
||||
// Not supposed to fail
|
||||
if (!_val) {
|
||||
std::abort();
|
||||
}
|
||||
|
||||
// Wait with timeout
|
||||
struct timespec timeout {};
|
||||
timeout.tv_nsec = (usec_timeout % 1000'000) * 1000;
|
||||
timeout.tv_sec = (usec_timeout / 1000'000);
|
||||
|
||||
int result = 0;
|
||||
std::errc result = {};
|
||||
|
||||
while (true) {
|
||||
result = syscall(SYS_futex, &m_value, FUTEX_WAIT, _val,
|
||||
usec_timeout + 1 ? &timeout : nullptr, 0, 0);
|
||||
if (result < 0) {
|
||||
result = errno;
|
||||
}
|
||||
result = m_value.wait(_val, std::chrono::microseconds(usec_timeout));
|
||||
|
||||
// Cleanup
|
||||
const auto old = atomic_fetch_op(m_value, [&](unsigned &value) {
|
||||
const auto old = m_value.fetch_op([&](unsigned &value) {
|
||||
// Remove waiter if no signals
|
||||
if (!(value & ~c_waiter_mask) && result != EAGAIN) {
|
||||
if (!(value & ~c_waiter_mask) &&
|
||||
result != std::errc::resource_unavailable_try_again) {
|
||||
value -= 1;
|
||||
}
|
||||
|
||||
|
|
@ -38,23 +33,32 @@ int shared_cv::impl_wait(shared_mutex &mutex, unsigned _val,
|
|||
value -= c_signal_one;
|
||||
}
|
||||
|
||||
#ifdef ORBIS_HAS_FUTEX
|
||||
if (value & c_locked_mask) {
|
||||
value -= c_locked_mask;
|
||||
}
|
||||
#endif
|
||||
});
|
||||
|
||||
#ifdef ORBIS_HAS_FUTEX
|
||||
// Lock is already acquired
|
||||
if (old & c_locked_mask) {
|
||||
return 0;
|
||||
return {};
|
||||
}
|
||||
|
||||
// Wait directly (waiter has been added)
|
||||
if (old & c_signal_mask) {
|
||||
return mutex.impl_wait();
|
||||
}
|
||||
#else
|
||||
if (old & c_signal_mask) {
|
||||
result = {};
|
||||
break;
|
||||
}
|
||||
#endif
|
||||
|
||||
// Possibly spurious wakeup
|
||||
if (result != EAGAIN) {
|
||||
if (result != std::errc::resource_unavailable_try_again) {
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
@ -66,27 +70,69 @@ int shared_cv::impl_wait(shared_mutex &mutex, unsigned _val,
|
|||
}
|
||||
|
||||
void shared_cv::impl_wake(shared_mutex &mutex, int _count) noexcept {
|
||||
unsigned _old = m_value.load();
|
||||
const bool is_one = _count == 1;
|
||||
#ifdef ORBIS_HAS_FUTEX
|
||||
while (true) {
|
||||
unsigned _old = m_value.load();
|
||||
const bool is_one = _count == 1;
|
||||
|
||||
// Enqueue _count waiters
|
||||
// Enqueue _count waiters
|
||||
_count = std::min<int>(_count, _old & c_waiter_mask);
|
||||
if (_count <= 0)
|
||||
return;
|
||||
|
||||
// Try to lock the mutex
|
||||
const bool locked = mutex.lock_forced(_count);
|
||||
|
||||
const int max_sig = m_value.op([&](unsigned &value) {
|
||||
// Verify the number of waiters
|
||||
int max_sig = std::min<int>(_count, value & c_waiter_mask);
|
||||
|
||||
// Add lock signal (mutex was immediately locked)
|
||||
if (locked && max_sig)
|
||||
value |= c_locked_mask;
|
||||
else if (locked)
|
||||
std::abort();
|
||||
|
||||
// Add normal signals
|
||||
value += c_signal_one * max_sig;
|
||||
|
||||
// Remove waiters
|
||||
value -= max_sig;
|
||||
_old = value;
|
||||
return max_sig;
|
||||
});
|
||||
|
||||
if (max_sig < _count) {
|
||||
// Fixup mutex
|
||||
mutex.lock_forced(max_sig - _count);
|
||||
_count = max_sig;
|
||||
}
|
||||
|
||||
if (_count) {
|
||||
// Wake up one thread + requeue remaining waiters
|
||||
unsigned awake_count = locked ? 1 : 0;
|
||||
if (auto r = syscall(SYS_futex, &m_value, FUTEX_REQUEUE, awake_count,
|
||||
_count - awake_count, &mutex, 0);
|
||||
r < _count) {
|
||||
// Keep awaking waiters
|
||||
_count = is_one ? 1 : INT_MAX;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
#else
|
||||
unsigned _old = m_value.load();
|
||||
_count = std::min<int>(_count, _old & c_waiter_mask);
|
||||
if (_count <= 0)
|
||||
return;
|
||||
|
||||
// Try to lock the mutex
|
||||
const bool locked = mutex.lock_forced(_count);
|
||||
mutex.lock_forced(1);
|
||||
|
||||
const int max_sig = atomic_op(m_value, [&](unsigned &value) {
|
||||
// Verify the number of waiters
|
||||
const int wakeupWaiters = m_value.op([&](unsigned &value) {
|
||||
int max_sig = std::min<int>(_count, value & c_waiter_mask);
|
||||
|
||||
// Add lock signal (mutex was immediately locked)
|
||||
if (locked && max_sig)
|
||||
value |= c_locked_mask;
|
||||
else if (locked)
|
||||
std::abort();
|
||||
|
||||
// Add normal signals
|
||||
value += c_signal_one * max_sig;
|
||||
|
||||
|
|
@ -96,21 +142,11 @@ void shared_cv::impl_wake(shared_mutex &mutex, int _count) noexcept {
|
|||
return max_sig;
|
||||
});
|
||||
|
||||
if (max_sig < _count) {
|
||||
// Fixup mutex
|
||||
mutex.lock_forced(max_sig - _count);
|
||||
_count = max_sig;
|
||||
if (wakeupWaiters > 0) {
|
||||
m_value.notify_n(wakeupWaiters);
|
||||
}
|
||||
|
||||
if (_count) {
|
||||
// Wake up one thread + requeue remaining waiters
|
||||
unsigned awake_count = locked ? 1 : 0;
|
||||
if (auto r = syscall(SYS_futex, &m_value, FUTEX_REQUEUE, awake_count,
|
||||
_count - awake_count, &mutex, 0);
|
||||
r < _count) {
|
||||
// Keep awaking waiters
|
||||
return impl_wake(mutex, is_one ? 1 : INT_MAX);
|
||||
}
|
||||
}
|
||||
mutex.unlock();
|
||||
#endif
|
||||
}
|
||||
} // namespace orbis::utils
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue