Skip to content

Commit e8cfb96

Browse files
committed
simplify code
1 parent 2e56ef1 commit e8cfb96

7 files changed

Lines changed: 23 additions & 146 deletions

XG_CancelGenBlacksDialog.hpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,10 @@ class XG_CancelGenBlacksDialog : public XG_Dialog
2727
xg_bCancelled = true;
2828
}
2929
::LeaveCriticalSection(&xg_csLock);
30-
m_cancellation_manager.SetCompleted();
31-
// スレッドを待つ(タイムアウト付き)。
32-
if (!m_cancellation_manager.WaitForCompletion(5000)) {
33-
// タイムアウトの場合は通常の待機。
34-
XgWaitForThreads();
35-
}
30+
// スレッドを待つ
31+
XgWaitForThreads();
3632
// キャンセル時に初期状態に復元。
3733
m_cancellation_manager.RestoreOnCancel();
38-
// グローバルポインタをクリア。
39-
xg_pCancellationManager = nullptr;
4034
// スレッドを閉じる。
4135
XgCloseThreads();
4236
}
@@ -49,8 +43,6 @@ class XG_CancelGenBlacksDialog : public XG_Dialog
4943
m_cancellation_manager.Reset();
5044
// 初期状態を保存。
5145
m_cancellation_manager.SaveInitialState();
52-
// グローバルポインタを設定。
53-
xg_pCancellationManager = &m_cancellation_manager;
5446
// 解を求めるのを開始。
5547
XgStartGenerateBlacks();
5648
// リトライ回数をリセット。

XG_CancelSmartSolveDialog.hpp

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ class XG_CancelSmartSolveDialog : public XG_Dialog
2525
m_cancellation_manager.Reset();
2626
// 初期状態を保存。
2727
m_cancellation_manager.SaveInitialState();
28-
// グローバルポインタを設定。
29-
xg_pCancellationManager = &m_cancellation_manager;
3028
XgStartSolve_Smart();
3129
// タイマーをセットする。
3230
::SetTimer(hwnd, uTimerID, INTERVAL, nullptr);
@@ -43,16 +41,10 @@ class XG_CancelSmartSolveDialog : public XG_Dialog
4341
xg_bCancelled = true;
4442
}
4543
::LeaveCriticalSection(&xg_csLock);
46-
m_cancellation_manager.SetCompleted();
47-
// スレッドを待つ(タイムアウト付き)。
48-
if (!m_cancellation_manager.WaitForCompletion(5000)) {
49-
// タイムアウトの場合は通常の待機。
50-
XgWaitForThreads();
51-
}
44+
// スレッドを待つ
45+
XgWaitForThreads();
5246
// キャンセル時に初期状態に復元。
5347
m_cancellation_manager.RestoreOnCancel();
54-
// グローバルポインタをクリア。
55-
xg_pCancellationManager = nullptr;
5648
// スレッドを閉じる。
5749
XgCloseThreads();
5850
}
@@ -68,14 +60,8 @@ class XG_CancelSmartSolveDialog : public XG_Dialog
6860
xg_bCancelled = true;
6961
}
7062
::LeaveCriticalSection(&xg_csLock);
71-
m_cancellation_manager.SetCompleted();
72-
// スレッドを待つ(タイムアウト付き)。
73-
if (!m_cancellation_manager.WaitForCompletion(5000)) {
74-
// タイムアウトの場合は通常の待機。
75-
XgWaitForThreads();
76-
}
77-
// グローバルポインタをクリア(リトライ時は状態復元しない)。
78-
xg_pCancellationManager = nullptr;
63+
// スレッドを待つ
64+
XgWaitForThreads();
7965
// スレッドを閉じる。
8066
XgCloseThreads();
8167

XG_CancelSolveDialog.hpp

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ class XG_CancelSolveDialog : public XG_Dialog
2323
m_cancellation_manager.Reset();
2424
// 初期状態を保存。
2525
m_cancellation_manager.SaveInitialState();
26-
// グローバルポインタを設定。
27-
xg_pCancellationManager = &m_cancellation_manager;
2826
// 解を求めるのを開始。
2927
XgStartSolve_AddBlack();
3028
// タイマーをセットする。
@@ -42,16 +40,10 @@ class XG_CancelSolveDialog : public XG_Dialog
4240
xg_bCancelled = true;
4341
}
4442
::LeaveCriticalSection(&xg_csLock);
45-
m_cancellation_manager.SetCompleted();
46-
// スレッドを待つ(タイムアウト付き)。
47-
if (!m_cancellation_manager.WaitForCompletion(5000)) {
48-
// タイムアウトの場合は通常の待機。
49-
XgWaitForThreads();
50-
}
43+
// スレッドを待つ
44+
XgWaitForThreads();
5145
// キャンセル時に初期状態に復元。
5246
m_cancellation_manager.RestoreOnCancel();
53-
// グローバルポインタをクリア。
54-
xg_pCancellationManager = nullptr;
5547
// スレッドを閉じる。
5648
XgCloseThreads();
5749
}
@@ -68,14 +60,8 @@ class XG_CancelSolveDialog : public XG_Dialog
6860
xg_bCancelled = true;
6961
}
7062
::LeaveCriticalSection(&xg_csLock);
71-
m_cancellation_manager.SetCompleted();
72-
// スレッドを待つ(タイムアウト付き)。
73-
if (!m_cancellation_manager.WaitForCompletion(5000)) {
74-
// タイムアウトの場合は通常の待機。
75-
XgWaitForThreads();
76-
}
77-
// グローバルポインタをクリア(リトライ時は状態復元しない)。
78-
xg_pCancellationManager = nullptr;
63+
// スレッドを待つ
64+
XgWaitForThreads();
7965
// スレッドを閉じる。
8066
XgCloseThreads();
8167

XG_CancelSolveNoAddBlackDialog.hpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ class XG_CancelSolveNoAddBlackDialog : public XG_Dialog
2323
m_cancellation_manager.Reset();
2424
// 初期状態を保存。
2525
m_cancellation_manager.SaveInitialState();
26-
// グローバルポインタを設定。
27-
xg_pCancellationManager = &m_cancellation_manager;
2826
// スマート解決なら、黒マスを生成する。
2927
XgStartSolve_NoAddBlack();
3028
// タイマーをセットする。
@@ -42,16 +40,10 @@ class XG_CancelSolveNoAddBlackDialog : public XG_Dialog
4240
xg_bCancelled = true;
4341
}
4442
::LeaveCriticalSection(&xg_csLock);
45-
m_cancellation_manager.SetCompleted();
46-
// スレッドを待つ(タイムアウト付き)。
47-
if (!m_cancellation_manager.WaitForCompletion(5000)) {
48-
// タイムアウトの場合は通常の待機。
49-
XgWaitForThreads();
50-
}
43+
// スレッドを待つ
44+
XgWaitForThreads();
5145
// キャンセル時に初期状態に復元。
5246
m_cancellation_manager.RestoreOnCancel();
53-
// グローバルポインタをクリア。
54-
xg_pCancellationManager = nullptr;
5547
// スレッドを閉じる。
5648
XgCloseThreads();
5749
}

XG_CancellationManager.hpp

Lines changed: 11 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -12,83 +12,36 @@
1212
// Lightweight cancellation manager to ensure stable state during cancellation
1313
// without impacting execution speed.
1414

15-
class XG_CancellationManager
16-
{
15+
class XG_CancellationManager {
1716
private:
18-
HANDLE m_hCompletionEvent; // 完了通知用イベント(completion event for thread waiting only)
1917
XG_Board m_initial_board; // 初期状態(復元用)(initial state for restoration)
20-
18+
2119
public:
22-
// コンストラクタ
23-
XG_CancellationManager() noexcept
24-
: m_hCompletionEvent(nullptr)
25-
{
26-
// 手動リセット、初期状態は非シグナル状態のイベントを作成
27-
// Create manual-reset event, initially non-signaled
28-
m_hCompletionEvent = ::CreateEventW(nullptr, TRUE, FALSE, nullptr);
29-
}
30-
31-
// デストラクタ
32-
~XG_CancellationManager() noexcept
33-
{
34-
if (m_hCompletionEvent != nullptr) {
35-
::CloseHandle(m_hCompletionEvent);
36-
m_hCompletionEvent = nullptr;
37-
}
38-
}
39-
20+
XG_CancellationManager() noexcept { }
21+
~XG_CancellationManager() noexcept { }
22+
4023
// 初期状態を保存
4124
// Save initial state for potential restoration
42-
void SaveInitialState() noexcept
43-
{
25+
void SaveInitialState() noexcept {
4426
m_initial_board = xg_xword;
4527
}
46-
47-
// 完了を通知(処理成功時)
48-
// Notify completion (on successful processing)
49-
void SetCompleted() noexcept
50-
{
51-
::EnterCriticalSection(&xg_csLock);
52-
// キャンセル済みでなければ完了を通知
53-
if (!xg_bCancelled && m_hCompletionEvent != nullptr) {
54-
::SetEvent(m_hCompletionEvent);
55-
}
56-
::LeaveCriticalSection(&xg_csLock);
57-
}
58-
59-
// 完了またはキャンセルを待機(タイムアウト付き)
60-
// Wait for completion or cancellation with timeout
61-
// Returns true if completed normally, false if timeout or cancelled
62-
bool WaitForCompletion(DWORD dwMilliseconds = 5000) const noexcept
63-
{
64-
if (m_hCompletionEvent == nullptr)
65-
return false;
66-
67-
DWORD dwResult = ::WaitForSingleObject(m_hCompletionEvent, dwMilliseconds);
68-
return (dwResult == WAIT_OBJECT_0);
69-
}
70-
28+
7129
// キャンセル時に初期状態に復元
7230
// Restore initial state on cancellation
73-
void RestoreOnCancel() noexcept
74-
{
31+
void RestoreOnCancel() noexcept {
7532
// キャンセルされた場合のみ復元
7633
// Only restore if cancelled
7734
if (xg_bCancelled && !xg_bSolved) {
7835
xg_xword = m_initial_board;
7936
}
8037
}
81-
38+
8239
// リセット(再利用時)
8340
// Reset for reuse
84-
void Reset() noexcept
85-
{
86-
if (m_hCompletionEvent != nullptr) {
87-
::ResetEvent(m_hCompletionEvent);
88-
}
41+
void Reset() noexcept {
8942
m_initial_board.clear();
9043
}
91-
44+
9245
// コピー禁止
9346
XG_CancellationManager(const XG_CancellationManager&) = delete;
9447
XG_CancellationManager& operator=(const XG_CancellationManager&) = delete;

XWordGiver.cpp

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,6 @@ XGStringW xg_strNotes;
5959
// 排他制御のためのクリティカルセクション(ロック)。
6060
CRITICAL_SECTION xg_csLock;
6161

62-
// キャンセル管理マネージャー(現在アクティブなもの)。
63-
XG_CancellationManager* xg_pCancellationManager = nullptr;
64-
6562
// キャレットの位置。
6663
XG_Pos xg_caret_pos = {0, 0};
6764

@@ -3944,10 +3941,6 @@ void __fastcall XgSolveXWord_AddBlackRecurse(const XG_Board& xw)
39443941
if (xw.GetAt(i) == ZEN_BLACK)
39453942
xg_xword.SetAt(i, ZEN_BLACK);
39463943
}
3947-
// 完了を通知。
3948-
if (xg_pCancellationManager != nullptr) {
3949-
xg_pCancellationManager->SetCompleted();
3950-
}
39513944
}
39523945
::LeaveCriticalSection(&xg_csLock);
39533946
}
@@ -4169,7 +4162,6 @@ void __fastcall XgSolveXWord_NoAddBlackRecurse(const XG_Board& xw)
41694162
}
41704163

41714164
// 解かどうか?
4172-
bool should_set_completed = false;
41734165
::EnterCriticalSection(&xg_csLock);
41744166
if (!xg_bCancelled && !xg_bSolved && xw.IsSolution()) { // 解だった。
41754167
xg_bSolved = true;
@@ -4183,15 +4175,8 @@ void __fastcall XgSolveXWord_NoAddBlackRecurse(const XG_Board& xw)
41834175
if (xw.GetAt(i) == ZEN_BLACK)
41844176
xg_xword.SetAt(i, ZEN_BLACK);
41854177
}
4186-
4187-
should_set_completed = (xg_pCancellationManager != nullptr);
41884178
}
41894179
::LeaveCriticalSection(&xg_csLock);
4190-
4191-
// 完了を通知(ロックの外で)。
4192-
if (should_set_completed) {
4193-
xg_pCancellationManager->SetCompleted();
4194-
}
41954180
}
41964181

41974182
// 縦と横を入れ替える。
@@ -7379,10 +7364,6 @@ bool __fastcall XgGenerateBlacksRecurse(const XG_Board& xword, LONG iRowjCol)
73797364
if (!xg_bCancelled && !xg_bBlacksGenerated) {
73807365
xg_bBlacksGenerated = true;
73817366
xg_xword = xword;
7382-
// 完了を通知。
7383-
if (xg_pCancellationManager != nullptr) {
7384-
xg_pCancellationManager->SetCompleted();
7385-
}
73867367
}
73877368
::LeaveCriticalSection(&xg_csLock);
73887369
return xg_bBlacksGenerated || xg_bCancelled;
@@ -7501,10 +7482,6 @@ bool __fastcall XgGenerateBlacksPointSymRecurse(const XG_Board& xword, LONG iRow
75017482
if (!xg_bCancelled && !xg_bBlacksGenerated) {
75027483
xg_bBlacksGenerated = true;
75037484
xg_xword = xword;
7504-
// 完了を通知。
7505-
if (xg_pCancellationManager != nullptr) {
7506-
xg_pCancellationManager->SetCompleted();
7507-
}
75087485
}
75097486
::LeaveCriticalSection(&xg_csLock);
75107487
return xg_bBlacksGenerated || xg_bCancelled;
@@ -7626,10 +7603,6 @@ bool __fastcall XgGenerateBlacksLineSymVRecurse(const XG_Board& xword, LONG iRow
76267603
if (!xg_bCancelled && !xg_bBlacksGenerated) {
76277604
xg_bBlacksGenerated = true;
76287605
xg_xword = xword;
7629-
// 完了を通知。
7630-
if (xg_pCancellationManager != nullptr) {
7631-
xg_pCancellationManager->SetCompleted();
7632-
}
76337606
}
76347607
::LeaveCriticalSection(&xg_csLock);
76357608
return xg_bBlacksGenerated || xg_bCancelled;
@@ -7766,10 +7739,6 @@ bool __fastcall XgGenerateBlacksLineSymHRecurse(const XG_Board& xword, LONG iRow
77667739
if (!xg_bCancelled && !xg_bBlacksGenerated) {
77677740
xg_bBlacksGenerated = true;
77687741
xg_xword = xword;
7769-
// 完了を通知。
7770-
if (xg_pCancellationManager != nullptr) {
7771-
xg_pCancellationManager->SetCompleted();
7772-
}
77737742
}
77747743
::LeaveCriticalSection(&xg_csLock);
77757744
return xg_bBlacksGenerated || xg_bCancelled;

XWordGiver.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1398,7 +1398,6 @@ extern CRITICAL_SECTION xg_csLock;
13981398

13991399
// キャンセル管理マネージャー(前方宣言)。
14001400
class XG_CancellationManager;
1401-
extern XG_CancellationManager* xg_pCancellationManager;
14021401

14031402
// スレッドの数。
14041403
extern DWORD xg_dwThreadCount;

0 commit comments

Comments
 (0)