diff options
author | Marc Mutz <marc.mutz@kdab.com> | 2012-08-24 12:06:03 +0200 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2012-09-22 19:19:37 +0200 |
commit | 373a7277db0583fbbb8ed61f9f61d6f77ed95da4 (patch) | |
tree | f530a3e205770802a2580789e8e5eb487065fedb /src/dbus/qdbusmetatype.cpp | |
parent | f799e57151dbd236f30d4ffb20d6c331b6e1a388 (diff) |
QDBusMetaTypeId: don't cache the result of qMetaTypeId<>() in static ints
There's not much point in caching the result of qMetaTypeId<>,
because it's already internally memoised.
In addition, the code that initialised the static int caches wasn't
protected against concurrent access under the assumption that the
operations performed were thread-safe.
That is true for most of them, but not for the stores to the static ints,
which race against each other:
// Thread A // Thread B
r1 = initialized /*=false*/
r1 = initialized /*=false*/
r2 = qMetaTypeId<...>();
r2 = qMetaTypeId<...>();
message = r2; message = r2; // race, ditto for all other ints
To fix, turn the ints into inline functions that just call the respective
qMetaTypeId<>() function.
Change-Id: I5aa80c624872c3867232abc26ffdcde70cd54022
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/dbus/qdbusmetatype.cpp')
-rw-r--r-- | src/dbus/qdbusmetatype.cpp | 45 |
1 files changed, 15 insertions, 30 deletions
diff --git a/src/dbus/qdbusmetatype.cpp b/src/dbus/qdbusmetatype.cpp index dade9788b3..d8ee0b31e7 100644 --- a/src/dbus/qdbusmetatype.cpp +++ b/src/dbus/qdbusmetatype.cpp @@ -89,14 +89,6 @@ inline static void registerHelper(T * = 0) reinterpret_cast<QDBusMetaType::DemarshallFunction>(df)); } -int QDBusMetaTypeId::message; -int QDBusMetaTypeId::argument; -int QDBusMetaTypeId::variant; -int QDBusMetaTypeId::objectpath; -int QDBusMetaTypeId::signature; -int QDBusMetaTypeId::error; -int QDBusMetaTypeId::unixfd; - void QDBusMetaTypeId::init() { static volatile bool initialized = false; @@ -104,16 +96,14 @@ void QDBusMetaTypeId::init() // reentrancy is not a problem since everything else is locked on their own // set the guard variable at the end if (!initialized) { -#ifndef QT_BOOTSTRAPPED // register our types with QtCore (calling qMetaTypeId<T>() does this implicitly) - message = qMetaTypeId<QDBusMessage>(); - error = qMetaTypeId<QDBusError>(); -#endif - argument = qMetaTypeId<QDBusArgument>(); - variant = qMetaTypeId<QDBusVariant>(); - objectpath = qMetaTypeId<QDBusObjectPath>(); - signature = qMetaTypeId<QDBusSignature>(); - unixfd = qMetaTypeId<QDBusUnixFileDescriptor>(); + (void)message(); + (void)argument(); + (void)variant(); + (void)objectpath(); + (void)signature(); + (void)error(); + (void)unixfd(); #ifndef QDBUS_NO_SPECIALTYPES // and register QtCore's with us @@ -145,11 +135,6 @@ void QDBusMetaTypeId::init() qDBusRegisterMetaType<QList<QDBusUnixFileDescriptor> >(); #endif -#ifdef QT_BOOTSTRAPPED - const int lastId = qDBusRegisterMetaType<QList<QDBusUnixFileDescriptor> >(); - message = lastId + 1; - error = lastId + 2; -#endif initialized = true; } } @@ -353,16 +338,16 @@ int QDBusMetaType::signatureToType(const char *signature) return QVariant::String; case DBUS_TYPE_OBJECT_PATH: - return QDBusMetaTypeId::objectpath; + return QDBusMetaTypeId::objectpath(); case DBUS_TYPE_SIGNATURE: - return QDBusMetaTypeId::signature; + return QDBusMetaTypeId::signature(); case DBUS_TYPE_UNIX_FD: - return QDBusMetaTypeId::unixfd; + return QDBusMetaTypeId::unixfd(); case DBUS_TYPE_VARIANT: - return QDBusMetaTypeId::variant; + return QDBusMetaTypeId::variant(); case DBUS_TYPE_ARRAY: // special case switch (signature[1]) { @@ -444,13 +429,13 @@ const char *QDBusMetaType::typeToSignature(int type) } QDBusMetaTypeId::init(); - if (type == QDBusMetaTypeId::variant) + if (type == QDBusMetaTypeId::variant()) return DBUS_TYPE_VARIANT_AS_STRING; - else if (type == QDBusMetaTypeId::objectpath) + else if (type == QDBusMetaTypeId::objectpath()) return DBUS_TYPE_OBJECT_PATH_AS_STRING; - else if (type == QDBusMetaTypeId::signature) + else if (type == QDBusMetaTypeId::signature()) return DBUS_TYPE_SIGNATURE_AS_STRING; - else if (type == QDBusMetaTypeId::unixfd) + else if (type == QDBusMetaTypeId::unixfd()) return DBUS_TYPE_UNIX_FD_AS_STRING; // try the database |