Skip to content

Commit 52c4ee8

Browse files
committed
everything対応、一旦完成
1 parent dab8f55 commit 52c4ee8

File tree

15 files changed

+155
-109
lines changed

15 files changed

+155
-109
lines changed

internal/dll_project/ohtorii_tools/ohtorii_tools/Source.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ EXPORTS
137137
AutoPreviewRegist
138138

139139
FileRegistAfterDeleteFile
140+
FileUnRegistAfterDeleteFile
140141
WriteToFile
141142
DllDetachFunc_After_Hm866
142143

internal/dll_project/ohtorii_tools/ohtorii_tools/async_files.cpp

Lines changed: 52 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ ASyncFile::ASyncFile(Unity*instance, const WCHAR*source_name, const WCHAR*filena
3939
void ASyncFile::RequestExit() {
4040
m_request_exit = true;
4141
}
42-
42+
const std::wstring& ASyncFile::GetSourceName()const {
43+
return m_source_name;
44+
}
4345

4446
bool ASyncFile::OpenFile() {
4547
if (m_file != nullptr) {
@@ -125,7 +127,7 @@ void ASyncFile::Exec(){
125127
}
126128
}
127129

128-
void ASyncFile::DoLine(WCHAR*line) {
130+
void ASyncFile::DoLine(WCHAR* line) {
129131
std::vector<std::wstring> tokens;
130132
Tokenize(tokens, line, _T("\t"));
131133

@@ -134,33 +136,40 @@ void ASyncFile::DoLine(WCHAR*line) {
134136
return;
135137
}
136138

137-
wchar_t sanitized_candidate_text[4096];
138-
if (swprintf(sanitized_candidate_text, _countof(sanitized_candidate_text), L"%ls", TrimString(tokens.at(0), _T("\n\r")).c_str()) <= 0) {
139+
std::vector<wchar_t> sanitized_candidate_text;
140+
sanitized_candidate_text.resize(1024, 0);
141+
if (swprintf(sanitized_candidate_text.data(), sanitized_candidate_text.size(), L"%ls", TrimString(tokens.at(0), _T("\n\r")).c_str()) <= 0) {
139142
return;
140143
}
141144

142-
Candidates::ContainerType& candidates = m_instance->QueryCandidates().GetCandidates();
143-
Candidates::ContainerType::scoped_lock locker(candidates);
144-
{
145-
candidates.emplace_back(m_source_name.c_str(), sanitized_candidate_text);
145+
//Memo. Lock期間を最小にするため一時変数に対して処理する
146+
Candidates::ContainerType::value_type candidate;
147+
candidate.m_source_name = m_source_name;
148+
candidate.m_text = sanitized_candidate_text.data();
149+
if (3 <= num) {
150+
const auto &action_name= tokens.at(1);
151+
auto action_text = TrimString(tokens.at(2), _T("\n\r"));
152+
auto &action = candidate.m_action;
153+
if (action_name == _T("@action_directory")) {
154+
action.m_directory_name.append(action_text);
155+
}
156+
else if (action_name == _T("@action_filename")) {
157+
action.m_file_name.append(action_text);
158+
}else if (action_name == _T("@description")) {
159+
candidate.m_description.assign(action_text);
160+
}else{
161+
//pass
162+
}
163+
}
146164

147-
if (3 <= num) {
148-
const auto &action_name= tokens.at(1);
149-
auto action_text = TrimString(tokens.at(2), _T("\n\r"));
150-
auto &candidate = candidates.back();
151-
auto &action = candidate.m_action;
165+
if (m_request_exit) {
166+
return;
167+
}
152168

153-
if (action_name == _T("@action_directory")) {
154-
action.m_directory_name.append(action_text);
155-
}
156-
else if (action_name == _T("@action_filename")) {
157-
action.m_file_name.append(action_text);
158-
}else if (action_name == _T("@description")) {
159-
candidate.m_description.assign(action_text);
160-
}else{
161-
//pass
162-
}
163-
}
169+
Candidates::ContainerType& candidates = m_instance->QueryCandidates().GetCandidates();
170+
{
171+
Candidates::ContainerType::scoped_lock locker(candidates);
172+
candidates.emplace_back(candidate);
164173
}
165174
m_instance->ChangeCandidates();
166175
}
@@ -213,7 +222,23 @@ INT_PTR ASyncFiles::AppendCandidate(const WCHAR* source_name, const WCHAR* filen
213222
}
214223

215224
void ASyncFiles::Exec() {
216-
//不要になったスレッドを破棄する
217-
m_threads.remove_if([](Thread&item) {return !item.m_thread.joinable(); });
218-
225+
//終了したスレッドをリストから削除する
226+
m_threads.remove_if([](Thread&item) {
227+
return !item.m_thread.joinable();
228+
});
219229
}
230+
231+
bool ASyncFiles::DestrotFromSourceName(const WCHAR* source_name) {
232+
m_threads.remove_if([source_name](Thread& item) {
233+
const bool isSource = wcscmp(item.m_file.GetSourceName().c_str(), source_name) == 0;
234+
if (isSource) {
235+
item.m_file.RequestExit();
236+
//スレッド中で候補を追加しているため、終了するまで待ってみる
237+
item.m_thread.join();
238+
}
239+
else {
240+
//pass
241+
}
242+
return isSource;
243+
});
244+
}

internal/dll_project/ohtorii_tools/ohtorii_tools/async_files.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class ASyncFile {
1212
ASyncFile(Unity*instance, const WCHAR*source_name, const WCHAR*filename);
1313
void RequestExit();
1414
void operator()();
15-
15+
const std::wstring& GetSourceName()const;
1616
private:
1717
bool OpenFile();
1818
void Exec();
@@ -35,7 +35,6 @@ class ASyncFile {
3535
FILE* m_file;
3636
StaticStatus m_status;
3737
std::chrono::system_clock::time_point m_file_read_start_clock;
38-
//Candidates::ContainerType& m_candidates;
3938
Unity* m_instance;
4039
bool m_request_exit;
4140
bool m_check_bom;
@@ -47,6 +46,7 @@ class ASyncFiles {
4746
ASyncFiles(Unity*instance);
4847
INT_PTR AppendCandidate(const WCHAR* source_name, const WCHAR* filename);
4948
void Exec();
49+
bool DestrotFromSourceName(const WCHAR* source_name);
5050
void Destroy();
5151

5252
private:

internal/dll_project/ohtorii_tools/ohtorii_tools/candidates.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ Candidate::Candidate() {
2828
m_selectable = true;
2929
m_selected = false;
3030
}
31-
3231
Candidate::Candidate(const WCHAR*source_name, const WCHAR*text, const WCHAR*description) :
3332
m_source_name(source_name),
3433
m_text(text),

internal/dll_project/ohtorii_tools/ohtorii_tools/candidates.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
///候補
99
struct Candidate {
1010
Candidate();
11+
Candidate(const Candidate&rht)=default;
12+
Candidate(Candidate&&rht) = default;
13+
Candidate& operator=(const Candidate& rht) = default;
14+
Candidate& operator=(Candidate&& rht) = default;
15+
1116
Candidate(const WCHAR*source_name, const WCHAR*text, const WCHAR*description=_T(""));
1217
Candidate(const WCHAR*source_name, const WCHAR*prefix, const WCHAR*text, const WCHAR*postfix, const WCHAR*description = _T(""));
1318

@@ -64,6 +69,10 @@ struct Candidate {
6469
m_process_id= UNITY_INVALID_PROCESS_ID;
6570
m_window_handle = UNITY_HIDEMARU_NULL_HANDLE;
6671
};
72+
Action(const Action&) = default;
73+
Action(Action&&) noexcept = default;
74+
Action& operator=(const Action&) = default;
75+
Action& operator=(Action&&) noexcept = default;
6776

6877
template<class Archive> void serialize(Archive & archive) {
6978
archive(
@@ -121,6 +130,9 @@ class Candidates {
121130
typedef ParallelVector<Candidate> ContainerType;
122131

123132
Candidates();
133+
Candidates(const Candidates& rht) = delete;
134+
Candidates(Candidates&& rht) = delete;
135+
Candidates& operator=(const Candidates& rht) = delete;
124136

125137
/**ヘッダーを追加する
126138
return 候補へのインデックス

internal/dll_project/ohtorii_tools/ohtorii_tools/dll_export.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ extern "C" WCHAR* CandidatesGetText(INT_PTR index) {
365365
}
366366

367367
extern "C" INT_PTR CandidatesClearWithSourceName(const WCHAR*source_name) {
368-
return Unity::Instance().lock()->QueryCandidates().ClearWithSourceName(source_name);
368+
return Unity::Instance().lock()->CandidatesClearWithSourceName(source_name);
369369
}
370370

371371

@@ -563,6 +563,10 @@ extern "C" INT_PTR FileRegistAfterDeleteFile(const WCHAR* filename) {
563563
return Unity::Instance().lock()->QueryFile().RegistAfterDelete(filename);
564564
}
565565

566+
extern "C" INT_PTR FileUnRegistAfterDeleteFile(const WCHAR* filename) {
567+
return Unity::Instance().lock()->QueryFile().UnRegistAfterDelete(filename);
568+
}
569+
566570
extern "C" INT_PTR DllDetachFunc_After_Hm866( INT_PTR n ) {
567571
DebugLog(_T("DllDetachFunc_After_Hm866 (%d)"), n);
568572
Unity::Destroy();

internal/dll_project/ohtorii_tools/ohtorii_tools/file.cpp

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,6 @@ void File::DeleteFile_::DeleteFile() {
2929
}
3030
}
3131

32-
void File::DeleteFile_::DeleteFileAsThread() {
33-
if (m_deleting) {
34-
return;
35-
}
36-
DebugLog(_T("DeleteFile -> %ls"), m_filename.c_str());
37-
m_deleting = true;
38-
m_thread= std::thread(::DeleteFile, m_filename.c_str());
39-
}
40-
41-
void File::DeleteFile_::Join() {
42-
if (!m_deleting) {
43-
return;
44-
}
45-
if (! m_thread.joinable()) {
46-
return;
47-
}
48-
m_thread.join();
49-
}
50-
51-
5232
/////////////////////////////////////////////////////////////////////////////
5333
//File
5434
/////////////////////////////////////////////////////////////////////////////
@@ -59,22 +39,31 @@ File::File(){
5939
File::~File(){
6040
}
6141

62-
void File::StartDestroy() {
42+
void File::Destroy() {
6343
for (auto&item:m_after_delete) {
6444
item.DeleteFile();
6545
}
6646
}
6747

68-
void File::JoinDestroy() {
69-
for (auto&item : m_after_delete) {
70-
item.Join();
48+
bool File::RegistAfterDelete(const WCHAR*filename){
49+
try {
50+
m_after_delete.emplace_back(filename);
51+
return true;
7152
}
72-
m_after_delete.clear();
53+
catch (std::exception) {
54+
//pass
55+
}
56+
return false;
7357
}
7458

75-
bool File::RegistAfterDelete(const WCHAR*filename){
59+
bool File::UnRegistAfterDelete(const WCHAR* filename) {
7660
try {
77-
m_after_delete.emplace_back(filename);
61+
auto itr = std::find_if(m_after_delete.begin(), m_after_delete.end(), [filename](File::DeleteFile_& x) {
62+
return lstrcmpW(x.GetFileName().c_str(), filename) == 0;
63+
});
64+
if (itr != m_after_delete.end()) {
65+
m_after_delete.erase(itr);
66+
}
7867
return true;
7968
}
8069
catch (std::exception) {

internal/dll_project/ohtorii_tools/ohtorii_tools/file.h

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,42 +10,28 @@ class File{
1010
File();
1111
~File();
1212
bool RegistAfterDelete(const WCHAR*filename);
13+
bool UnRegistAfterDelete(const WCHAR*filename);
1314
bool CreateTempFile(std::wstring&out);
1415
bool WriteToFile(const WCHAR* filename, const WCHAR* string);
1516

1617
struct EnumeFileResult {
1718
std::wstring m_abs_filename;
18-
//std::wstring m_filename;
1919
};
2020
typedef std::deque<EnumeFileResult> EnumeFileResultContainer;
2121
///指定ディレクトリのファイルを絶対パスで列挙する
2222
static bool EnumeFiles(EnumeFileResultContainer&out_files, const WCHAR*root_dir, const WCHAR* extension);
23-
24-
static void StartDestroy();
25-
static void JoinDestroy();
23+
static void Destroy();
2624

2725
protected:
2826
private:
29-
File(const File&);
30-
File &operator=(const File&);
31-
3227
struct DeleteFile_ {
3328
DeleteFile_();
3429
DeleteFile_(const std::wstring&filename);
35-
3630
void DeleteFile();
37-
void DeleteFileAsThread();
38-
void Join();
39-
31+
const std::wstring& GetFileName() const { return m_filename; }
4032
private:
4133
bool m_deleting;
4234
std::wstring m_filename;
43-
std::thread m_thread;
44-
45-
private:
46-
DeleteFile_(const DeleteFile_&);
47-
DeleteFile_&operator=(const DeleteFile_&);
48-
4935
};
5036
static std::deque<DeleteFile_> m_after_delete;
5137
};

internal/dll_project/ohtorii_tools/ohtorii_tools/parallel_container.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ class ParallelVector : public std::vector<typename _TYPE>, public concurrency::c
88
public:
99
typedef concurrency::critical_section::scoped_lock scoped_lock;
1010

11+
ParallelVector() {};
12+
ParallelVector(const ParallelVector&) = delete;
13+
ParallelVector& operator=(const ParallelVector& rht) = delete;
1114
private:
1215

1316
};

internal/dll_project/ohtorii_tools/ohtorii_tools/unity.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -173,15 +173,7 @@ void Unity::Destroy() {
173173
}
174174
m_kinds.Clear();
175175
m_current_instance_index = 0;
176-
177-
//
178-
//memo: ASyncFileでファイルを閉じた後で終了処理を行う。
179-
//そうしないと、ASyncFileがファイルを開いているためファイルの削除が出来ない。
180-
//
181-
//今のところ削除するファイルは1個でパフォーマンスに大きな影響は無いため対策は行わないことにした。
182-
//
183-
File::StartDestroy();
184-
File::JoinDestroy();
176+
File::Destroy();
185177
}
186178

187179
Sources& Unity::QuerySources(){
@@ -281,6 +273,12 @@ bool Unity::AutoPreviewRegist(const WCHAR*filename) {
281273
return m_recurring_task.Register<AutoPreview>(filename);
282274
}
283275

276+
bool Unity::CandidatesClearWithSourceName(const WCHAR* source_name) {
277+
//初めに、候補を生成しているASyncFileskから削除する
278+
QueryASyncFiles().DestrotFromSourceName(source_name);
279+
QueryCandidates().ClearWithSourceName(source_name);
280+
}
281+
284282
const WCHAR* Unity::GetInteractiveSourceNames(const WCHAR* separator) {
285283
m_last_interactive_source_names.clear();
286284
bool first = true;

0 commit comments

Comments
 (0)