2018-02-08 23:00:20 +08:00
|
|
|
/*
|
2019-03-26 18:59:46 +08:00
|
|
|
* Copyright (C) 2018-2019 Intel Corporation
|
2018-02-08 23:00:20 +08:00
|
|
|
*
|
2018-09-18 15:11:08 +08:00
|
|
|
* SPDX-License-Identifier: MIT
|
2018-02-08 23:00:20 +08:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2019-07-12 22:50:14 +08:00
|
|
|
#include "core/helpers/non_copyable_or_moveable.h"
|
2018-10-16 21:53:39 +08:00
|
|
|
|
2018-02-08 23:00:20 +08:00
|
|
|
#include <atomic>
|
|
|
|
|
2019-03-26 18:59:46 +08:00
|
|
|
namespace NEO {
|
2018-02-08 23:00:20 +08:00
|
|
|
|
2018-10-16 21:53:39 +08:00
|
|
|
class SpinLock : NonCopyableOrMovableClass {
|
2018-02-08 23:00:20 +08:00
|
|
|
public:
|
2018-10-16 21:53:39 +08:00
|
|
|
SpinLock() = default;
|
|
|
|
~SpinLock() = default;
|
|
|
|
|
|
|
|
void lock() {
|
|
|
|
while (flag.test_and_set(std::memory_order_acquire))
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool try_lock() { // NOLINT
|
|
|
|
return flag.test_and_set(std::memory_order_acquire) == false;
|
2018-02-08 23:00:20 +08:00
|
|
|
}
|
2018-10-16 21:53:39 +08:00
|
|
|
|
|
|
|
void unlock() {
|
|
|
|
flag.clear(std::memory_order_release);
|
2018-02-08 23:00:20 +08:00
|
|
|
}
|
2018-10-16 21:53:39 +08:00
|
|
|
|
|
|
|
protected:
|
|
|
|
std::atomic_flag flag = ATOMIC_FLAG_INIT;
|
2018-02-08 23:00:20 +08:00
|
|
|
};
|
|
|
|
|
2019-03-26 18:59:46 +08:00
|
|
|
} // namespace NEO
|