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.
void RemovableDriveManager::update()
{
std::vector<DriveData> current_drives = this->search_for_removable_drives();
// Post update events.
tbb::mutex::scoped_lock lock(m_drives_mutex);
std::sort(current_drives.begin(), current_drives.end());
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));
tbb::mutex::scoped_lock inside_update_lock;
if (inside_update_lock.try_acquire(m_inside_update_mutex)) {
// Got the lock without waiting. That means, the update was not running.
// Run the update.
std::vector<DriveData> current_drives = this->search_for_removable_drives();
// Post update events.
tbb::mutex::scoped_lock lock(m_drives_mutex);
std::sort(current_drives.begin(), current_drives.end());
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

View File

@ -102,6 +102,8 @@ private:
// sorted ascending by path
std::vector<DriveData> m_current_drives;
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.
std::string get_removable_drive_from_path(const std::string& path);