Add a high-performance spin mutex class.

This commit is contained in:
KingToolbox 2020-07-28 01:43:10 +08:00
parent 1ec05ef658
commit 067d6e4045
2 changed files with 84 additions and 1 deletions

View File

@ -10,4 +10,8 @@ A quick circular buffer template class.
## Onigmo
An improved version based on Onigmo 5.13.5. In particular, **the addition of iterator makes it possible to match gap buffer or nonadjacent memory blocks.** Please refer to the sample files for how to use.
An improved version based on Onigmo 5.13.5. In particular, **the addition of iterator makes it possible to match gap buffer or nonadjacent memory blocks.** Please refer to the sample files for how to use.
## Spin.h
A high-performance spin mutex and locker.

79
src/Spin.h Normal file
View File

@ -0,0 +1,79 @@
/*
* Copyright 2020, WindTerm.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef SPIN_H
#define SPIN_H
#pragma once
#include <atomic>
#include <thread>
class SpinMutex {
public:
SpinMutex() {
unlock();
}
void yield(size_t nCount) {
if (nCount < 2) {
} else if (nCount < 16) {
std::this_thread::yield();
} else if (nCount < 32) {
std::this_thread::sleep_for(std::chrono::nanoseconds(100));
} else {
std::this_thread::sleep_for(std::chrono::microseconds(10));
}
}
void lock() {
for (size_t i = 0; !try_lock(); i++) {
yield(i);
}
}
void unlock() {
flag.clear(std::memory_order_release);
}
bool try_lock() {
return flag.test_and_set(std::memory_order_acquire) == false;
}
private:
SpinMutex(const SpinMutex &) = delete;
SpinMutex &operator=(const SpinMutex &) = delete;
std::atomic_flag flag;
};
template <typename LockType>
class ThreadLocker {
public:
ThreadLocker(LockType& m_, bool bLock_ = true) : m(m_), bLock(bLock_) {
if (bLock) m.lock();
}
~ThreadLocker() {
if (bLock) m.unlock();
}
private:
LockType& m;
bool bLock;
};
#endif // SPIN_H