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
);
30 m_bItemsAddedSinceLastUpdate
= false;
33 CShellUpdater::~CShellUpdater(void)
38 void CShellUpdater::Stop()
40 InterlockedExchange(&m_bRunning
, FALSE
);
41 if (m_hTerminationEvent
)
43 SetEvent(m_hTerminationEvent
);
44 if(WaitForSingleObject(m_hThread
, 200) != WAIT_OBJECT_0
)
46 ATLTRACE("Error terminating shell updater thread\n");
49 m_hThread
.CloseHandle();
50 m_hTerminationEvent
.CloseHandle();
51 m_hWakeEvent
.CloseHandle();
54 void CShellUpdater::Initialise()
56 // Don't call Initialize more than once
57 ATLASSERT(!m_hThread
);
59 // Just start the worker thread.
60 // It will wait for event being signaled.
61 // If m_hWakeEvent is already signaled the worker thread
62 // will behave properly (with normal priority at worst).
64 InterlockedExchange(&m_bRunning
, TRUE
);
65 unsigned int threadId
;
66 m_hThread
= (HANDLE
)_beginthreadex(NULL
,0,ThreadEntry
,this,0,&threadId
);
67 SetThreadPriority(m_hThread
, THREAD_PRIORITY_LOWEST
);
70 void CShellUpdater::AddPathForUpdate(const CTGitPath
& path
)
73 AutoLocker
lock(m_critSec
);
74 for(int i
=0;i
<m_pathsToUpdate
.size();i
++)
76 if(m_pathsToUpdate
[i
] == path
)
80 //ATLTRACE(_T("Add Path for Update : %s\n"), path.GetWinPath());
82 m_pathsToUpdate
.push_back(path
);
84 // set this flag while we are synced
85 // with the worker thread
86 m_bItemsAddedSinceLastUpdate
= true;
89 SetEvent(m_hWakeEvent
);
93 unsigned int CShellUpdater::ThreadEntry(void* pContext
)
95 ((CShellUpdater
*)pContext
)->WorkerThread();
99 void CShellUpdater::WorkerThread()
101 HANDLE hWaitHandles
[2];
102 hWaitHandles
[0] = m_hTerminationEvent
;
103 hWaitHandles
[1] = m_hWakeEvent
;
107 DWORD waitResult
= WaitForMultipleObjects(_countof(hWaitHandles
), hWaitHandles
, FALSE
, INFINITE
);
109 // exit event/working loop if the first event (m_hTerminationEvent)
110 // has been signaled or if one of the events has been abandoned
111 // (i.e. ~CShellUpdater() is being executed)
112 if(waitResult
== WAIT_OBJECT_0
|| waitResult
== WAIT_ABANDONED_0
|| waitResult
== WAIT_ABANDONED_0
+1)
117 // wait some time before we notify the shell
121 CTGitPath workingPath
;
126 AutoLocker
lock(m_critSec
);
127 if(m_pathsToUpdate
.empty())
129 // Nothing left to do
133 if(m_bItemsAddedSinceLastUpdate
)
135 m_pathsToUpdate
.erase(std::unique(m_pathsToUpdate
.begin(), m_pathsToUpdate
.end(), &CTGitPath::PredLeftEquivalentToRight
), m_pathsToUpdate
.end());
136 m_bItemsAddedSinceLastUpdate
= false;
139 workingPath
= m_pathsToUpdate
.front();
140 m_pathsToUpdate
.pop_front();
142 if (workingPath
.IsEmpty())
144 ATLTRACE(_T("Update notifications for: %s\n"), workingPath
.GetWinPath());
145 if (workingPath
.IsDirectory())
147 // check if the path is monitored by the watcher. If it isn't, then we have to invalidate the cache
148 // for that path and add it to the watcher.
149 if (!CGitStatusCache::Instance().IsPathWatched(workingPath
))
151 if (workingPath
.HasAdminDir())
152 CGitStatusCache::Instance().AddPathToWatch(workingPath
);
154 // first send a notification about a sub folder change, so explorer doesn't discard
155 // the folder notification. Since we only know for sure that the subversion admin
156 // dir is present, we send a notification for that folder.
157 CString admindir
= workingPath
.GetWinPathString() + _T("\\") + g_GitAdminDir
.GetAdminDirName();
158 if(::PathFileExists(admindir
))
159 SHChangeNotify(SHCNE_UPDATEITEM
, SHCNF_PATH
| SHCNF_FLUSHNOWAIT
, (LPCTSTR
)admindir
, NULL
);
161 SHChangeNotify(SHCNE_UPDATEITEM
, SHCNF_PATH
| SHCNF_FLUSHNOWAIT
, workingPath
.GetWinPath(), NULL
);
162 // Sending an UPDATEDIR notification somehow overwrites/deletes the UPDATEITEM message. And without
163 // that message, the folder overlays in the current view don't get updated without hitting F5.
164 // Drawback is, without UPDATEDIR, the left tree view isn't always updated...
166 SHChangeNotify(SHCNE_UPDATEDIR
, SHCNF_PATH
| SHCNF_FLUSHNOWAIT
, workingPath
.GetWinPath(), NULL
);
169 SHChangeNotify(SHCNE_UPDATEITEM
, SHCNF_PATH
| SHCNF_FLUSHNOWAIT
, workingPath
.GetWinPath(), NULL
);