[Libreoffice-commits] online.git: loolwsd/ChildProcessSession.cpp loolwsd/LOOLWSD.cpp loolwsd/LOOLWSD.hpp loolwsd/MasterProcessSession.cpp loolwsd/MasterProcessSession.hpp loolwsd/Util.cpp loolwsd/Util.hpp

Ashod Nakashian ashod.nakashian at collabora.co.uk
Wed Dec 23 09:18:34 PST 2015


 loolwsd/ChildProcessSession.cpp  |    2 +-
 loolwsd/LOOLWSD.cpp              |    7 +------
 loolwsd/LOOLWSD.hpp              |    2 --
 loolwsd/MasterProcessSession.cpp |    2 --
 loolwsd/MasterProcessSession.hpp |    2 --
 loolwsd/Util.cpp                 |   17 +++++++++++++++++
 loolwsd/Util.hpp                 |    5 +++++
 7 files changed, 24 insertions(+), 13 deletions(-)

New commits:
commit ab5253ba1e8b0b819103fc6d11650ca4132ff75d
Author: Ashod Nakashian <ashod.nakashian at collabora.co.uk>
Date:   Sat Dec 19 09:16:44 2015 -0500

    loolwsd: Refactored the PRNG
    
    Moved the PRNG into utility namespace and interface
    that internally manages seeding (once per process)
    and thread-safety.
    
    Change-Id: I78f0ef2c3a01aa6c6aefc8e4b07f3802f2421c68
    Reviewed-on: https://gerrit.libreoffice.org/20901
    Reviewed-by: Ashod Nakashian <ashnakash at gmail.com>
    Tested-by: Ashod Nakashian <ashnakash at gmail.com>

diff --git a/loolwsd/ChildProcessSession.cpp b/loolwsd/ChildProcessSession.cpp
index f5b0320..95909af 100644
--- a/loolwsd/ChildProcessSession.cpp
+++ b/loolwsd/ChildProcessSession.cpp
@@ -562,7 +562,7 @@ bool ChildProcessSession::downloadAs(const char* /*buffer*/, int /*length*/, Str
         {
             delete file;
         }
-        tmpDir = std::to_string((((Poco::UInt64)LOOLWSD::_rng.next()) << 32) | LOOLWSD::_rng.next() | 1);
+        tmpDir = std::to_string(Util::rng::getNext());
         url = jailDocumentURL + "/" + tmpDir + "/" + name;
         file = new File(url);
     } while (file->exists());
diff --git a/loolwsd/LOOLWSD.cpp b/loolwsd/LOOLWSD.cpp
index 5588be2..229f78d 100644
--- a/loolwsd/LOOLWSD.cpp
+++ b/loolwsd/LOOLWSD.cpp
@@ -587,8 +587,6 @@ std::string LOOLWSD::loTemplate;
 std::string LOOLWSD::childRoot;
 std::string LOOLWSD::loSubPath = "lo";
 std::string LOOLWSD::jail;
-std::mutex LOOLWSD::_rngMutex;
-Random LOOLWSD::_rng;
 Poco::NamedMutex LOOLWSD::_namedMutexLOOL("loolwsd");
 Poco::SharedMemory LOOLWSD::_sharedForkChild("loolwsd", sizeof(bool), Poco::SharedMemory::AM_WRITE);
 
@@ -1090,9 +1088,7 @@ int LOOLWSD::createComponent()
 {
     int pid;
 
-    std::unique_lock<std::mutex> rngLock(_rngMutex);
-    _childId = (((Poco::UInt64)_rng.next()) << 32) | _rng.next() | 1;
-    rngLock.unlock();
+    _childId = Util::rng::getNext();
 
     if ((pid = fork()) == -1)
     {
@@ -1128,7 +1124,6 @@ void LOOLWSD::desktopMain()
     setSignals(false);
 #endif
 
-    _rng.seed(Process::id());
     startupComponent(_numPreSpawnedChildren);
 
     while (MasterProcessSession::_childProcesses.size() > 0)
diff --git a/loolwsd/LOOLWSD.hpp b/loolwsd/LOOLWSD.hpp
index 9771e4c..637c246 100644
--- a/loolwsd/LOOLWSD.hpp
+++ b/loolwsd/LOOLWSD.hpp
@@ -44,7 +44,6 @@ public:
     static std::string jail;
     static Poco::SharedMemory _sharedForkChild;
     static Poco::NamedMutex _namedMutexLOOL;
-    static Poco::Random _rng;
     static Poco::UInt64 _childId;
 
     static const int DEFAULT_CLIENT_PORT_NUMBER = 9980;
@@ -76,7 +75,6 @@ private:
     int  createComponent();
     int  createDesktop();
 
-    static std::mutex _rngMutex;
 
 #if ENABLE_DEBUG
 public:
diff --git a/loolwsd/MasterProcessSession.cpp b/loolwsd/MasterProcessSession.cpp
index b931f41..fb4af0b 100644
--- a/loolwsd/MasterProcessSession.cpp
+++ b/loolwsd/MasterProcessSession.cpp
@@ -49,8 +49,6 @@ std::map<Process::PID, UInt64> MasterProcessSession::_childProcesses;
 std::set<std::shared_ptr<MasterProcessSession>> MasterProcessSession::_availableChildSessions;
 std::mutex MasterProcessSession::_availableChildSessionMutex;
 std::condition_variable MasterProcessSession::_availableChildSessionCV;
-Random MasterProcessSession::_rng;
-std::mutex MasterProcessSession::_rngMutex;
 
 MasterProcessSession::MasterProcessSession(std::shared_ptr<WebSocket> ws, const Kind kind) :
     LOOLSession(ws, kind),
diff --git a/loolwsd/MasterProcessSession.hpp b/loolwsd/MasterProcessSession.hpp
index 8161144..ec5ff0d 100644
--- a/loolwsd/MasterProcessSession.hpp
+++ b/loolwsd/MasterProcessSession.hpp
@@ -77,8 +77,6 @@ private:
     Poco::UInt64 _childId;
     // The pid of the child process
     Poco::Process::PID _pidChild;
-    static Poco::Random _rng;
-    static std::mutex _rngMutex;
     int _curPart;
     int _loadPart;
     /// Kind::ToClient instances store URLs of completed 'save as' documents.
diff --git a/loolwsd/Util.cpp b/loolwsd/Util.cpp
index 9d74278..4ce1cd0 100644
--- a/loolwsd/Util.cpp
+++ b/loolwsd/Util.cpp
@@ -15,6 +15,8 @@
 #include <sstream>
 #include <string>
 #include <cassert>
+#include <random>
+#include <mutex>
 
 #include <png.h>
 
@@ -54,6 +56,21 @@ extern "C"
 
 namespace Util
 {
+namespace rng
+{
+    static std::random_device _rd;
+    static std::mutex _rngMutex;
+    static std::mt19937_64 _rng = std::mt19937_64(_rd());
+    unsigned getNext()
+    {
+        std::unique_lock<std::mutex> lock(_rngMutex);
+        return _rng();
+    }
+}
+}
+
+namespace Util
+{
     static const Poco::Int64 epochStart = Poco::Timestamp().epochMicroseconds();
 
     std::string logPrefix()
diff --git a/loolwsd/Util.hpp b/loolwsd/Util.hpp
index 7a82fa4..8db7df3 100644
--- a/loolwsd/Util.hpp
+++ b/loolwsd/Util.hpp
@@ -19,6 +19,11 @@
 
 namespace Util
 {
+    namespace rng
+    {
+       unsigned getNext();
+    }
+
     std::string logPrefix();
 
     bool windowingAvailable();


More information about the Libreoffice-commits mailing list