[Libreoffice-commits] online.git: common/MessageQueue.hpp kit/Kit.cpp kit/TestStubs.cpp test/Makefile.am

Libreoffice Gerrit user logerrit at kemper.freedesktop.org
Fri May 10 13:43:47 UTC 2019


 common/MessageQueue.hpp |    9 ++++++
 kit/Kit.cpp             |   66 +++++++++++++++++++++---------------------------
 kit/TestStubs.cpp       |   21 +++++++++++++++
 test/Makefile.am        |    2 +
 4 files changed, 62 insertions(+), 36 deletions(-)

New commits:
commit 956f8bf5e8689b72a4e40eed1a6b716ab270323a
Author:     Michael Meeks <michael.meeks at collabora.com>
AuthorDate: Fri May 3 17:05:52 2019 +0100
Commit:     Michael Meeks <michael.meeks at collabora.com>
CommitDate: Fri May 10 14:43:27 2019 +0100

    Unipoll: move event processing into the same thread.
    
    Change-Id: I15aff3b5f18201eca915da94dbaa05148026e244

diff --git a/common/MessageQueue.hpp b/common/MessageQueue.hpp
index 74acc7ea8..ac8d4e51f 100644
--- a/common/MessageQueue.hpp
+++ b/common/MessageQueue.hpp
@@ -79,6 +79,15 @@ public:
         return get_impl();
     }
 
+    /// Get a message without waiting
+    Payload pop()
+    {
+        std::unique_lock<std::mutex> lock(_mutex);
+        if (!_queue.size())
+            return Payload();
+        return get_impl();
+    }
+
     /// Thread safe removal of all the pending messages.
     void clear()
     {
diff --git a/kit/Kit.cpp b/kit/Kit.cpp
index c6ab8fd4a..6ac59e84d 100644
--- a/kit/Kit.cpp
+++ b/kit/Kit.cpp
@@ -858,7 +858,7 @@ public:
 /// per process. But for security reasons don't.
 /// However, we could have a loolkit instance
 /// per user or group of users (a trusted circle).
-class Document : public Runnable, public DocumentManagerInterface
+class Document : public DocumentManagerInterface
 {
 public:
     /// We have two types of password protected documents
@@ -898,7 +898,10 @@ public:
                 "] and id [" << _docId << "].");
         assert(_loKit);
 
-        _callbackThread.start(*this);
+#if !MOBILEAPP
+        _lastMemStatsTime = std::chrono::steady_clock::now();
+        sendTextFrame(Util::getMemoryStats(ProcSMapsFile));
+#endif
     }
 
     ~Document()
@@ -912,12 +915,11 @@ public:
         _stop = true;
 
         _tileQueue->put("eof");
-        _callbackThread.join();
     }
 
     const std::string& getUrl() const { return _url; }
 
-    /// Post the message in the correct thread.
+    /// Post the message - in the unipoll world we're in the right thread anyway
     bool postMessage(const std::shared_ptr<std::vector<char>>& message, const WSOpCode code) const
     {
         LOG_TRC("postMessage called with: " << getAbbreviatedMessage(message->data(), message->size()));
@@ -927,9 +929,7 @@ public:
             return false;
         }
 
-        _socketPoll.addCallback([=]{
-                _websocketHandler->sendMessage(message->data(), message->size(), code);
-            });
+        _websocketHandler->sendMessage(message->data(), message->size(), code);
         return true;
     }
 
@@ -1982,35 +1982,16 @@ private:
         return std::string();
     }
 
-    void run() override
+public:
+    void drainQueue(const std::chrono::steady_clock::time_point &now)
     {
-        Util::setThreadName("lokit_" + _docId);
-
-        LOG_DBG("Thread started.");
-#if !MOBILEAPP
-        // Update memory stats and editor every 5 seconds.
-        const int memStatsPeriodMs = 5000;
-        auto lastMemStatsTime = std::chrono::steady_clock::now();
-        sendTextFrame(Util::getMemoryStats(ProcSMapsFile));
-#endif
         try
         {
-            while (!_stop && !TerminationFlag)
+            while (true)
             {
-                const TileQueue::Payload input = _tileQueue->get(POLL_TIMEOUT_MS * 2);
-                if (input.empty())
-                {
-#if !MOBILEAPP
-                    auto duration = (std::chrono::steady_clock::now() - lastMemStatsTime);
-                    std::chrono::milliseconds::rep durationMs = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count();
-                    if (durationMs > memStatsPeriodMs)
-                    {
-                        sendTextFrame(Util::getMemoryStats(ProcSMapsFile));
-                        lastMemStatsTime = std::chrono::steady_clock::now();
-                    }
-#endif
-                    continue;
-                }
+                const TileQueue::Payload input = _tileQueue->pop();
+                if (input.size() <= 0)
+                    break;
 
                 LOG_TRC("Kit Recv " << LOOLProtocol::getAbbreviatedMessage(input));
 
@@ -2113,6 +2094,17 @@ private:
                     LOG_ERR("Unexpected request: [" << LOOLProtocol::getAbbreviatedMessage(input) << "].");
                 }
             }
+
+#if !MOBILEAPP
+            std::chrono::milliseconds::rep durationMs =
+                std::chrono::duration_cast<std::chrono::milliseconds>(now - _lastMemStatsTime).count();
+            // Update memory stats and editor every 5 seconds.
+            if (durationMs > 5000)
+            {
+                sendTextFrame(Util::getMemoryStats(ProcSMapsFile));
+                _lastMemStatsTime = std::chrono::steady_clock::now();
+            }
+#endif
         }
         catch (const std::exception& exc)
         {
@@ -2122,10 +2114,9 @@ private:
         {
             LOG_FTL("QueueHandler::run: Unknown exception");
         }
-
-        LOG_DBG("Thread finished.");
     }
 
+private:
     /// Return access to the lok::Office instance.
     std::shared_ptr<lok::Office> getLOKit() override
     {
@@ -2207,6 +2198,7 @@ private:
     std::map<int, int> _speedCount;
     /// For showing disconnected user info in the doc repair dialog.
     std::map<int, UserInfo> _sessionUserInfo;
+    std::chrono::steady_clock::time_point _lastMemStatsTime;
     Poco::Thread _callbackThread;
 };
 
@@ -2277,7 +2269,6 @@ protected:
 
             if (!document)
             {
-                // Creating the Document object starts a thread running Document::run().
                 document = std::make_shared<Document>(_loKit, _jailId, docKey, docId, url, _queue, _socketPoll, shared_from_this());
             }
 
@@ -2336,7 +2327,7 @@ void documentViewCallback(const int type, const char* payload, void* data)
     Document::ViewCallback(type, payload, data);
 }
 
-/// Called by LOK main-loop
+/// Called by LOK main-loop the central location for data processing.
 int pollCallback(void* pData, int timeoutUs)
 {
     if (!pData)
@@ -2379,6 +2370,9 @@ int pollCallback(void* pData, int timeoutUs)
         while (maxExtraEvents-- > 0);
     }
 
+    if (document)
+        document->drainQueue(std::chrono::steady_clock::now());
+
 #if !MOBILEAPP
     if (document && document->purgeSessions() == 0)
     {
diff --git a/kit/TestStubs.cpp b/kit/TestStubs.cpp
new file mode 100644
index 000000000..0cb32a091
--- /dev/null
+++ b/kit/TestStubs.cpp
@@ -0,0 +1,21 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+/*
+ * Stub missing symbols required for unit tests ...
+ */
+
+#include <config.h>
+
+#include "common/Common.hpp"
+#include "ChildSession.hpp"
+
+void ChildSession::loKitCallback(const int /* type */, const std::string& /* payload */) {}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/test/Makefile.am b/test/Makefile.am
index 2683bce16..bd107629e 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -43,10 +43,12 @@ wsd_sources = \
             ../common/Log.cpp \
             ../common/Protocol.cpp \
             ../common/Session.cpp \
+            ../common/SpookyV2.cpp \
             ../common/Util.cpp \
             ../common/MessageQueue.cpp \
             ../common/Authorization.cpp \
             ../kit/Kit.cpp \
+            ../kit/TestStubs.cpp \
             ../wsd/Auth.cpp \
             ../wsd/TileCache.cpp \
             ../wsd/TestStubs.cpp \


More information about the Libreoffice-commits mailing list