Fix typos
[TortoiseGit.git] / src / TGitCache / FolderCrawler.h
blob73fa04d951619c1ba15f6c9413b1e8b53664123d
1 // TortoiseGit - a Windows shell extension for easy version control
3 // External Cache Copyright (C) 2005-2007, 2009-2011, 2014 TortoiseSVN
4 // Copyright (C) 2008-2012, 2023 - TortoiseGit
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #pragma once
22 #include "TGitPath.h"
23 #include "CacheInterface.h"
24 #include "SmartHandle.h"
25 #include "UniqueQueue.h"
26 #include <set>
27 //////////////////////////////////////////////////////////////////////////
31 #pragma pack(push, r1, 16)
33 /**
34 * \ingroup TGitCache
35 * Helper class to crawl folders in the background (in a separate thread)
36 * so that the main cache isn't blocked until all the status are fetched.
38 class CFolderCrawler
40 public:
41 CFolderCrawler();
42 ~CFolderCrawler();
44 public:
45 void Initialise();
46 void AddDirectoryForUpdate(const CTGitPath& path);
47 void AddPathForUpdate(const CTGitPath& path);
48 void ReleasePathForUpdate(const CTGitPath& path);
49 void Stop();
50 bool SetHoldoff(DWORD milliseconds = 100);
51 void BlockPath(const CTGitPath& path, DWORD ticks = 0);
52 private:
53 static unsigned int __stdcall ThreadEntry(void* pContext);
54 void WorkerThread();
56 private:
57 CComAutoCriticalSection m_critSec;
58 CAutoGeneralHandle m_hThread;
59 UniqueQueue<CTGitPath> m_foldersToUpdate;
60 UniqueQueue<CTGitPath> m_pathsToUpdate;
61 UniqueQueue<CTGitPath> m_pathsToRelease;
63 CAutoGeneralHandle m_hTerminationEvent;
64 CAutoGeneralHandle m_hWakeEvent;
66 // This will be *asynchronously* modified by CCrawlInhibitor.
67 // So, we have to mark it volatile, preparing compiler and
68 // optimizer for the "worst".
69 volatile LONG m_lCrawlInhibitSet = 0;
71 // While the shell is still asking for items, we don't
72 // want to start crawling. This timer is pushed-out for
73 // every shell request, and stops us crawling until
74 // a bit of quiet time has elapsed
75 LONGLONG m_crawlHoldoffReleasesAt;
76 bool m_bItemsAddedSinceLastCrawl = false;
77 bool m_bPathsAddedSinceLastCrawl = false;
79 CTGitPath m_blockedPath;
80 ULONGLONG m_blockReleasesAt = 0;
81 bool m_bRun = false;
84 friend class CCrawlInhibitor;
88 //////////////////////////////////////////////////////////////////////////
91 class CCrawlInhibitor
93 private:
94 CCrawlInhibitor(); // Not defined
95 public:
96 explicit CCrawlInhibitor(CFolderCrawler* pCrawler)
98 m_pCrawler = pCrawler;
100 // Count locks instead of a mere flag toggle (exchange)
101 // to allow for multiple, concurrent locks.
102 ::InterlockedIncrement(&m_pCrawler->m_lCrawlInhibitSet);
104 ~CCrawlInhibitor()
106 ::InterlockedDecrement(&m_pCrawler->m_lCrawlInhibitSet);
107 m_pCrawler->SetHoldoff();
109 private:
110 CFolderCrawler* m_pCrawler;
113 #pragma pack(pop, r1)