File: mt_helpers.h

package info (click to toggle)
intel-compute-runtime 25.35.35096.9-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 79,324 kB
  • sloc: cpp: 926,243; lisp: 3,433; sh: 715; makefile: 162; python: 21
file content (39 lines) | stat: -rw-r--r-- 1,019 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
 * Copyright (C) 2019-2023 Intel Corporation
 *
 * SPDX-License-Identifier: MIT
 *
 */

#pragma once

#include <atomic>

namespace NEO {
namespace MultiThreadHelpers {

template <typename Type>
inline bool atomicCompareExchangeWeakSpin(std::atomic<Type> &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 <typename Type>
void interlockedMax(std::atomic<Type> &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