CoreCon Server: guard with a QMutex
authorAndrew Knight <andrew.knight@digia.com>
Mon, 17 Feb 2014 08:10:24 +0000 (10:10 +0200)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Mon, 17 Feb 2014 13:27:53 +0000 (14:27 +0100)
The server may be used from several threads at once during startup, so
use a mutex to protect initialization and enumeration of devices.

Change-Id: I6a6605ac80af3da7f330474146e2256418892e50
Reviewed-by: Oliver Wolff <oliver.wolff@digia.com>
src/shared/corecon/corecon.cpp

index c75f080..558e8b1 100644 (file)
@@ -42,6 +42,8 @@
 #include "corecon.h"
 
 #include <QtCore/QString>
+#include <QtCore/QMutex>
+#include <QtCore/QMutexLocker>
 
 #include <comdef.h>
 #include <wrl.h>
@@ -133,10 +135,12 @@ public:
     ComPtr<ICcServer> handle;
     QList<CoreConDevice *> devices;
     HMODULE langModule;
+    QMutex mutex;
 };
 
 CoreConServer::CoreConServer() : d(new CoreConServerPrivate)
 {
+    QMutexLocker(&d->mutex);
     HRESULT hr = CoCreateInstance(CLSID_ConMan, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&d->handle));
     if (FAILED(hr))
         qCWarning(lcCoreCon) << "Failed to initialize connection server." << formatError(hr);
@@ -158,6 +162,11 @@ QList<CoreConDevice *> CoreConServer::devices() const
 {
     qCDebug(lcCoreCon) << __FUNCTION__;
 
+    while (!d)
+        Sleep(1);
+
+    QMutexLocker(&d->mutex);
+
     if (!d->devices.isEmpty() || !d->handle)
         return d->devices;