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 c389fed
Showing 1 changed file with 84 additions and 14 deletions.
98 changes: 84 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,48 @@ 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
if (NtWaitForAlertByThreadId(const_cast<void*>(data), nullptr) == NTSTATUS_ALERTED)
{
fallback = true;
}

if (ptr_cmp(data, size, old_value, mask))
{
break;
}

if (!NtWaitForKeyedEvent(nullptr, sema, false, timeout + 1 ? &qw : nullptr))
// Restart waiting
sema->release(GetCurrentThreadId());
fallback = false;
}
else
{
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 +494,24 @@ 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))
{
// Succeed in self-notifying
break;
}

if (NtWaitForAlertByThreadId(const_cast<void*>(data), nullptr) == NTSTATUS_ALERTED)
{
// Wait for promised alert
break;
}

// Should be unreachable
continue;
}

if (sema->compare_and_swap_test(1, 2))
{
// Succeeded in self-notifying
Expand Down Expand Up @@ -487,6 +544,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) == 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 +618,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 c389fed

Please sign in to comment.