锁优化
This commit is contained in:
@@ -1,34 +1,18 @@
|
||||
#ifndef Toolbox_Singleton_H
|
||||
#define Toolbox_Singleton_H
|
||||
#include <mutex>
|
||||
namespace Toolbox {
|
||||
template<typename Derived>
|
||||
class Singleton {
|
||||
public:
|
||||
static Derived *instance() {
|
||||
Derived *temp = instance_.load(std::memory_order_acquire);
|
||||
if (temp == nullptr) {
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
temp = instance_.load(std::memory_order_relaxed);
|
||||
if (temp == nullptr) {
|
||||
temp = new Derived;
|
||||
instance_.store(temp, std::memory_order_release);
|
||||
}
|
||||
}
|
||||
return temp;
|
||||
static Derived instance;
|
||||
return &instance;
|
||||
}
|
||||
Singleton(const Singleton &) = delete;
|
||||
Singleton &operator=(const Singleton &) = delete;
|
||||
protected:
|
||||
Singleton() = default;
|
||||
~Singleton() = default;
|
||||
private:
|
||||
static std::atomic<Derived *> instance_;
|
||||
static std::mutex mutex_;
|
||||
};
|
||||
template<typename Derived>
|
||||
std::atomic<Derived *> Singleton<Derived>::instance_(nullptr);
|
||||
template<typename Derived>
|
||||
std::mutex Singleton<Derived>::mutex_;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
#include <chrono>
|
||||
#include "SpinLock.h"
|
||||
namespace Toolbox {
|
||||
void SpinLock::lock() {
|
||||
while (flag.test_and_set(std::memory_order_acquire)) {}
|
||||
}
|
||||
bool SpinLock::tryLock() {
|
||||
return !flag.test_and_set(std::memory_order_acquire);
|
||||
}
|
||||
bool SpinLock::tryLock(int durationMillis) {
|
||||
if (durationMillis == 0) {
|
||||
return tryLock();
|
||||
}
|
||||
auto duration = std::chrono::milliseconds(durationMillis);
|
||||
auto start = std::chrono::steady_clock::now();
|
||||
while (std::chrono::steady_clock::now() - start < duration) {
|
||||
if (tryLock()) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
void SpinLock::unlock() {
|
||||
flag.clear(std::memory_order_release);
|
||||
}
|
||||
SpinLockGuard::~SpinLockGuard() {
|
||||
if (mLock) mLock->unlock();
|
||||
}
|
||||
SpinLockGuard::SpinLockGuard(SpinLock *lock) : mLock(lock) {
|
||||
if (mLock) mLock->lock();
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
#ifndef Toolbox_SpinLock_H
|
||||
#define Toolbox_SpinLock_H
|
||||
#include <atomic>
|
||||
namespace Toolbox {
|
||||
class SpinLock {
|
||||
public:
|
||||
void lock();
|
||||
bool tryLock();
|
||||
bool tryLock(int durationMillis);
|
||||
void unlock();
|
||||
private:
|
||||
std::atomic_flag flag = ATOMIC_FLAG_INIT;
|
||||
};
|
||||
class SpinLockGuard {
|
||||
public:
|
||||
explicit SpinLockGuard(SpinLock *lock);
|
||||
~SpinLockGuard();
|
||||
private:
|
||||
SpinLock *mLock;
|
||||
};
|
||||
}
|
||||
#endif
|
||||
@@ -1,8 +1,7 @@
|
||||
#include "Node.hpp"
|
||||
#include "CircularLinkedList.hpp"
|
||||
#include "SpinLock.h"
|
||||
#include "Singleton.hpp"
|
||||
#include "RollObject.h"
|
||||
#include "SingletonWidget.hpp"
|
||||
#include "view/FileFolderBar.h"
|
||||
#include "pinyin/VirtualKeyBoard.h"
|
||||
#include "pinyin/VirtualKeyBoard.h"
|
||||
|
||||
Reference in New Issue
Block a user