Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure ExternalInputDeviceHub::remove_observer() synchronizes memory across threads #360

Merged
merged 3 commits into from May 11, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/server/input/default_input_device_hub.cpp
Expand Up @@ -88,26 +88,25 @@ void mi::ExternalInputDeviceHub::remove_observer(std::weak_ptr<InputDeviceObserv
auto observer = obs.lock();
if (observer)
{
std::mutex mutex;
std::condition_variable cv;
std::atomic<bool> removed{false};
bool removed{false};

data->observer_queue->enqueue_with_guaranteed_execution(
[&,this]
{
// Unless remove() is on the same thread as add() external synchronization would be needed
data->observers.remove(observer);

// We do *not* take a lock and instead rely on removed being atomic,
// as enqueue_with_guaranteed_execution may run on our stack.
std::lock_guard<decltype(mutex)> lock{mutex};
removed = true;
cv.notify_one();
});

std::mutex mutex;
std::unique_lock<decltype(mutex)> lock{mutex};

// Before returning wait for the remove - otherwise notifications can still happen
cv.wait(lock, [&] { return removed.load(); });
cv.wait(lock, [&] { return removed; });
}
}

Expand Down