From dc1593d60f63aa951c1463ade13c97cfde94d2f5 Mon Sep 17 00:00:00 2001 From: Mounir IDRASSI Date: Thu, 24 Mar 2016 01:34:55 +0100 Subject: Windows: reduce CPU usage by caching WNetGetConnection calls result for 2 seconds. --- src/Common/Dlgcode.c | 51 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 37 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/Common/Dlgcode.c b/src/Common/Dlgcode.c index 24f93559..2cf5bb8e 100644 --- a/src/Common/Dlgcode.c +++ b/src/Common/Dlgcode.c @@ -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 @@ -6628,26 +6635,42 @@ DWORD GetUsedLogicalDrives (void) DWORD dwUsedDrives = GetLogicalDrives(); if (!bShowDisconnectedNetworkDrives) { - /* 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; - for (WCHAR i = 0; i <= MAX_MOUNTED_VOLUME_DRIVE_NUMBER; i++) + 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) { - 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; - dwLen = ARRAYSIZE (remotePath); - status = WNetGetConnection (drive, remotePath, &dwLen); - if ((NO_ERROR == status) || (status == ERROR_CONNECTION_UNAVAIL)) + if ((dwUsedDrives & (1 << i)) == 0) { - /* this is a mapped network share, mark it as used */ - dwUsedDrives |= (1 << i); + drive[0] = L'A' + 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; -- cgit v1.2.3