HDK
Main Page
Related Pages
Modules
Namespaces
Classes
Files
Examples
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Groups
Pages
ort_spin_lock.h
Go to the documentation of this file.
1
// Copyright (c) Microsoft Corporation. All rights reserved.
2
// Licensed under the MIT License.
3
#pragma once
4
#include "
core/common/spin_pause.h
"
5
#include <atomic>
6
7
namespace
onnxruntime {
8
/*
9
OrtSpinLock implemented mutex semantic "lock-freely",
10
calling thread will not be put to sleep on blocked,
11
which reduces cpu usage on context switching.
12
*/
13
struct
OrtSpinLock
{
14
using
LockState
=
enum
{ Locked = 0,
15
Unlocked };
16
17
void
lock
() noexcept {
18
LockState
state = Unlocked;
19
while
(!state_.compare_exchange_weak(state, Locked, std::memory_order_acq_rel, std::memory_order_relaxed)) {
20
state = Unlocked;
21
concurrency::SpinPause
();
// pause and retry
22
}
23
}
24
bool
try_lock
() noexcept {
25
LockState
state = Unlocked;
26
return
state_.compare_exchange_weak(state, Locked, std::memory_order_acq_rel, std::memory_order_relaxed);
27
}
28
void
unlock
() noexcept {
29
state_.store(Unlocked, std::memory_order_release);
30
}
31
32
private
:
33
std::atomic<LockState> state_{Unlocked};
34
};
35
}
// namespace onnxruntime
spin_pause.h
onnxruntime::OrtSpinLock::lock
void lock() noexcept
Definition:
ort_spin_lock.h:17
onnxruntime::OrtSpinLock
Definition:
ort_spin_lock.h:13
onnxruntime::OrtSpinLock::unlock
void unlock() noexcept
Definition:
ort_spin_lock.h:28
onnxruntime::concurrency::SpinPause
void SpinPause()
Definition:
spin_pause.h:20
onnxruntime::OrtSpinLock::try_lock
bool try_lock() noexcept
Definition:
ort_spin_lock.h:24
onnxruntime::OrtSpinLock::LockState
enum{Locked=0, Unlocked} LockState
Definition:
ort_spin_lock.h:15
onnxruntime
core
platform
ort_spin_lock.h
Generated on Tue Dec 17 2024 03:40:02 for HDK by
1.8.6