Skip to content

Commit

Permalink
Merge branch 'threadpool' into develop
Browse files Browse the repository at this point in the history
# Conflicts:
#	.gitmodules
#	CMakeLists.txt
#	src/mkpsxiso/cdwriter.h
  • Loading branch information
CookiePLMonster committed Mar 16, 2023
2 parents 90b5e08 + 042e500 commit ff45d1b
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@
[submodule "ghc"]
path = ghc
url = https://github.com/gulrak/filesystem
[submodule "ThreadPool"]
path = ThreadPool
url = https://github.com/progschj/ThreadPool
6 changes: 5 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ if(NOT EXISTS ${PROJECT_SOURCE_DIR}/ghc/include/ghc/filesystem.hpp)
message(FATAL_ERROR "The ghc directory is empty. Run 'git submodule update --init --recursive' to populate it.")
endif()

if(NOT EXISTS ${PROJECT_SOURCE_DIR}/ThreadPool/ThreadPool.h)
message(FATAL_ERROR "The ThreadPool directory is empty. Run 'git submodule update --init --recursive' to populate it.")
endif()

# Build tinyxml2. I didn't bother with tinyxml2's actual CMake integration
# because it's far easier do do this. It is a single-file library after all.
add_library (tinyxml2 STATIC tinyxml2/tinyxml2.cpp)
Expand Down Expand Up @@ -86,7 +90,7 @@ add_executable(mkpsxiso
${mkpsxiso_dir}/iso.cpp
${mkpsxiso_dir}/main.cpp
)
target_include_directories(mkpsxiso PUBLIC "miniaudio")
target_include_directories(mkpsxiso PUBLIC "miniaudio" "ThreadPool")
target_link_libraries(mkpsxiso tinyxml2 iso_shared Threads::Threads)
if(MINGW)
target_link_libraries(mkpsxiso "-municode")
Expand Down
1 change: 1 addition & 0 deletions ThreadPool
Submodule ThreadPool added at 9a42ec
15 changes: 9 additions & 6 deletions src/mkpsxiso/cdwriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ bool IsoWriter::Create(const fs::path& fileName, unsigned int sizeLBA)
{
const uint64_t sizeBytes = static_cast<uint64_t>(sizeLBA) * CD_SECTOR_SIZE;

m_threadPool = std::make_unique<ThreadPool>(std::thread::hardware_concurrency());

m_mmap = std::make_unique<MMappedFile>();
return m_mmap->Create(fileName, sizeBytes);
}
Expand All @@ -33,8 +35,9 @@ void IsoWriter::Close()

// ======================================================

IsoWriter::SectorView::SectorView(MMappedFile* mappedFile, unsigned int offsetLBA, unsigned int sizeLBA, EdcEccForm edcEccForm)
: m_view(mappedFile->GetView(static_cast<uint64_t>(offsetLBA) * CD_SECTOR_SIZE, static_cast<size_t>(sizeLBA) * CD_SECTOR_SIZE))
IsoWriter::SectorView::SectorView(ThreadPool* threadPool, MMappedFile* mappedFile, unsigned int offsetLBA, unsigned int sizeLBA, EdcEccForm edcEccForm)
: m_threadPool(threadPool)
, m_view(mappedFile->GetView(static_cast<uint64_t>(offsetLBA) * CD_SECTOR_SIZE, static_cast<size_t>(sizeLBA) * CD_SECTOR_SIZE))
, m_currentLBA(offsetLBA)
, m_endLBA(offsetLBA + sizeLBA)
, m_edcEccForm(edcEccForm)
Expand Down Expand Up @@ -85,7 +88,7 @@ void IsoWriter::SectorView::CalculateForm1()
{
SECTOR_M2F1* sector = static_cast<SECTOR_M2F1*>(m_currentSector);

m_checksumJobs.emplace_front(std::async(std::launch::async, [](SECTOR_M2F1* sector)
m_checksumJobs.emplace_front(m_threadPool->enqueue([](SECTOR_M2F1* sector)
{
// Encode EDC data
EDC_ECC_GEN.ComputeEdcBlock(sector->subHead, sizeof(sector->subHead) + sizeof(sector->data), sector->edc);
Expand All @@ -101,7 +104,7 @@ void IsoWriter::SectorView::CalculateForm1()
void IsoWriter::SectorView::CalculateForm2()
{
SECTOR_M2F2* sector = static_cast<SECTOR_M2F2*>(m_currentSector);
m_checksumJobs.emplace_front(std::async(std::launch::async, &EDCECC::ComputeEdcBlock, &EDC_ECC_GEN,
m_checksumJobs.emplace_front(m_threadPool->enqueue(&EDCECC::ComputeEdcBlock, &EDC_ECC_GEN,
sector->data, sizeof(sector->data) - 4, sector->data + sizeof(sector->data) - 4));
}

Expand Down Expand Up @@ -258,7 +261,7 @@ class SectorViewM2F1 final : public IsoWriter::SectorView

auto IsoWriter::GetSectorViewM2F1(unsigned int offsetLBA, unsigned int sizeLBA, EdcEccForm edcEccForm) const -> std::unique_ptr<SectorView>
{
return std::make_unique<SectorViewM2F1>(m_mmap.get(), offsetLBA, sizeLBA, edcEccForm);
return std::make_unique<SectorViewM2F1>(m_threadPool.get(), m_mmap.get(), offsetLBA, sizeLBA, edcEccForm);
}

class SectorViewM2F2 final : public IsoWriter::SectorView
Expand Down Expand Up @@ -423,7 +426,7 @@ class SectorViewM2F2 final : public IsoWriter::SectorView

auto IsoWriter::GetSectorViewM2F2(unsigned int offsetLBA, unsigned int sizeLBA, EdcEccForm edcEccForm) const -> std::unique_ptr<SectorView>
{
return std::make_unique<SectorViewM2F2>(m_mmap.get(), offsetLBA, sizeLBA, edcEccForm);
return std::make_unique<SectorViewM2F2>(m_threadPool.get(), m_mmap.get(), offsetLBA, sizeLBA, edcEccForm);
}

// ======================================================
Expand Down
6 changes: 4 additions & 2 deletions src/mkpsxiso/cdwriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#include "cd.h"
#include "mmappedfile.h"
#include "fs.h"
#include <ThreadPool.h>
#include <forward_list>
#include <future>
#include <memory>

namespace cd {
Expand All @@ -31,7 +31,7 @@ class IsoWriter
class SectorView
{
public:
SectorView(MMappedFile* mappedFile, unsigned int offsetLBA, unsigned int sizeLBA, EdcEccForm edcEccForm);
SectorView(ThreadPool* threadPool, MMappedFile* mappedFile, unsigned int offsetLBA, unsigned int sizeLBA, EdcEccForm edcEccForm);
virtual ~SectorView();

virtual void WriteFile(FILE* file) = 0;
Expand Down Expand Up @@ -59,6 +59,7 @@ class IsoWriter

private:
std::forward_list<std::future<void>> m_checksumJobs;
ThreadPool* m_threadPool;
MMappedFile::View m_view;
};

Expand Down Expand Up @@ -86,6 +87,7 @@ class IsoWriter

private:
std::unique_ptr<MMappedFile> m_mmap;
std::unique_ptr<ThreadPool> m_threadPool;
};

ISO_USHORT_PAIR SetPair16(unsigned short val);
Expand Down

0 comments on commit ff45d1b

Please sign in to comment.