Skip to content

Commit 08d5b82

Browse files
committed
code review updates
1 parent bdb4e22 commit 08d5b82

3 files changed

Lines changed: 76 additions & 2 deletions

File tree

windows/runner/main.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ namespace {
1010

1111
constexpr const wchar_t kSingleInstanceMutexName[] =
1212
L"Local\\org.getlantern.lantern.single-instance";
13+
constexpr const wchar_t kSingleInstanceReadyEventName[] =
14+
L"Local\\org.getlantern.lantern.window-ready";
15+
constexpr DWORD kWindowReadyTimeoutMs = 5000;
16+
constexpr DWORD kActivationRetryTimeoutMs = 1000;
17+
constexpr DWORD kActivationRetryIntervalMs = 100;
1318

1419
} // namespace
1520

@@ -21,10 +26,14 @@ int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev,
2126
CreateAndAttachConsole();
2227
}
2328

29+
SingleInstanceReadyEvent window_ready(kSingleInstanceReadyEventName);
2430
SingleInstanceLock single_instance(kSingleInstanceMutexName);
2531
if (!single_instance.IsPrimaryInstance()) {
26-
ActivateExistingLanternWindow();
27-
return EXIT_SUCCESS;
32+
window_ready.Wait(kWindowReadyTimeoutMs);
33+
return ActivateExistingLanternWindowWithRetry(kActivationRetryTimeoutMs,
34+
kActivationRetryIntervalMs)
35+
? EXIT_SUCCESS
36+
: EXIT_FAILURE;
2837
}
2938
if (ActivateExistingLanternWindow()) {
3039
return EXIT_SUCCESS;
@@ -48,6 +57,7 @@ int APIENTRY wWinMain(_In_ HINSTANCE instance, _In_opt_ HINSTANCE prev,
4857
return EXIT_FAILURE;
4958
}
5059
window.SetQuitOnClose(true);
60+
window_ready.Signal();
5161

5262
::MSG msg;
5363
while (::GetMessage(&msg, nullptr, 0, 0)) {

windows/runner/single_instance.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,29 @@ bool SingleInstanceLock::IsPrimaryInstance() const {
6464
return is_primary_instance_;
6565
}
6666

67+
SingleInstanceReadyEvent::SingleInstanceReadyEvent(const wchar_t* event_name) {
68+
event_ = CreateEventW(nullptr, TRUE, FALSE, event_name);
69+
}
70+
71+
SingleInstanceReadyEvent::~SingleInstanceReadyEvent() {
72+
if (event_ != nullptr) {
73+
CloseHandle(event_);
74+
}
75+
}
76+
77+
void SingleInstanceReadyEvent::Signal() {
78+
if (event_ != nullptr) {
79+
SetEvent(event_);
80+
}
81+
}
82+
83+
bool SingleInstanceReadyEvent::Wait(DWORD timeout_ms) const {
84+
if (event_ == nullptr) {
85+
return false;
86+
}
87+
return WaitForSingleObject(event_, timeout_ms) == WAIT_OBJECT_0;
88+
}
89+
6790
bool ActivateExistingLanternWindow() {
6891
HWND existing_window = FindExistingLanternWindow();
6992
if (existing_window == nullptr) {
@@ -105,3 +128,27 @@ bool ActivateExistingLanternWindow() {
105128
}
106129
return true;
107130
}
131+
132+
bool ActivateExistingLanternWindowWithRetry(DWORD timeout_ms,
133+
DWORD retry_interval_ms) {
134+
const ULONGLONG deadline = GetTickCount64() + timeout_ms;
135+
136+
do {
137+
if (ActivateExistingLanternWindow()) {
138+
return true;
139+
}
140+
141+
const ULONGLONG now = GetTickCount64();
142+
if (now >= deadline) {
143+
break;
144+
}
145+
146+
DWORD sleep_ms = static_cast<DWORD>(deadline - now);
147+
if (sleep_ms > retry_interval_ms) {
148+
sleep_ms = retry_interval_ms;
149+
}
150+
Sleep(sleep_ms);
151+
} while (true);
152+
153+
return false;
154+
}

windows/runner/single_instance.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,23 @@ class SingleInstanceLock {
1818
bool is_primary_instance_ = true;
1919
};
2020

21+
class SingleInstanceReadyEvent {
22+
public:
23+
explicit SingleInstanceReadyEvent(const wchar_t* event_name);
24+
~SingleInstanceReadyEvent();
25+
26+
SingleInstanceReadyEvent(const SingleInstanceReadyEvent&) = delete;
27+
SingleInstanceReadyEvent& operator=(const SingleInstanceReadyEvent&) = delete;
28+
29+
void Signal();
30+
bool Wait(DWORD timeout_ms) const;
31+
32+
private:
33+
HANDLE event_ = nullptr;
34+
};
35+
2136
bool ActivateExistingLanternWindow();
37+
bool ActivateExistingLanternWindowWithRetry(DWORD timeout_ms,
38+
DWORD retry_interval_ms);
2239

2340
#endif // RUNNER_SINGLE_INSTANCE_H_

0 commit comments

Comments
 (0)