[SHELL32] CDirectoryWatcher: Check if window is alive (#6915)

Kill the zombie watchers for system
performance.
JIRA issue: CORE-13950
- Add HWND parameter to
  CDirectoryWatcher::Create.
- Check whether the HWND is alive at
  CDirectoryWatcher::IsDead.
This commit is contained in:
Katayama Hirofumi MZ 2024-05-22 20:33:26 +09:00 committed by GitHub
parent d7be7486c9
commit 8204c6e2fd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 24 additions and 16 deletions

View file

@ -199,7 +199,8 @@ CreateDirectoryWatcherFromRegEntry(LPREGENTRY pRegEntry)
return NULL;
// create a CDirectoryWatcher
CDirectoryWatcher *pDirectoryWatcher = CDirectoryWatcher::Create(szPath, pRegEntry->fRecursive);
CDirectoryWatcher *pDirectoryWatcher =
CDirectoryWatcher::Create(pRegEntry->hwnd, szPath, pRegEntry->fRecursive);
if (pDirectoryWatcher == NULL)
return NULL;

View file

@ -68,17 +68,18 @@ static void NTAPI _RequestAllTerminationAPC(ULONG_PTR Parameter)
s_hThreadAPC = NULL;
}
CDirectoryWatcher::CDirectoryWatcher(LPCWSTR pszDirectoryPath, BOOL fSubTree)
: m_fDead(FALSE)
CDirectoryWatcher::CDirectoryWatcher(HWND hNotifyWnd, LPCWSTR pszDirectoryPath, BOOL fSubTree)
: m_hNotifyWnd(hNotifyWnd)
, m_fDead(FALSE)
, m_fRecursive(fSubTree)
, m_dir_list(pszDirectoryPath, fSubTree)
{
TRACE("CDirectoryWatcher::CDirectoryWatcher: %p, '%S'\n", this, pszDirectoryPath);
TRACE("%p, '%S'\n", this, pszDirectoryPath);
lstrcpynW(m_szDirectoryPath, pszDirectoryPath, MAX_PATH);
GetFullPathNameW(pszDirectoryPath, _countof(m_szDirectoryPath), m_szDirectoryPath, NULL);
// open the directory to watch changes (for ReadDirectoryChangesW)
m_hDirectory = CreateFileW(pszDirectoryPath, FILE_LIST_DIRECTORY,
m_hDirectory = CreateFileW(m_szDirectoryPath, FILE_LIST_DIRECTORY,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL, OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED,
@ -86,12 +87,10 @@ CDirectoryWatcher::CDirectoryWatcher(LPCWSTR pszDirectoryPath, BOOL fSubTree)
}
/*static*/ CDirectoryWatcher *
CDirectoryWatcher::Create(LPCWSTR pszDirectoryPath, BOOL fSubTree)
CDirectoryWatcher::Create(HWND hNotifyWnd, LPCWSTR pszDirectoryPath, BOOL fSubTree)
{
WCHAR szFullPath[MAX_PATH];
GetFullPathNameW(pszDirectoryPath, _countof(szFullPath), szFullPath, NULL);
CDirectoryWatcher *pDirectoryWatcher = new CDirectoryWatcher(szFullPath, fSubTree);
CDirectoryWatcher *pDirectoryWatcher =
new CDirectoryWatcher(hNotifyWnd, pszDirectoryPath, fSubTree);
if (pDirectoryWatcher->m_hDirectory == INVALID_HANDLE_VALUE)
{
ERR("CreateFileW failed\n");
@ -103,7 +102,7 @@ CDirectoryWatcher::Create(LPCWSTR pszDirectoryPath, BOOL fSubTree)
CDirectoryWatcher::~CDirectoryWatcher()
{
TRACE("CDirectoryWatcher::~CDirectoryWatcher: %p, '%S'\n", this, m_szDirectoryPath);
TRACE("%p, '%S'\n", this, m_szDirectoryPath);
if (m_hDirectory != INVALID_HANDLE_VALUE)
CloseHandle(m_hDirectory);
@ -385,10 +384,17 @@ void CDirectoryWatcher::QuitWatching()
assert(this != NULL);
m_fDead = TRUE;
m_hNotifyWnd = NULL;
CancelIo(m_hDirectory);
}
BOOL CDirectoryWatcher::IsDead() const
BOOL CDirectoryWatcher::IsDead()
{
if (m_hNotifyWnd && !::IsWindow(m_hNotifyWnd))
{
m_hNotifyWnd = NULL;
m_fDead = TRUE;
CancelIo(m_hDirectory);
}
return m_fDead;
}

View file

@ -17,11 +17,11 @@ public:
HANDLE m_hDirectory;
WCHAR m_szDirectoryPath[MAX_PATH];
static CDirectoryWatcher *Create(LPCWSTR pszDirectoryPath, BOOL fSubTree);
static CDirectoryWatcher *Create(HWND hNotifyWnd, LPCWSTR pszDirectoryPath, BOOL fSubTree);
static void RequestAllWatchersTermination();
~CDirectoryWatcher();
BOOL IsDead() const;
BOOL IsDead();
BOOL RestartWatching();
void QuitWatching();
BOOL RequestAddWatcher();
@ -29,6 +29,7 @@ public:
void ReadCompletion(DWORD dwErrorCode, DWORD dwNumberOfBytesTransfered);
protected:
HWND m_hNotifyWnd;
BOOL m_fDead;
BOOL m_fRecursive;
CDirectoryList m_dir_list;
@ -36,5 +37,5 @@ protected:
BOOL CreateAPCThread();
void ProcessNotification();
CDirectoryWatcher(LPCWSTR pszDirectoryPath, BOOL fSubTree);
CDirectoryWatcher(HWND hNotifyWnd, LPCWSTR pszDirectoryPath, BOOL fSubTree);
};