Files
compute-runtime/core/helpers/interlocked_max.h
Zbigniew Zdanowicz e2906fcdf1 Move interlockedMax to core helpers
Change-Id: I5496ea963e68e0ef1e107c860112b7123d38aa81
Signed-off-by: Zbigniew Zdanowicz <zbigniew.zdanowicz@intel.com>
2019-05-28 17:38:17 +02:00

19 lines
435 B
C++

/*
* Copyright (C) 2019 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#pragma once
#include <atomic>
template <typename Type>
void interlockedMax(std::atomic<Type> &dest, Type newVal) {
Type oldVal = dest;
Type maxVal = oldVal < newVal ? newVal : oldVal;
while (!std::atomic_compare_exchange_weak(&dest, &oldVal, maxVal)) {
oldVal = dest;
maxVal = oldVal < newVal ? newVal : oldVal;
}
}