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 77e383d commit ab22024
Showing 1 changed file with 57 additions and 14 deletions.
71 changes: 57 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,10 +405,16 @@ 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;

if (NtWaitForAlertByThreadId)
{
// Similarly to futex, never required
fallback = true;
}
#endif

while (ptr_cmp(data, size, old_value, mask))
Expand Down Expand Up @@ -428,18 +443,33 @@ 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 (sema->load() == umax) [[unlikely]]
{
// Signaled prematurely
sema->release(GetCurrentThreadId());
}
else
{
NtWaitForAlertByThreadId(const_cast<void*>(data), timeout + 1 ? &qw : nullptr);
}
}

if (!NtWaitForKeyedEvent(nullptr, sema, false, timeout + 1 ? &qw : nullptr))
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 Down Expand Up @@ -487,6 +517,19 @@ static inline bool alert_sema(atomic_t<u32>* sema)
return true;
}
#elif defined(_WIN32)
if (NtWaitForAlertByThreadId)
{
u32 tid = sema->load();

if (tid + 1 <= 1 && sema->compare_and_swap_test(tid, -1))
{
verify(HERE), NtAlertThreadByThreadId(tid) == STATUS_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 +591,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 ab22024

Please sign in to comment.