1 // TortoiseGit - a Windows shell extension for easy version control
3 // External Cache Copyright (C) 2005-2008 - TortoiseSVN
4 // Copyright (C) 2008-2011 - 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.
23 #include "GitStatusCache.h"
25 CShellUpdater::CShellUpdater(void)
27 m_hWakeEvent
= CreateEvent(NULL
,FALSE
,FALSE
,NULL
);
28 m_hTerminationEvent
= CreateEvent(NULL
,TRUE
,FALSE
,NULL
);
29 m_hThread
= INVALID_HANDLE_VALUE
;
31 m_bItemsAddedSinceLastUpdate
= false;
34 CShellUpdater::~CShellUpdater(void)
39 void CShellUpdater::Stop()
41 InterlockedExchange(&m_bRunning
, FALSE
);
42 if (m_hTerminationEvent
!= INVALID_HANDLE_VALUE
)
44 SetEvent(m_hTerminationEvent
);
45 if(WaitForSingleObject(m_hThread
, 200) != WAIT_OBJECT_0
)
47 ATLTRACE("Error terminating shell updater thread\n");
49 CloseHandle(m_hThread
);
50 m_hThread
= INVALID_HANDLE_VALUE
;
51 CloseHandle(m_hTerminationEvent
);
52 m_hTerminationEvent
= INVALID_HANDLE_VALUE
;
53 CloseHandle(m_hWakeEvent
);
54 m_hWakeEvent
= INVALID_HANDLE_VALUE
;
58 void CShellUpdater::Initialise()
60 // Don't call Initialize more than once
61 ATLASSERT(m_hThread
== INVALID_HANDLE_VALUE
);
63 // Just start the worker thread.
64 // It will wait for event being signaled.
65 // If m_hWakeEvent is already signaled the worker thread
66 // will behave properly (with normal priority at worst).
68 InterlockedExchange(&m_bRunning
, TRUE
);
69 unsigned int threadId
;
70 m_hThread
= (HANDLE
)_beginthreadex(NULL
,0,ThreadEntry
,this,0,&threadId
);
71 SetThreadPriority(m_hThread
, THREAD_PRIORITY_LOWEST
);
74 void CShellUpdater::AddPathForUpdate(const CTGitPath
& path
)
77 AutoLocker
lock(m_critSec
);
78 for(int i
=0;i
<m_pathsToUpdate
.size();i
++)
80 if(m_pathsToUpdate
[i
] == path
)
84 //ATLTRACE(_T("Add Path for Update : %s\n"), path.GetWinPath());
86 m_pathsToUpdate
.push_back(path
);
88 // set this flag while we are synced
89 // with the worker thread
90 m_bItemsAddedSinceLastUpdate
= true;
93 SetEvent(m_hWakeEvent
);
97 unsigned int CShellUpdater::ThreadEntry(void* pContext
)
99 ((CShellUpdater
*)pContext
)->WorkerThread();
103 void CShellUpdater::WorkerThread()
105 HANDLE hWaitHandles
[2];
106 hWaitHandles
[0] = m_hTerminationEvent
;
107 hWaitHandles
[1] = m_hWakeEvent
;
111 DWORD waitResult
= WaitForMultipleObjects(sizeof(hWaitHandles
)/sizeof(hWaitHandles
[0]), hWaitHandles
, FALSE
, INFINITE
);
113 // exit event/working loop if the first event (m_hTerminationEvent)
114 // has been signaled or if one of the events has been abandoned
115 // (i.e. ~CShellUpdater() is being executed)
116 if(waitResult
== WAIT_OBJECT_0
|| waitResult
== WAIT_ABANDONED_0
|| waitResult
== WAIT_ABANDONED_0
+1)
121 // wait some time before we notify the shell
125 CTGitPath workingPath
;
130 AutoLocker
lock(m_critSec
);
131 if(m_pathsToUpdate
.empty())
133 // Nothing left to do
137 if(m_bItemsAddedSinceLastUpdate
)
139 m_pathsToUpdate
.erase(std::unique(m_pathsToUpdate
.begin(), m_pathsToUpdate
.end(), &CTGitPath::PredLeftEquivalentToRight
), m_pathsToUpdate
.end());
140 m_bItemsAddedSinceLastUpdate
= false;
143 workingPath
= m_pathsToUpdate
.front();
144 m_pathsToUpdate
.pop_front();
146 if (workingPath
.IsEmpty())
148 ATLTRACE(_T("Update notifications for: %s\n"), workingPath
.GetWinPath());
149 if (workingPath
.IsDirectory())
151 // check if the path is monitored by the watcher. If it isn't, then we have to invalidate the cache
152 // for that path and add it to the watcher.
153 if (!CGitStatusCache::Instance().IsPathWatched(workingPath
))
155 if (workingPath
.HasAdminDir())
156 CGitStatusCache::Instance().AddPathToWatch(workingPath
);
158 // first send a notification about a sub folder change, so explorer doesn't discard
159 // the folder notification. Since we only know for sure that the subversion admin
160 // dir is present, we send a notification for that folder.
161 CString admindir
= workingPath
.GetWinPathString() + _T("\\") + g_GitAdminDir
.GetAdminDirName();
162 if(::PathFileExists(admindir
))
163 SHChangeNotify(SHCNE_UPDATEITEM
, SHCNF_PATH
| SHCNF_FLUSHNOWAIT
, (LPCTSTR
)admindir
, NULL
);
165 SHChangeNotify(SHCNE_UPDATEITEM
, SHCNF_PATH
| SHCNF_FLUSHNOWAIT
, workingPath
.GetWinPath(), NULL
);
166 // Sending an UPDATEDIR notification somehow overwrites/deletes the UPDATEITEM message. And without
167 // that message, the folder overlays in the current view don't get updated without hitting F5.
168 // Drawback is, without UPDATEDIR, the left tree view isn't always updated...
170 //SHChangeNotify(SHCNE_UPDATEDIR, SHCNF_PATH | SHCNF_FLUSHNOWAIT, workingPath.GetWinPath(), NULL);
173 SHChangeNotify(SHCNE_UPDATEITEM
, SHCNF_PATH
| SHCNF_FLUSHNOWAIT
, workingPath
.GetWinPath(), NULL
);