mirror of
https://github.com/WerWolv/ImHex.git
synced 2026-03-27 23:37:05 -05:00
impr: Calculate hashes in a background thread
This commit is contained in:
@@ -38,24 +38,14 @@ EXPORT_MODULE namespace hex {
|
||||
[[nodiscard]] const Hash *getType() const { return m_type; }
|
||||
[[nodiscard]] const std::string& getName() const { return m_name; }
|
||||
|
||||
const std::vector<u8>& get(const Region& region, prv::Provider *provider) {
|
||||
if (m_cache.empty()) {
|
||||
m_cache = m_callback(region, provider);
|
||||
}
|
||||
|
||||
return m_cache;
|
||||
}
|
||||
|
||||
void reset() {
|
||||
m_cache.clear();
|
||||
std::vector<u8> get(const Region& region, prv::Provider *provider) const {
|
||||
return m_callback(region, provider);
|
||||
}
|
||||
|
||||
private:
|
||||
Hash *m_type;
|
||||
std::string m_name;
|
||||
Callback m_callback;
|
||||
|
||||
std::vector<u8> m_cache;
|
||||
};
|
||||
|
||||
virtual void draw() { }
|
||||
|
||||
@@ -70,6 +70,8 @@ EXPORT_MODULE namespace hex {
|
||||
[[nodiscard]] u64 getValue() const;
|
||||
[[nodiscard]] u64 getMaxValue() const;
|
||||
|
||||
void wait() const;
|
||||
|
||||
private:
|
||||
void finish();
|
||||
void interruption();
|
||||
@@ -87,9 +89,9 @@ EXPORT_MODULE namespace hex {
|
||||
std::atomic<bool> m_background = true;
|
||||
std::atomic<bool> m_blocking = false;
|
||||
|
||||
std::atomic<bool> m_interrupted = false;
|
||||
std::atomic<bool> m_finished = false;
|
||||
std::atomic<bool> m_hadException = false;
|
||||
std::atomic_flag m_interrupted = false;
|
||||
std::atomic_flag m_finished = false;
|
||||
std::atomic_flag m_hadException = false;
|
||||
std::string m_exceptionMessage;
|
||||
|
||||
struct TaskInterruptor { virtual ~TaskInterruptor() = default; };
|
||||
@@ -114,6 +116,7 @@ EXPORT_MODULE namespace hex {
|
||||
[[nodiscard]] u32 getProgress() const;
|
||||
|
||||
void interrupt() const;
|
||||
void wait() const;
|
||||
private:
|
||||
std::weak_ptr<Task> m_task;
|
||||
};
|
||||
|
||||
@@ -82,9 +82,17 @@ namespace hex {
|
||||
m_maxValue = u64(other.m_maxValue);
|
||||
m_currValue = u64(other.m_currValue);
|
||||
|
||||
m_finished = bool(other.m_finished);
|
||||
m_hadException = bool(other.m_hadException);
|
||||
m_interrupted = bool(other.m_interrupted);
|
||||
if (other.m_finished.test())
|
||||
m_finished.test_and_set();
|
||||
if (other.m_hadException.test())
|
||||
m_hadException.test_and_set();
|
||||
if (other.m_interrupted.test())
|
||||
m_interrupted.test_and_set();
|
||||
|
||||
m_finished.notify_all();
|
||||
m_hadException.notify_all();
|
||||
m_interrupted.notify_all();
|
||||
|
||||
m_shouldInterrupt = bool(other.m_shouldInterrupt);
|
||||
}
|
||||
|
||||
@@ -143,11 +151,11 @@ namespace hex {
|
||||
|
||||
|
||||
bool Task::isFinished() const {
|
||||
return m_finished;
|
||||
return m_finished.test();
|
||||
}
|
||||
|
||||
bool Task::hadException() const {
|
||||
return m_hadException;
|
||||
return m_hadException.test();
|
||||
}
|
||||
|
||||
bool Task::shouldInterrupt() const {
|
||||
@@ -155,11 +163,11 @@ namespace hex {
|
||||
}
|
||||
|
||||
bool Task::wasInterrupted() const {
|
||||
return m_interrupted;
|
||||
return m_interrupted.test();
|
||||
}
|
||||
|
||||
void Task::clearException() {
|
||||
m_hadException = false;
|
||||
m_hadException.clear();
|
||||
}
|
||||
|
||||
std::string Task::getExceptionMessage() const {
|
||||
@@ -180,12 +188,18 @@ namespace hex {
|
||||
return m_maxValue;
|
||||
}
|
||||
|
||||
void Task::wait() const {
|
||||
m_finished.wait(false);
|
||||
}
|
||||
|
||||
void Task::finish() {
|
||||
m_finished = true;
|
||||
m_finished.test_and_set();
|
||||
m_finished.notify_all();
|
||||
}
|
||||
|
||||
void Task::interruption() {
|
||||
m_interrupted = true;
|
||||
m_interrupted.test_and_set();
|
||||
m_interrupted.notify_all();
|
||||
}
|
||||
|
||||
void Task::exception(const char *message) {
|
||||
@@ -193,7 +207,8 @@ namespace hex {
|
||||
|
||||
// Store information about the caught exception
|
||||
m_exceptionMessage = message;
|
||||
m_hadException = true;
|
||||
m_hadException.test_and_set();
|
||||
m_hadException.notify_all();
|
||||
|
||||
// Call the interrupt callback on the current thread if one is set
|
||||
if (m_interruptCallback)
|
||||
@@ -241,6 +256,14 @@ namespace hex {
|
||||
task->interrupt();
|
||||
}
|
||||
|
||||
void TaskHolder::wait() const {
|
||||
const auto &task = m_task.lock();
|
||||
if (!task)
|
||||
return;
|
||||
|
||||
task->wait();
|
||||
}
|
||||
|
||||
u32 TaskHolder::getProgress() const {
|
||||
const auto &task = m_task.lock();
|
||||
if (!task)
|
||||
|
||||
Reference in New Issue
Block a user