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). */
volatile HANDLE hAppSetupMutex = NULL;
/* Critical section used to protect access to global variables used in WNetGetConnection calls */
CRITICAL_SECTION csWNetCalls;
HINSTANCE hInst = NULL;
HCURSOR hCursor = NULL;
@ -382,6 +385,8 @@ void cleanup ()
EncryptionThreadPoolStop();
#endif
DeleteCriticalSection (&csWNetCalls);
}
@ -2497,6 +2502,8 @@ void InitApp (HINSTANCE hInstance, wchar_t *lpszCommandLine)
InitOSVersionInfo();
InitializeCriticalSection (&csWNetCalls);
LoadSystemDll (L"ntmarta.dll", &hntmartadll, TRUE, SRC_POS);
LoadSystemDll (L"MPR.DLL", &hmprdll, TRUE, SRC_POS);
#ifdef SETUP
@ -6627,6 +6634,16 @@ DWORD GetUsedLogicalDrives (void)
{
DWORD dwUsedDrives = GetLogicalDrives();
if (!bShowDisconnectedNetworkDrives)
{
static DWORD g_dwLastMappedDrives = 0;
static time_t g_lastCallTime = 0;
EnterCriticalSection (&csWNetCalls);
finally_do ({ LeaveCriticalSection (&csWNetCalls); });
/* update values every 2 seconds to reduce CPU consumption */
if ((time (NULL) - g_lastCallTime) > 2)
{
/* detect disconnected mapped network shares and removed
* their associated drives from the list
@ -6634,6 +6651,7 @@ DWORD GetUsedLogicalDrives (void)
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++)
{
if ((dwUsedDrives & (1 << i)) == 0)
@ -6644,10 +6662,15 @@ DWORD GetUsedLogicalDrives (void)
if ((NO_ERROR == status) || (status == ERROR_CONNECTION_UNAVAIL))
{
/* this is a mapped network share, mark it as used */
dwUsedDrives |= (1 << i);
g_dwLastMappedDrives |= (1 << i);
}
}
}
g_lastCallTime = time (NULL);
}
dwUsedDrives |= g_dwLastMappedDrives;
}
return dwUsedDrives;