Io: Allow early cancel of CRC calculation.

This commit is contained in:
Unknown W. Brackets 2021-02-16 19:37:48 -08:00
parent 25b0be7fe3
commit 872fecd8ed
3 changed files with 8 additions and 3 deletions

View File

@ -50,11 +50,13 @@ BlockDevice *constructBlockDevice(FileLoader *fileLoader) {
return new FileBlockDevice(fileLoader);
}
u32 BlockDevice::CalculateCRC() {
u32 BlockDevice::CalculateCRC(volatile bool *cancel) {
u32 crc = crc32(0, Z_NULL, 0);
u8 block[2048];
for (u32 i = 0; i < GetNumBlocks(); ++i) {
if (cancel && *cancel)
return 0;
if (!ReadBlock(i, block, true)) {
ERROR_LOG(FILESYS, "Failed to read block for CRC");
return 0;

View File

@ -47,7 +47,7 @@ public:
virtual u32 GetNumBlocks() = 0;
virtual bool IsDisc() = 0;
u32 CalculateCRC();
u32 CalculateCRC(volatile bool *cancel = nullptr);
void NotifyReadError();
protected:

View File

@ -103,6 +103,7 @@ namespace Reporting
static std::string crcFilename;
static std::map<std::string, u32> crcResults;
static volatile bool crcPending = false;
static volatile bool crcCancel = false;
static std::thread crcThread;
static int CalculateCRCThread() {
@ -114,7 +115,7 @@ namespace Reporting
u32 crc = 0;
if (blockDevice) {
crc = blockDevice->CalculateCRC();
crc = blockDevice->CalculateCRC(&crcCancel);
}
delete blockDevice;
@ -145,6 +146,7 @@ namespace Reporting
crcFilename = gamePath;
crcPending = true;
crcCancel = false;
crcThread = std::thread(CalculateCRCThread);
}
@ -179,6 +181,7 @@ namespace Reporting
static void PurgeCRC() {
std::unique_lock<std::mutex> guard(crcLock);
crcCancel = true;
while (crcPending) {
crcCond.wait(guard);
}