From 06dc3fd1bcfb2a6a4aeff8f0cd7587ffc849d7ce Mon Sep 17 00:00:00 2001 From: Pranav Kant Date: Mon, 11 Jul 2016 10:01:11 +0530 Subject: [PATCH] loolwsd: Give editlock correctly when adding a new session This should be given to first editable(non-readonly) session. Change-Id: I3b6e53df4b2450cc799c4b4e6a800f662d90a2c1 --- loolwsd/DocumentBroker.cpp | 12 ++++++++---- loolwsd/DocumentBroker.hpp | 1 + 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/loolwsd/DocumentBroker.cpp b/loolwsd/DocumentBroker.cpp index 09644c33d..0bc67c178 100644 --- a/loolwsd/DocumentBroker.cpp +++ b/loolwsd/DocumentBroker.cpp @@ -113,6 +113,7 @@ DocumentBroker::DocumentBroker(const Poco::URI& uriPublic, _cursorPosY(0), _isLoaded(false), _isModified(false), + _isEditLockHeld(false), _tileVersion(0) { assert(!_docKey.empty()); @@ -345,12 +346,11 @@ size_t DocumentBroker::addSession(std::shared_ptr& session) { Log::debug("Adding a readonly session [" + id + "]"); } - // TODO: Below is not always true. What if readonly session is already opened - // In that case we still have to give edit lock to this *second* session. - else if (_sessions.size() == 1) + else if (!_isEditLockHeld) { - Log::debug("Giving editing lock to the first session [" + id + "]."); + Log::debug("Giving editing lock to the first editable session [" + id + "]."); _sessions.begin()->second->markEditLock(true); + _isEditLockHeld = true; } else { @@ -392,14 +392,18 @@ size_t DocumentBroker::removeSession(const std::string& id) if (haveEditLock) { // pass the edit lock to first non-readonly session in map + bool editLockGiven = false; for (auto& session: _sessions) { if (!session.second->isReadOnly()) { session.second->setEditLock(true); + editLockGiven = true; break; } } + + _isEditLockHeld = editLockGiven; } } diff --git a/loolwsd/DocumentBroker.hpp b/loolwsd/DocumentBroker.hpp index d1ef7c6de..203775f3a 100644 --- a/loolwsd/DocumentBroker.hpp +++ b/loolwsd/DocumentBroker.hpp @@ -257,6 +257,7 @@ private: mutable std::mutex _mutex; std::condition_variable _saveCV; std::mutex _saveMutex; + std::atomic _isEditLockHeld; /// Versioning is used to prevent races between /// painting and invalidation.