/* * Copyright (C) 2019-2023 Intel Corporation * * SPDX-License-Identifier: MIT * */ #pragma once #include namespace NEO { namespace MultiThreadHelpers { template inline bool atomicCompareExchangeWeakSpin(std::atomic &destination, Type &expectedValue, const Type desiredValue) { const Type currentValue = destination; if (currentValue == expectedValue) { if (destination.compare_exchange_weak(expectedValue, desiredValue)) { return true; } } else { expectedValue = currentValue; } return false; } template void interlockedMax(std::atomic &dest, Type newVal) { Type oldVal = dest; Type maxVal = oldVal < newVal ? newVal : oldVal; while (!atomicCompareExchangeWeakSpin(dest, oldVal, maxVal)) { maxVal = oldVal < newVal ? newVal : oldVal; } } } // namespace MultiThreadHelpers } // namespace NEO