DPC++ Runtime
Runtime libraries for oneAPI DPC++
spinlock.hpp
Go to the documentation of this file.
1 //==-------------------- spinlock.hpp --- Spin lock ------------------------==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #pragma once
10 
11 #include <sycl/detail/defines.hpp>
12 
13 #include <atomic>
14 #include <thread>
15 
16 namespace sycl {
17 inline namespace _V1 {
18 namespace detail {
27 class SpinLock {
28 public:
29  void lock() {
30  while (MLock.test_and_set(std::memory_order_acquire))
31  std::this_thread::yield();
32  }
33  void unlock() { MLock.clear(std::memory_order_release); }
34 
35 private:
36  std::atomic_flag MLock = ATOMIC_FLAG_INIT;
37 };
38 } // namespace detail
39 } // namespace _V1
40 } // namespace sycl
SpinLock is a synchronization primitive, that uses atomic variable and causes thread trying acquire l...
Definition: spinlock.hpp:27
constexpr auto memory_order_release
constexpr auto memory_order_acquire
Definition: access.hpp:18