From 1e09407742f4e5870f5b71b602a8b32d0cecc6a7 Mon Sep 17 00:00:00 2001 From: Simon Rozman Date: Thu, 26 May 2016 12:59:06 +0200 Subject: [PATCH] Related characters preparation multi-threaded now --- ZRColaCompile/dbsource.cpp | 90 ++++++++++++++++++++++++++++++++++++++ ZRColaCompile/dbsource.h | 23 ++++++++-- ZRColaCompile/main.cpp | 17 +------ lib/stdex | 2 +- 4 files changed, 112 insertions(+), 20 deletions(-) diff --git a/ZRColaCompile/dbsource.cpp b/ZRColaCompile/dbsource.cpp index 339c63c..235c8fb 100644 --- a/ZRColaCompile/dbsource.cpp +++ b/ZRColaCompile/dbsource.cpp @@ -22,6 +22,92 @@ using namespace std; +////////////////////////////////////////////////////////////////////////// +// ZRCola::DBSource::character_bank +////////////////////////////////////////////////////////////////////////// + +ZRCola::DBSource::character_bank::character_bank() : vector >() +{ + resize(0x10000); +} + + +void ZRCola::DBSource::character_bank::build_related() +{ + SYSTEM_INFO si; + GetSystemInfo(&si); + + // Launch threads. + vector threads; + threads.reserve(si.dwNumberOfProcessors); + size_type from = 0, to; + for (DWORD i = 0; i < si.dwNumberOfProcessors; i++) { + to = MulDiv(i + 1, 0x10000, si.dwNumberOfProcessors); + threads.push_back(build_related_worker(this, from, to)); + from = to; + } + + // Wait for threads. + for (DWORD i = 0; i < si.dwNumberOfProcessors; i++) { + HANDLE h = threads[i].get(); + if (h) WaitForSingleObject(h, INFINITE); + } +} + + +ZRCola::DBSource::character_bank::build_related_worker::build_related_worker(_In_ character_bank *cb, _In_ size_type from, _In_ size_type to) : + thread_type((HANDLE)_beginthreadex(NULL, 0, process, this, CREATE_SUSPENDED, NULL)), + m_cb(cb), + m_from(from), + m_to(to) +{ + ResumeThread(get()); +} + + +ZRCola::DBSource::character_bank::build_related_worker::build_related_worker(_Inout_ build_related_worker &&othr) : + thread_type((thread_type&&)othr), + m_cb(othr.m_cb), + m_from(othr.m_from), + m_to(othr.m_to) +{ + othr.release(); +} + + +unsigned int ZRCola::DBSource::character_bank::build_related_worker::process() +{ + for (size_type i = m_from; i < m_to; i++) { + ZRCola::DBSource::character &chr = *(m_cb->at(i).get()); + if (&chr == NULL) continue; + + // Remove all unexisting, inactive, or self related characters. + for (wstring::size_type i = chr.rel.length(); i--;) { + if (!m_cb->at(chr.rel[i]) || (wchar_t)i == chr.rel[i]) + chr.rel.erase(i, 1); + } + + //for (size_t j = 0, j_end = size(); j < j_end; j++) { + // if (i == j) continue; + // ZRCola::DBSource::character &chr = *(chrs[i].get()); + // if (&chr == NULL) continue; + //} + } + + return 0; +} + + +unsigned int __stdcall ZRCola::DBSource::character_bank::build_related_worker::process(_In_ void *param) +{ + return ((ZRCola::DBSource::character_bank::build_related_worker*)param)->process(); +} + + +////////////////////////////////////////////////////////////////////////// +// ZRCola::DBSource::character_desc_idx +////////////////////////////////////////////////////////////////////////// + void ZRCola::DBSource::character_desc_idx::parse_keywords(const wchar_t *str, list &terms) { wxASSERT_MSG(str, wxT("string is NULL")); @@ -115,6 +201,10 @@ void ZRCola::DBSource::character_desc_idx::save(ZRCola::textindex > { public: - character_bank() : std::vector >() + character_bank(); + void build_related(); + + protected: + class build_related_worker : public std::unique_ptr > { - resize(0x10000); - } + public: + typedef std::unique_ptr > thread_type; + + public: + build_related_worker(_In_ character_bank *cb, _In_ size_type from, _In_ size_type to); + build_related_worker(_Inout_ build_related_worker &&othr); + + protected: + unsigned int process(); + static unsigned int __stdcall process(_In_ void *param); + + protected: + character_bank *m_cb; + size_type m_from, m_to; + }; }; diff --git a/ZRColaCompile/main.cpp b/ZRColaCompile/main.cpp index c0af840..7d67d53 100644 --- a/ZRColaCompile/main.cpp +++ b/ZRColaCompile/main.cpp @@ -411,22 +411,7 @@ int _tmain(int argc, _TCHAR *argv[]) } // Phase 2: Build related character lists. - for (size_t i = 0, i_end = chrs.size(); i < i_end; i++) { - ZRCola::DBSource::character &chr = *(chrs[i].get()); - if (&chr == NULL) continue; - - // Remove all unexisting, inactive, or self related characters. - for (wstring::size_type i = chr.rel.length(); i--;) { - if (!chrs[chr.rel[i]] || (wchar_t)i == chr.rel[i]) - chr.rel.erase(i, 1); - } - - //for (size_t j = 0, j_end = chrs.size(); j < j_end; j++) { - // if (i == j) continue; - // ZRCola::DBSource::character &chr = *(chrs[i].get()); - // if (&chr == NULL) continue; - //} - } + chrs.build_related(); ZRCola::character_db db; diff --git a/lib/stdex b/lib/stdex index c004f8c..49c5b47 160000 --- a/lib/stdex +++ b/lib/stdex @@ -1 +1 @@ -Subproject commit c004f8c2ef368971f0b994e36e83dd63838c70b3 +Subproject commit 49c5b4723093c4692a0a08c077068477945672f1