metaforce/Runtime/CDvdFile.cpp

134 lines
3.5 KiB
C++
Raw Normal View History

2016-03-07 03:12:32 +00:00
#include "CDvdFile.hpp"
#include "CDvdRequest.hpp"
2017-11-19 07:10:54 +00:00
#include "CStopwatch.hpp"
2016-03-07 03:12:32 +00:00
namespace urde
{
hecl::ProjectPath CDvdFile::m_DvdRoot;
class CFileDvdRequest : public IDvdRequest
{
2016-12-14 01:10:17 +00:00
std::shared_ptr<athena::io::FileReader> m_reader;
2016-03-07 03:12:32 +00:00
void* m_buf;
u32 m_len;
ESeekOrigin m_whence;
int m_offset;
bool m_cancel = false;
bool m_complete = false;
std::function<void(u32)> m_callback;
2016-03-07 03:12:32 +00:00
public:
~CFileDvdRequest()
{
PostCancelRequest();
}
void WaitUntilComplete()
{
while (!m_complete && !m_cancel)
{
std::unique_lock<std::mutex> lk(CDvdFile::m_WaitMutex);
2016-03-07 03:12:32 +00:00
}
}
bool IsComplete() {return m_complete;}
void PostCancelRequest()
{
2017-10-28 07:08:48 +00:00
std::unique_lock<std::mutex> waitlk(CDvdFile::m_WaitMutex);
2016-03-07 03:12:32 +00:00
m_cancel = true;
}
EMediaType GetMediaType() const
{
return EMediaType::File;
}
CFileDvdRequest(CDvdFile& file, void* buf, u32 len, ESeekOrigin whence, int off, std::function<void(u32)>&& cb)
: m_reader(file.m_reader), m_buf(buf), m_len(len), m_whence(whence), m_offset(off), m_callback(std::move(cb)) {}
2016-03-07 03:12:32 +00:00
void DoRequest()
{
if (m_cancel)
return;
u32 readLen;
2016-03-07 03:12:32 +00:00
if (m_whence == ESeekOrigin::Cur && m_offset == 0)
{
readLen = m_reader->readBytesToBuf(m_buf, m_len);
}
2016-03-07 03:12:32 +00:00
else
{
2016-12-14 01:10:17 +00:00
m_reader->seek(m_offset, athena::SeekOrigin(m_whence));
readLen = m_reader->readBytesToBuf(m_buf, m_len);
2016-03-07 03:12:32 +00:00
}
if (m_callback)
m_callback(readLen);
2016-03-07 03:12:32 +00:00
m_complete = true;
}
};
std::thread CDvdFile::m_WorkerThread;
std::mutex CDvdFile::m_WorkerMutex;
std::condition_variable CDvdFile::m_WorkerCV;
std::mutex CDvdFile::m_WaitMutex;
2016-03-07 03:12:32 +00:00
bool CDvdFile::m_WorkerRun = false;
std::vector<std::shared_ptr<IDvdRequest>> CDvdFile::m_RequestQueue;
void CDvdFile::WorkerProc()
{
logvisor::RegisterThreadName("CDvdFile Thread");
2016-03-07 03:12:32 +00:00
while (m_WorkerRun)
{
std::unique_lock<std::mutex> lk(CDvdFile::m_WorkerMutex);
while (CDvdFile::m_RequestQueue.size())
2016-03-07 03:12:32 +00:00
{
std::vector<std::shared_ptr<IDvdRequest>> swapQueue;
swapQueue.swap(CDvdFile::m_RequestQueue);
lk.unlock();
std::unique_lock<std::mutex> waitlk(CDvdFile::m_WaitMutex);
2016-03-07 03:12:32 +00:00
for (std::shared_ptr<IDvdRequest>& req : swapQueue)
{
CFileDvdRequest& concreteReq = static_cast<CFileDvdRequest&>(*req);
concreteReq.DoRequest();
}
waitlk.unlock();
swapQueue.clear();
2016-03-07 03:12:32 +00:00
lk.lock();
}
if (!m_WorkerRun)
break;
2016-03-07 03:12:32 +00:00
m_WorkerCV.wait(lk);
}
}
std::shared_ptr<IDvdRequest> CDvdFile::AsyncSeekRead(void* buf, u32 len, ESeekOrigin whence,
int off, std::function<void(u32)>&& cb)
2016-03-07 03:12:32 +00:00
{
std::shared_ptr<IDvdRequest> ret =
std::make_shared<CFileDvdRequest>(*this, buf, len, whence, off, std::move(cb));
2016-03-07 03:12:32 +00:00
std::unique_lock<std::mutex> lk(CDvdFile::m_WorkerMutex);
m_RequestQueue.emplace_back(ret);
lk.unlock();
m_WorkerCV.notify_one();
return ret;
}
void CDvdFile::Initialize(const hecl::ProjectPath& path)
{
m_DvdRoot = path;
if (m_WorkerRun)
return;
m_WorkerRun = true;
m_WorkerThread = std::thread(WorkerProc);
}
void CDvdFile::Shutdown()
{
if (!m_WorkerRun)
return;
m_WorkerRun = false;
m_WorkerCV.notify_one();
if (m_WorkerThread.joinable())
m_WorkerThread.join();
m_RequestQueue.clear();
}
}