OSDN Git Service

Some improvements to connectivity check: Start with small timeout and increase the...
[mutilities/MUtilities.git] / src / Global.cpp
index 2cfce45..dc48e84 100644 (file)
@@ -1,6 +1,6 @@
 ///////////////////////////////////////////////////////////////////////////////
 // MuldeR's Utilities for Qt
-// Copyright (C) 2004-2016 LoRd_MuldeR <MuldeR2@GMX.de>
+// Copyright (C) 2004-2017 LoRd_MuldeR <MuldeR2@GMX.de>
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
@@ -26,6 +26,7 @@
 //MUtils
 #include <MUtils/Global.h>
 #include <MUtils/OSSupport.h>
+#include <MUtils/Version.h>
 
 //Internal
 #include "DirLocker.h"
@@ -39,6 +40,7 @@
 #include <QPair>
 #include <QListIterator>
 #include <QMutex>
+#include <QThreadStorage>
 
 //CRT
 #include <cstdlib>
 #define rand_s(X) (true)
 #endif
 
+//Per-thread init flag
+static QThreadStorage<bool> g_srand_flag;
+
 //Robert Jenkins' 96 bit Mix Function
-static unsigned int mix_function(const unsigned int x, const unsigned int y, const unsigned int z)
+static quint32 mix_function(const quint32 x, const quint32 y, const quint32 z)
 {
-       unsigned int a = x;
-       unsigned int b = y;
-       unsigned int c = z;
+       quint32 a = x;
+       quint32 b = y;
+       quint32 c = z;
        
        a=a-b;  a=a-c;  a=a^(c >> 13);
-       b=b-c;  b=b-a;  b=b^(a << ); 
+       b=b-c;  b=b-a;  b=b^(a <<  8); 
        c=c-a;  c=c-b;  c=c^(b >> 13);
        a=a-b;  a=a-c;  a=a^(c >> 12);
        b=b-c;  b=b-a;  b=b^(a << 16);
-       c=c-a;  c=c-b;  c=c^(b >> );
-       a=a-b;  a=a-c;  a=a^(c >> );
+       c=c-a;  c=c-b;  c=c^(b >>  5);
+       a=a-b;  a=a-c;  a=a^(c >>  3);
        b=b-c;  b=b-a;  b=b^(a << 10);
        c=c-a;  c=c-b;  c=c^(b >> 15);
 
        return a ^ b ^ c;
 }
 
-void MUtils::seed_rand(void)
+static void seed_rand(void)
 {
-       qsrand(mix_function(clock(), time(NULL), _getpid()));
+       QDateTime build(MUtils::Version::lib_build_date(), MUtils::Version::lib_build_time());
+       const quint32 seed = mix_function(MUtils::OS::process_id(), MUtils::OS::thread_id(), build.toMSecsSinceEpoch());
+       qsrand(mix_function(clock(), time(NULL), seed));
+}
+
+static quint32 rand_fallback(void)
+{
+       Q_ASSERT(RAND_MAX >= 0xFFF);
+       if (!(g_srand_flag.hasLocalData() && g_srand_flag.localData()))
+       {
+               seed_rand();
+               g_srand_flag.setLocalData(true);
+       }
+       quint32 rnd = 0x32288EA3;
+       for (size_t i = 0; i < 3; i++)
+       {
+               rnd = (rnd << 12) ^ qrand();
+       }
+       return rnd;
 }
 
 quint32 MUtils::next_rand_u32(void)
@@ -88,10 +111,7 @@ quint32 MUtils::next_rand_u32(void)
        quint32 rnd;
        if (rand_s(&rnd))
        {
-               for (size_t i = 0; i < sizeof(quint32); i += 2)
-               {
-                       rnd = (rnd << 16) ^ qrand();
-               }
+               return rand_fallback();
        }
        return rnd;
 }
@@ -386,6 +406,7 @@ bool MUtils::remove_file(const QString &fileName)
                {
                        return true;
                }
+               MUtils::OS::sleep_ms(1);
                fileInfo.refresh();
        }
 
@@ -445,6 +466,7 @@ bool MUtils::remove_directory(const QString &folderPath, const bool &recursive)
                {
                        return true;
                }
+               MUtils::OS::sleep_ms(1);
                folder.refresh();
        }