Improvement in the RemovableDriveManager update function:

Don't call the update() if it is already running. Wait for the
other instance to finish instead.
This commit is contained in:
bubnikv 2020-03-12 11:30:51 +01:00
parent 29f2394ecb
commit c145d60df4
2 changed files with 20 additions and 10 deletions

View File

@ -487,17 +487,25 @@ RemovableDriveManager::RemovableDrivesStatus RemovableDriveManager::status()
// Update is called from thread_proc() and from most of the public methods on demand. // Update is called from thread_proc() and from most of the public methods on demand.
void RemovableDriveManager::update() void RemovableDriveManager::update()
{ {
std::vector<DriveData> current_drives = this->search_for_removable_drives(); tbb::mutex::scoped_lock inside_update_lock;
if (inside_update_lock.try_acquire(m_inside_update_mutex)) {
// Post update events. // Got the lock without waiting. That means, the update was not running.
tbb::mutex::scoped_lock lock(m_drives_mutex); // Run the update.
std::sort(current_drives.begin(), current_drives.end()); std::vector<DriveData> current_drives = this->search_for_removable_drives();
if (current_drives != m_current_drives) { // Post update events.
assert(m_callback_evt_handler); tbb::mutex::scoped_lock lock(m_drives_mutex);
if (m_callback_evt_handler) std::sort(current_drives.begin(), current_drives.end());
wxPostEvent(m_callback_evt_handler, RemovableDrivesChangedEvent(EVT_REMOVABLE_DRIVES_CHANGED)); if (current_drives != m_current_drives) {
assert(m_callback_evt_handler);
if (m_callback_evt_handler)
wxPostEvent(m_callback_evt_handler, RemovableDrivesChangedEvent(EVT_REMOVABLE_DRIVES_CHANGED));
}
m_current_drives = std::move(current_drives);
} else {
// Acquiring the m_iniside_update lock failed, therefore another update is running.
// Just block until the other instance of update() finishes.
inside_update_lock.acquire(m_inside_update_mutex);
} }
m_current_drives = std::move(current_drives);
} }
#ifndef REMOVABLE_DRIVE_MANAGER_OS_CALLBACKS #ifndef REMOVABLE_DRIVE_MANAGER_OS_CALLBACKS

View File

@ -102,6 +102,8 @@ private:
// sorted ascending by path // sorted ascending by path
std::vector<DriveData> m_current_drives; std::vector<DriveData> m_current_drives;
mutable tbb::mutex m_drives_mutex; mutable tbb::mutex m_drives_mutex;
// Locking the update() function to avoid that the function is executed multiple times.
mutable tbb::mutex m_inside_update_mutex;
// Returns drive path (same as path in DriveData) if exists otherwise empty string. // Returns drive path (same as path in DriveData) if exists otherwise empty string.
std::string get_removable_drive_from_path(const std::string& path); std::string get_removable_drive_from_path(const std::string& path);