cdvdgigaherz: Avoid race condition by reading from cache/disk

Instead of reading from a buffer shared by multiple threads, just read
the correct data from the cache or disk instead.
This commit is contained in:
Jonathan Li 2016-11-16 22:02:00 +00:00 committed by Gregory Hainaut
parent 31b0b53394
commit 3919a32dc3
1 changed files with 10 additions and 11 deletions

View File

@ -37,7 +37,6 @@ static std::thread s_thread;
static std::mutex s_notify_lock; static std::mutex s_notify_lock;
static std::condition_variable s_notify_cv; static std::condition_variable s_notify_cv;
static std::mutex s_request_lock; static std::mutex s_request_lock;
static std::condition_variable s_request_cv;
static std::mutex s_cache_lock; static std::mutex s_cache_lock;
static std::atomic<bool> cdvd_is_open; static std::atomic<bool> cdvd_is_open;
@ -195,7 +194,6 @@ void cdvdThread()
handlingRequest = false; handlingRequest = false;
threadRequestPending = false; threadRequestPending = false;
s_request_cv.notify_one();
prefetch_last_lba = info.lsn; prefetch_last_lba = info.lsn;
prefetch_last_mode = info.mode; prefetch_last_mode = info.mode;
@ -259,19 +257,20 @@ s32 cdvdRequestComplete()
u8 *cdvdGetSector(u32 sector, s32 mode) u8 *cdvdGetSector(u32 sector, s32 mode)
{ {
{ static u8 buffer[2352 * 16];
std::unique_lock<std::mutex> guard(s_request_lock); u32 sector_block = sector & ~15;
while (threadRequestPending)
s_request_cv.wait_for(guard, std::chrono::milliseconds(10)); if (!cdvdCacheFetch(sector_block, mode, buffer))
} if (cdvdReadBlockOfSectors(sector_block, mode, buffer))
cdvdCacheUpdate(sector_block, mode, buffer);
if (mode == CDVD_MODE_2048) { if (mode == CDVD_MODE_2048) {
u32 offset = 2048 * (sector - threadRequestInfo.lsn); u32 offset = 2048 * (sector - sector_block);
return threadRequestInfo.data + offset; return buffer + offset;
} }
u32 offset = 2352 * (sector - threadRequestInfo.lsn); u32 offset = 2352 * (sector - sector_block);
u8 *data = threadRequestInfo.data + offset; u8 *data = buffer + offset;
switch (mode) { switch (mode) {
case CDVD_MODE_2328: case CDVD_MODE_2328: