CommitDlg: Update index using libgit2 incrementally
[TortoiseGit.git] / src / TGitCache / FolderCrawler.h
blobe3f6aa61111d11d934af127e5bcae9c20c13e968
1 // TortoiseGit - a Windows shell extension for easy version control
3 // External Cache Copyright (C) 2005-2007, 2009-2011 TortoiseSVN
4 // Copyright (C) 2008-2012 - 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(void);
42 ~CFolderCrawler(void);
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 void RemoveDuplicate(std::deque<CTGitPath> &list,const CTGitPath &path);
65 CAutoGeneralHandle m_hTerminationEvent;
66 CAutoGeneralHandle m_hWakeEvent;
68 // This will be *asynchronously* modified by CCrawlInhibitor.
69 // So, we have to mark it volatile, preparing compiler and
70 // optimizer for the "worst".
71 volatile LONG m_lCrawlInhibitSet;
73 // While the shell is still asking for items, we don't
74 // want to start crawling. This timer is pushed-out for
75 // every shell request, and stops us crawling until
76 // a bit of quiet time has elapsed
77 long m_crawlHoldoffReleasesAt;
78 bool m_bItemsAddedSinceLastCrawl;
79 bool m_bPathsAddedSinceLastCrawl;
81 CTGitPath m_blockedPath;
82 DWORD m_blockReleasesAt;
83 bool m_bRun;
86 friend class CCrawlInhibitor;
90 //////////////////////////////////////////////////////////////////////////
93 class CCrawlInhibitor
95 private:
96 CCrawlInhibitor(); // Not defined
97 public:
98 explicit CCrawlInhibitor(CFolderCrawler* pCrawler)
100 m_pCrawler = pCrawler;
102 // Count locks instead of a mere flag toggle (exchange)
103 // to allow for multiple, concurrent locks.
104 ::InterlockedIncrement(&m_pCrawler->m_lCrawlInhibitSet);
106 ~CCrawlInhibitor()
108 ::InterlockedDecrement(&m_pCrawler->m_lCrawlInhibitSet);
109 m_pCrawler->SetHoldoff();
111 private:
112 CFolderCrawler* m_pCrawler;
115 #pragma pack(pop, r1)