Skip to content

Commit

Permalink
atomic.cpp: use new thread alerting API (Win8+)
Browse files Browse the repository at this point in the history
Win7 will remain using old API (keyed events).
  • Loading branch information
Nekotekina committed Oct 24, 2020
1 parent 97ae5ab commit 5f6329d
Showing 1 changed file with 76 additions and 14 deletions.
90 changes: 76 additions & 14 deletions rpcs3/util/atomic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,17 @@ namespace
// Find lowest clear bit
const auto sema = &sema_data[std::countr_one(bits)];

#if defined(USE_FUTEX) || defined(_WIN32)
#if defined(USE_FUTEX)
sema->release(1);
#elif defined(_WIN32)
if (NtWaitForAlertByThreadId)
{
sema->release(GetCurrentThreadId());
}
else
{
sema->release(1);
}
#endif

return sema;
Expand Down Expand Up @@ -396,9 +405,9 @@ void atomic_storage_futex::wait(const void* data, std::size_t size, u64 old_valu
}

// Can skip unqueue process if true
#ifdef USE_FUTEX
bool fallback = true;
#else
#if defined(USE_FUTEX)
const bool fallback = true;
#elif defined(_WIN32)
bool fallback = false;
#endif

Expand Down Expand Up @@ -428,18 +437,40 @@ void atomic_storage_futex::wait(const void* data, std::size_t size, u64 old_valu
qw.QuadPart -= 1;
}

if (fallback)
if (NtWaitForAlertByThreadId)
{
// Restart waiting
verify(HERE), sema->load() == 2;
sema->release(1);
fallback = false;
}
if (fallback) [[unlikely]]
{
// Restart waiting
if (sema->load() == umax)
{
sema->release(GetCurrentThreadId());
}

fallback = false;
}

if (!NtWaitForKeyedEvent(nullptr, sema, false, timeout + 1 ? &qw : nullptr))
// Let's assume it can return spuriously
if (NtWaitForAlertByThreadId(const_cast<void*>(data), timeout + 1 ? &qw : nullptr) == NTSTATUS_ALERTED)
{
fallback = true;
}
}
else
{
// Error code assumed to be timeout
fallback = true;
if (fallback)
{
// Restart waiting
verify(HERE), sema->load() == 2;
sema->release(1);
fallback = false;
}

if (!NtWaitForKeyedEvent(nullptr, sema, false, timeout + 1 ? &qw : nullptr))
{
// Error code assumed to be timeout
fallback = true;
}
}
#endif

Expand All @@ -455,6 +486,21 @@ void atomic_storage_futex::wait(const void* data, std::size_t size, u64 old_valu
#if defined(_WIN32)
static LARGE_INTEGER instant{};

if (NtWaitForAlertByThreadId)
{
if (sema->compare_and_swap_test(GetCurrentThreadId(), -1))
{
break;
}

if (NtWaitForAlertByThreadId(const_cast<void*>(data), &instant) == NTSTATUS_ALERTED)
{
break;
}

continue;
}

if (sema->compare_and_swap_test(1, 2))
{
// Succeeded in self-notifying
Expand Down Expand Up @@ -487,6 +533,22 @@ static inline bool alert_sema(atomic_t<u32>* sema)
return true;
}
#elif defined(_WIN32)
if (NtWaitForAlertByThreadId)
{
u32 tid = sema->load();

// Check if tid is neither 0 nor -1
if (tid + 1 > 1 && sema->compare_and_swap_test(tid, -1))
{
if (NtAlertThreadByThreadId(tid) == NTSTATUS_SUCCESS)
{
return true;
}
}

return false;
}

if (sema->load() == 1 && sema->compare_and_swap_test(1, 2))
{
// Can wait in rare cases, which is its annoying weakness
Expand Down Expand Up @@ -548,7 +610,7 @@ void atomic_storage_futex::notify_all(const void* data)
}

#if defined(_WIN32) && !defined(USE_FUTEX)
if (true)
if (!NtAlertThreadByThreadId)
{
// Make a copy to filter out waiters that fail some checks
u64 copy = slot->sema_bits.load();
Expand Down

0 comments on commit 5f6329d

Please sign in to comment.