diff --git a/src/xenia/cpu/backend/x64/x64_code_cache.cc b/src/xenia/cpu/backend/x64/x64_code_cache.cc index b258f2658..e4a23248e 100644 --- a/src/xenia/cpu/backend/x64/x64_code_cache.cc +++ b/src/xenia/cpu/backend/x64/x64_code_cache.cc @@ -174,15 +174,17 @@ void* X64CodeCache::PlaceGuestCode(uint32_t guest_address, void* machine_code, // If we are going above the high water mark of committed memory, commit // some more. It's ok if multiple threads do this, as redundant commits // aren't harmful. - size_t old_commit_mark = generated_code_commit_mark_; - if (high_mark > old_commit_mark) { - size_t new_commit_mark = old_commit_mark + 16 * 1024 * 1024; + size_t old_commit_mark, new_commit_mark; + do { + old_commit_mark = generated_code_commit_mark_; + if (high_mark <= old_commit_mark) break; + + new_commit_mark = old_commit_mark + 16 * 1024 * 1024; xe::memory::AllocFixed(generated_code_base_, new_commit_mark, xe::memory::AllocationType::kCommit, xe::memory::PageAccess::kExecuteReadWrite); - generated_code_commit_mark_.compare_exchange_strong(old_commit_mark, - new_commit_mark); - } + } while (generated_code_commit_mark_.compare_exchange_weak( + old_commit_mark, new_commit_mark)); // Copy code. std::memcpy(code_address, machine_code, code_size); @@ -248,15 +250,17 @@ uint32_t X64CodeCache::PlaceData(const void* data, size_t length) { // If we are going above the high water mark of committed memory, commit some // more. It's ok if multiple threads do this, as redundant commits aren't // harmful. - size_t old_commit_mark = generated_code_commit_mark_; - if (high_mark > old_commit_mark) { - size_t new_commit_mark = old_commit_mark + 16 * 1024 * 1024; + size_t old_commit_mark, new_commit_mark; + do { + old_commit_mark = generated_code_commit_mark_; + if (high_mark <= old_commit_mark) break; + + new_commit_mark = old_commit_mark + 16 * 1024 * 1024; xe::memory::AllocFixed(generated_code_base_, new_commit_mark, xe::memory::AllocationType::kCommit, xe::memory::PageAccess::kExecuteReadWrite); - generated_code_commit_mark_.compare_exchange_strong(old_commit_mark, - new_commit_mark); - } + } while (generated_code_commit_mark_.compare_exchange_weak(old_commit_mark, + new_commit_mark)); // Copy code. std::memcpy(data_address, data, length);