QHostInfo: replace a volatile bool with an atomic int
authorMarc Mutz <marc.mutz@kdab.com>
Fri, 24 Aug 2012 07:47:55 +0000 (09:47 +0200)
committerQt by Nokia <qt-info@nokia.com>
Wed, 29 Aug 2012 12:14:17 +0000 (14:14 +0200)
A volatile bool read/store is documented on MSVC to have acquire/release
semantics, respectively, but that doesn't need to be true for MinGW, so
use explicit memory ordering.

Apply the same fix to the Unix implementation, too.

Change-Id: Ica466cec50beed830aafa4e3384d82f02e1a47e8
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Shane Kearns <shane.kearns@accenture.com>
src/network/kernel/qhostinfo_unix.cpp
src/network/kernel/qhostinfo_win.cpp

index b696265..e3f6885 100644 (file)
@@ -48,6 +48,7 @@
 #include "qiodevice.h"
 #include <qbytearray.h>
 #include <qlibrary.h>
+#include <qbasicatomic.h>
 #include <qurl.h>
 #include <qfile.h>
 #include <private/qmutexpool_p.h>
@@ -127,12 +128,12 @@ QHostInfo QHostInfoAgent::fromName(const QString &hostName)
 #endif
 
     // Load res_init on demand.
-    static volatile bool triedResolve = false;
-    if (!triedResolve) {
+    static QBasicAtomicInt triedResolve = Q_BASIC_ATOMIC_INITIALIZER(false);
+    if (!triedResolve.loadAcquire()) {
         QMutexLocker locker(QMutexPool::globalInstanceGet(&local_res_init));
-        if (!triedResolve) {
+        if (!triedResolve.load()) {
             resolveLibrary();
-            triedResolve = true;
+            triedResolve.storeRelease(true);
         }
     }
 
index 8ace68d..5c91d75 100644 (file)
@@ -46,6 +46,7 @@
 #include <ws2tcpip.h>
 #include <private/qsystemlibrary_p.h>
 #include <qmutex.h>
+#include <qbasicatomic.h>
 #include <qurl.h>
 #include <private/qmutexpool_p.h>
 
@@ -121,12 +122,12 @@ QHostInfo QHostInfoAgent::fromName(const QString &hostName)
     QWindowsSockInit winSock;
 
     // Load res_init on demand.
-    static volatile bool triedResolve = false;
-    if (!triedResolve) {
+    static QBasicAtomicInt triedResolve = Q_BASIC_ATOMIC_INITIALIZER(false);
+    if (!triedResolve.loadAcquire()) {
         QMutexLocker locker(QMutexPool::globalInstanceGet(&local_getaddrinfo));
-        if (!triedResolve) {
+        if (!triedResolve.load()) {
             resolveLibrary();
-            triedResolve = true;
+            triedResolve.storeRelease(true);
         }
     }