锁优化

This commit is contained in:
2026-07-23 12:27:07 +08:00
parent bd3ef5d1d5
commit 76a7053554
7 changed files with 80 additions and 136 deletions
-1
View File
@@ -2,7 +2,6 @@
#define BackEnd_H
#include <QObject>
#include <QMutex>
#include <QTimer>
#include <QKeyEvent>
+1 -56
View File
@@ -14,62 +14,7 @@
#include <QtWidgets>
#include <QtWidgetsDepends>
#include <functional>
#include "psc_global_include/Singleton.hpp"
// 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;
// }
// 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_;
//template<typename T>
//class Singleton {
//public:
// static T *instance() {
// if(!mInstance) {
// mInstance = new T();
// }
// return mInstance;
// }
// virtual ~Singleton() = default;
// Singleton(T &&) = delete;
// Singleton(const T &) = delete;
// void operator=(const T &) = delete;
//protected:
// Singleton() = default;
// static T* mInstance;
//};
//template<typename T> T* Singleton<T>::mInstance = nullptr;
class Global : public Psc::Singleton<Global> {
public:
explicit Global();
@@ -101,4 +46,4 @@ QPushButton *createClickButton(const QString &text, int fontSize, const std::fun
QPushButton* createNormal(const QString& text, int fontSize, const std::function<void(void)>& mFunc = nullptr);
#endif
#endif
+3 -19
View File
@@ -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
-32
View File
@@ -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();
}
}
-22
View File
@@ -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 -2
View File
@@ -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"