QProcessEnvironment: simplify locking
The mutex is only protecting 'nameMap'. Proof: it's only defined on platforms on which there is a 'nameMap'. Also, nothing else is mutable, so no lazy init going on here. So, drop all the mutex protection, except where we access 'nameMap', and draw the mutex as close as possible to the nameMap uses, iow: copy ctor, prepareName() and nameToString(). As a consequence, the old (Ordered)MutexLocker class only needs to be defined on Unix. Change-Id: Ic969313bc48ad7ebf24c5dca7fd48359956b048d Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
parent
2e5b8032a2
commit
c5d6b263c2
@ -202,6 +202,7 @@ void QProcessEnvironmentPrivate::insert(const QProcessEnvironmentPrivate &other)
|
|||||||
vars.insert(it.key(), it.value());
|
vars.insert(it.key(), it.value());
|
||||||
|
|
||||||
#ifdef Q_OS_UNIX
|
#ifdef Q_OS_UNIX
|
||||||
|
const OrderedNameMapMutexLocker locker(this, &other);
|
||||||
auto nit = other.nameMap.constBegin();
|
auto nit = other.nameMap.constBegin();
|
||||||
const auto nend = other.nameMap.constEnd();
|
const auto nend = other.nameMap.constEnd();
|
||||||
for ( ; nit != nend; ++nit)
|
for ( ; nit != nend; ++nit)
|
||||||
@ -275,7 +276,6 @@ bool QProcessEnvironment::operator==(const QProcessEnvironment &other) const
|
|||||||
return true;
|
return true;
|
||||||
if (d) {
|
if (d) {
|
||||||
if (other.d) {
|
if (other.d) {
|
||||||
QProcessEnvironmentPrivate::OrderedMutexLocker locker(d, other.d);
|
|
||||||
return d->vars == other.d->vars;
|
return d->vars == other.d->vars;
|
||||||
} else {
|
} else {
|
||||||
return isEmpty();
|
return isEmpty();
|
||||||
@ -322,7 +322,6 @@ bool QProcessEnvironment::contains(const QString &name) const
|
|||||||
{
|
{
|
||||||
if (!d)
|
if (!d)
|
||||||
return false;
|
return false;
|
||||||
QProcessEnvironmentPrivate::MutexLocker locker(d);
|
|
||||||
return d->vars.contains(d->prepareName(name));
|
return d->vars.contains(d->prepareName(name));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -373,7 +372,6 @@ QString QProcessEnvironment::value(const QString &name, const QString &defaultVa
|
|||||||
if (!d)
|
if (!d)
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
|
|
||||||
QProcessEnvironmentPrivate::MutexLocker locker(d);
|
|
||||||
const auto it = d->vars.constFind(d->prepareName(name));
|
const auto it = d->vars.constFind(d->prepareName(name));
|
||||||
if (it == d->vars.constEnd())
|
if (it == d->vars.constEnd())
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
@ -398,7 +396,6 @@ QStringList QProcessEnvironment::toStringList() const
|
|||||||
{
|
{
|
||||||
if (!d)
|
if (!d)
|
||||||
return QStringList();
|
return QStringList();
|
||||||
QProcessEnvironmentPrivate::MutexLocker locker(d);
|
|
||||||
return d->toList();
|
return d->toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -412,7 +409,6 @@ QStringList QProcessEnvironment::keys() const
|
|||||||
{
|
{
|
||||||
if (!d)
|
if (!d)
|
||||||
return QStringList();
|
return QStringList();
|
||||||
QProcessEnvironmentPrivate::MutexLocker locker(d);
|
|
||||||
return d->keys();
|
return d->keys();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -429,7 +425,6 @@ void QProcessEnvironment::insert(const QProcessEnvironment &e)
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
// our re-impl of detach() detaches from null
|
// our re-impl of detach() detaches from null
|
||||||
QProcessEnvironmentPrivate::MutexLocker locker(e.d);
|
|
||||||
d->insert(*e.d);
|
d->insert(*e.d);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -146,16 +146,22 @@ public:
|
|||||||
inline QString nameToString(const Key &name) const { return name; }
|
inline QString nameToString(const Key &name) const { return name; }
|
||||||
inline Value prepareValue(const QString &value) const { return value; }
|
inline Value prepareValue(const QString &value) const { return value; }
|
||||||
inline QString valueToString(const Value &value) const { return value; }
|
inline QString valueToString(const Value &value) const { return value; }
|
||||||
struct MutexLocker {
|
|
||||||
MutexLocker(const QProcessEnvironmentPrivate *) {}
|
|
||||||
};
|
|
||||||
struct OrderedMutexLocker {
|
|
||||||
OrderedMutexLocker(const QProcessEnvironmentPrivate *,
|
|
||||||
const QProcessEnvironmentPrivate *) {}
|
|
||||||
};
|
|
||||||
#else
|
#else
|
||||||
|
struct NameMapMutexLocker : public QMutexLocker
|
||||||
|
{
|
||||||
|
NameMapMutexLocker(const QProcessEnvironmentPrivate *d) : QMutexLocker(&d->nameMapMutex) {}
|
||||||
|
};
|
||||||
|
struct OrderedNameMapMutexLocker : public QOrderedMutexLocker
|
||||||
|
{
|
||||||
|
OrderedNameMapMutexLocker(const QProcessEnvironmentPrivate *d1,
|
||||||
|
const QProcessEnvironmentPrivate *d2)
|
||||||
|
: QOrderedMutexLocker(&d1->nameMapMutex, &d2->nameMapMutex)
|
||||||
|
{}
|
||||||
|
};
|
||||||
|
|
||||||
inline Key prepareName(const QString &name) const
|
inline Key prepareName(const QString &name) const
|
||||||
{
|
{
|
||||||
|
const NameMapMutexLocker locker(this);
|
||||||
Key &ent = nameMap[name];
|
Key &ent = nameMap[name];
|
||||||
if (ent.isEmpty())
|
if (ent.isEmpty())
|
||||||
ent = name.toLocal8Bit();
|
ent = name.toLocal8Bit();
|
||||||
@ -164,40 +170,27 @@ public:
|
|||||||
inline QString nameToString(const Key &name) const
|
inline QString nameToString(const Key &name) const
|
||||||
{
|
{
|
||||||
const QString sname = QString::fromLocal8Bit(name);
|
const QString sname = QString::fromLocal8Bit(name);
|
||||||
nameMap[sname] = name;
|
{
|
||||||
|
const NameMapMutexLocker locker(this);
|
||||||
|
nameMap[sname] = name;
|
||||||
|
}
|
||||||
return sname;
|
return sname;
|
||||||
}
|
}
|
||||||
inline Value prepareValue(const QString &value) const { return Value(value); }
|
inline Value prepareValue(const QString &value) const { return Value(value); }
|
||||||
inline QString valueToString(const Value &value) const { return value.string(); }
|
inline QString valueToString(const Value &value) const { return value.string(); }
|
||||||
|
|
||||||
struct MutexLocker : public QMutexLocker
|
|
||||||
{
|
|
||||||
MutexLocker(const QProcessEnvironmentPrivate *d) : QMutexLocker(&d->mutex) {}
|
|
||||||
};
|
|
||||||
struct OrderedMutexLocker : public QOrderedMutexLocker
|
|
||||||
{
|
|
||||||
OrderedMutexLocker(const QProcessEnvironmentPrivate *d1,
|
|
||||||
const QProcessEnvironmentPrivate *d2) :
|
|
||||||
QOrderedMutexLocker(&d1->mutex, &d2->mutex)
|
|
||||||
{}
|
|
||||||
};
|
|
||||||
|
|
||||||
QProcessEnvironmentPrivate() : QSharedData() {}
|
QProcessEnvironmentPrivate() : QSharedData() {}
|
||||||
QProcessEnvironmentPrivate(const QProcessEnvironmentPrivate &other) :
|
QProcessEnvironmentPrivate(const QProcessEnvironmentPrivate &other) :
|
||||||
QSharedData()
|
QSharedData(), vars(other.vars)
|
||||||
{
|
{
|
||||||
// This being locked ensures that the functions that only assign
|
|
||||||
// d pointers don't need explicit locking.
|
|
||||||
// We don't need to lock our own mutex, as this object is new and
|
// We don't need to lock our own mutex, as this object is new and
|
||||||
// consequently not shared. For the same reason, non-const methods
|
// consequently not shared. For the same reason, non-const methods
|
||||||
// do not need a lock, as they detach objects (however, we need to
|
// do not need a lock, as they detach objects (however, we need to
|
||||||
// ensure that they really detach before using prepareName()).
|
// ensure that they really detach before using prepareName()).
|
||||||
MutexLocker locker(&other);
|
NameMapMutexLocker locker(&other);
|
||||||
vars = other.vars;
|
|
||||||
nameMap = other.nameMap;
|
nameMap = other.nameMap;
|
||||||
// We need to detach our members, so that our mutex can protect them.
|
// We need to detach our nameMap, so that our mutex can protect it.
|
||||||
// As we are being detached, they likely would be detached a moment later anyway.
|
// As we are being detached, it likely would be detached a moment later anyway.
|
||||||
vars.detach();
|
|
||||||
nameMap.detach();
|
nameMap.detach();
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@ -208,8 +201,7 @@ public:
|
|||||||
#ifdef Q_OS_UNIX
|
#ifdef Q_OS_UNIX
|
||||||
typedef QHash<QString, Key> NameHash;
|
typedef QHash<QString, Key> NameHash;
|
||||||
mutable NameHash nameMap;
|
mutable NameHash nameMap;
|
||||||
|
mutable QMutex nameMapMutex;
|
||||||
mutable QMutex mutex;
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static QProcessEnvironment fromList(const QStringList &list);
|
static QProcessEnvironment fromList(const QStringList &list);
|
||||||
|
@ -438,7 +438,6 @@ void QProcessPrivate::startProcess()
|
|||||||
int envc = 0;
|
int envc = 0;
|
||||||
char **envp = 0;
|
char **envp = 0;
|
||||||
if (environment.d.constData()) {
|
if (environment.d.constData()) {
|
||||||
QProcessEnvironmentPrivate::MutexLocker locker(environment.d);
|
|
||||||
envp = _q_dupEnvironment(environment.d.constData()->vars, &envc);
|
envp = _q_dupEnvironment(environment.d.constData()->vars, &envc);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -970,7 +969,6 @@ bool QProcessPrivate::startDetached(qint64 *pid)
|
|||||||
int envc = 0;
|
int envc = 0;
|
||||||
char **envp = nullptr;
|
char **envp = nullptr;
|
||||||
if (environment.d.constData()) {
|
if (environment.d.constData()) {
|
||||||
QProcessEnvironmentPrivate::MutexLocker locker(environment.d);
|
|
||||||
envp = _q_dupEnvironment(environment.d.constData()->vars, &envc);
|
envp = _q_dupEnvironment(environment.d.constData()->vars, &envc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user