1
0
mirror of https://github.com/veracrypt/VeraCrypt.git synced 2025-11-11 11:08:02 -06:00

Windows: reduce CPU usage by caching WNetGetConnection calls result for 2 seconds.

This commit is contained in:
Mounir IDRASSI
2016-03-24 01:34:55 +01:00
parent 1e204da223
commit dc1593d60f

View File

@@ -158,6 +158,9 @@ volatile HANDLE hDriverSetupMutex = NULL;
of the TrueCrypt installer is running (which is also useful for enforcing restart before the apps can be used). */ of the TrueCrypt installer is running (which is also useful for enforcing restart before the apps can be used). */
volatile HANDLE hAppSetupMutex = NULL; volatile HANDLE hAppSetupMutex = NULL;
/* Critical section used to protect access to global variables used in WNetGetConnection calls */
CRITICAL_SECTION csWNetCalls;
HINSTANCE hInst = NULL; HINSTANCE hInst = NULL;
HCURSOR hCursor = NULL; HCURSOR hCursor = NULL;
@@ -382,6 +385,8 @@ void cleanup ()
EncryptionThreadPoolStop(); EncryptionThreadPoolStop();
#endif #endif
DeleteCriticalSection (&csWNetCalls);
} }
@@ -2497,6 +2502,8 @@ void InitApp (HINSTANCE hInstance, wchar_t *lpszCommandLine)
InitOSVersionInfo(); InitOSVersionInfo();
InitializeCriticalSection (&csWNetCalls);
LoadSystemDll (L"ntmarta.dll", &hntmartadll, TRUE, SRC_POS); LoadSystemDll (L"ntmarta.dll", &hntmartadll, TRUE, SRC_POS);
LoadSystemDll (L"MPR.DLL", &hmprdll, TRUE, SRC_POS); LoadSystemDll (L"MPR.DLL", &hmprdll, TRUE, SRC_POS);
#ifdef SETUP #ifdef SETUP
@@ -6628,26 +6635,42 @@ DWORD GetUsedLogicalDrives (void)
DWORD dwUsedDrives = GetLogicalDrives(); DWORD dwUsedDrives = GetLogicalDrives();
if (!bShowDisconnectedNetworkDrives) if (!bShowDisconnectedNetworkDrives)
{ {
/* detect disconnected mapped network shares and removed static DWORD g_dwLastMappedDrives = 0;
* their associated drives from the list static time_t g_lastCallTime = 0;
*/
WCHAR remotePath[512]; EnterCriticalSection (&csWNetCalls);
WCHAR drive[3] = {L'A', L':', 0};
DWORD dwLen, status; finally_do ({ LeaveCriticalSection (&csWNetCalls); });
for (WCHAR i = 0; i <= MAX_MOUNTED_VOLUME_DRIVE_NUMBER; i++)
/* update values every 2 seconds to reduce CPU consumption */
if ((time (NULL) - g_lastCallTime) > 2)
{ {
if ((dwUsedDrives & (1 << i)) == 0) /* detect disconnected mapped network shares and removed
* their associated drives from the list
*/
WCHAR remotePath[512];
WCHAR drive[3] = {L'A', L':', 0};
DWORD dwLen, status;
g_dwLastMappedDrives = 0;
for (WCHAR i = 0; i <= MAX_MOUNTED_VOLUME_DRIVE_NUMBER; i++)
{ {
drive[0] = L'A' + i; if ((dwUsedDrives & (1 << i)) == 0)
dwLen = ARRAYSIZE (remotePath);
status = WNetGetConnection (drive, remotePath, &dwLen);
if ((NO_ERROR == status) || (status == ERROR_CONNECTION_UNAVAIL))
{ {
/* this is a mapped network share, mark it as used */ drive[0] = L'A' + i;
dwUsedDrives |= (1 << i); dwLen = ARRAYSIZE (remotePath);
status = WNetGetConnection (drive, remotePath, &dwLen);
if ((NO_ERROR == status) || (status == ERROR_CONNECTION_UNAVAIL))
{
/* this is a mapped network share, mark it as used */
g_dwLastMappedDrives |= (1 << i);
}
} }
} }
g_lastCallTime = time (NULL);
} }
dwUsedDrives |= g_dwLastMappedDrives;
} }
return dwUsedDrives; return dwUsedDrives;