QReadLocker / QWriteLocker instead manual lock() unlock() for safety (e.g. exception before unlock())

master ahead manual fix
This commit is contained in:
Roman
2015-04-14 23:03:04 +03:00
parent e7c6afcd49
commit 906c48b9b6
4 changed files with 18 additions and 16 deletions
+8 -7
View File
@@ -9,6 +9,9 @@
#include <limits>
#include <QReadLocker>
#include <QWriteLocker>
QHash<QMetaType::Type, MsgPackPrivate::packer_t> MsgPackPrivate::user_packers;
bool MsgPackPrivate::compatibilityMode = false;
QReadWriteLock MsgPackPrivate::packers_lock;
@@ -41,9 +44,9 @@ quint8 *MsgPackPrivate::pack(const QVariant &v, quint8 *p, bool wr)
else {
if (t == QMetaType::User)
t = (QMetaType::Type)v.userType();
packers_lock.lockForRead();
QReadLocker locker(&packers_lock);
bool has_packer = user_packers.contains(t);
packers_lock.unlock();
locker.unlock();
if (has_packer)
p = pack_user(v, p, wr);
else
@@ -281,17 +284,15 @@ bool MsgPackPrivate::register_packer(QMetaType::Type q_type, qint8 msgpack_type,
qWarning() << "MsgPack::packer for qtype" << q_type << "is invalid";
return false;
}
packers_lock.lockForWrite();
QWriteLocker locker(&packers_lock);
if (user_packers.contains(q_type)) {
qWarning() << "MsgPack::packer for qtype" << q_type << "already exist";
packers_lock.unlock();
return false;
}
packer_t p;
p.packer = packer;
p.type = msgpack_type;
user_packers.insert(q_type, p);
packers_lock.unlock();
return true;
}
@@ -300,9 +301,9 @@ quint8 *MsgPackPrivate::pack_user(const QVariant &v, quint8 *p, bool wr)
QMetaType::Type t = (QMetaType::Type)v.type() == QMetaType::User ?
(QMetaType::Type)v.userType() : (QMetaType::Type)v.type();
QByteArray data;
packers_lock.lockForRead();
QReadLocker locker(&packers_lock);
packer_t pt = user_packers[t];
packers_lock.unlock();
locker.unlock();
quint32 len = pt.packer(v, data, wr);
if (len == 1) {
if (wr) *p = 0xd4;