summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaszlo Papp <lpapp@kde.org>2014-02-16 01:01:03 +0000
committerThe Qt Project <gerrit-noreply@qt-project.org>2014-02-16 11:59:41 +0100
commitc0e0bd80c3876ce1f9b7db80b5873e78b94fe039 (patch)
tree3bd80fcb62a52aa247e0711b0128838d4010b9d5
parent7c8dd1d696c61c4c49ffcb2c1c1b455389edb771 (diff)
downloadqtserialport-c0e0bd80c3876ce1f9b7db80b5873e78b94fe039.tar.gz
Use Q_GLOBAL_STATIC for the udev symbol loading QLibrary instance
Even though this has been introduced in 5.1, officially, we can rely on it in the previous versions even if private API that it was. * Global variables are better avoided. * Q_GLOBAL_STATIC is thread-safe in Qt 5. * QLibrary depends on QCoreApplication and we need to make sure that is initialized afterwards, respectively. The instantiation is now moved into the corresponding source file so that if it is included at more than one place, it will not be instantiated each time it is used in a new source file including this header. Task-number: QTBUG-36870 Change-Id: I96de1257e5836b69d0c48b717d7c2e708d6b0fee Reviewed-by: Denis Shienkov <denis.shienkov@gmail.com> Reviewed-by: Sergey Belyashov <Sergey.Belyashov@gmail.com>
-rw-r--r--src/serialport/qserialportinfo_unix.cpp6
-rw-r--r--src/serialport/qtudev_p.h26
2 files changed, 17 insertions, 15 deletions
diff --git a/src/serialport/qserialportinfo_unix.cpp b/src/serialport/qserialportinfo_unix.cpp
index 80b2f7d..a8bb6d3 100644
--- a/src/serialport/qserialportinfo_unix.cpp
+++ b/src/serialport/qserialportinfo_unix.cpp
@@ -195,10 +195,14 @@ QList<QSerialPortInfo> availablePortsBySysfs()
return serialPortInfoList;
}
+#ifndef LINK_LIBUDEV
+ Q_GLOBAL_STATIC(QLibrary, udevLibrary)
+#endif
+
QList<QSerialPortInfo> availablePortsByUdev()
{
#ifndef LINK_LIBUDEV
- static bool symbolsResolved = resolveSymbols();
+ static bool symbolsResolved = resolveSymbols(udevLibrary());
if (!symbolsResolved)
return QList<QSerialPortInfo>();
#endif
diff --git a/src/serialport/qtudev_p.h b/src/serialport/qtudev_p.h
index 1187165..434292d 100644
--- a/src/serialport/qtudev_p.h
+++ b/src/serialport/qtudev_p.h
@@ -57,7 +57,7 @@ extern "C"
fp_##symbolName symbolName;
#define RESOLVE_SYMBOL(symbolName) \
- symbolName = (fp_##symbolName)resolveSymbol(#symbolName); \
+ symbolName = (fp_##symbolName)resolveSymbol(udevLibrary, #symbolName); \
if (!symbolName) \
return false;
@@ -89,16 +89,14 @@ GENERATE_SYMBOL_VARIABLE(void, udev_device_unref, struct udev_device *)
GENERATE_SYMBOL_VARIABLE(void, udev_enumerate_unref, struct udev_enumerate *)
GENERATE_SYMBOL_VARIABLE(void, udev_unref, struct udev *)
-QLibrary udevLibrary;
-
#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
-inline QFunctionPointer resolveSymbol(const char *symbolName)
+inline QFunctionPointer resolveSymbol(QLibrary *udevLibrary, const char *symbolName)
{
- QFunctionPointer symbolFunctionPointer = udevLibrary.resolve(symbolName);
+ QFunctionPointer symbolFunctionPointer = udevLibrary->resolve(symbolName);
#else
-inline void *resolveSymbol(const char *symbolName)
+inline void *resolveSymbol(QLibrary *udevLibrary, const char *symbolName)
{
- void *symbolFunctionPointer = udevLibrary.resolve(symbolName);
+ void *symbolFunctionPointer = udevLibrary->resolve(symbolName);
#endif
if (!symbolFunctionPointer)
qWarning("Failed to resolve the udev symbol: %s", symbolName);
@@ -106,14 +104,14 @@ inline void *resolveSymbol(const char *symbolName)
return symbolFunctionPointer;
}
-inline bool resolveSymbols()
+inline bool resolveSymbols(QLibrary *udevLibrary)
{
- if (!udevLibrary.isLoaded()) {
- udevLibrary.setFileNameAndVersion(QStringLiteral("udev"), 1);
- if (!udevLibrary.load()) {
- udevLibrary.setFileNameAndVersion(QStringLiteral("udev"), 0);
- if (!udevLibrary.load()) {
- qWarning("Failed to load the library: %s, supported version(s): %i and %i", qPrintable(udevLibrary.fileName()), 1, 0);
+ if (!udevLibrary->isLoaded()) {
+ udevLibrary->setFileNameAndVersion(QStringLiteral("udev"), 1);
+ if (!udevLibrary->load()) {
+ udevLibrary->setFileNameAndVersion(QStringLiteral("udev"), 0);
+ if (!udevLibrary->load()) {
+ qWarning("Failed to load the library: %s, supported version(s): %i and %i", qPrintable(udevLibrary->fileName()), 1, 0);
return false;
}
}