* actually delete the matches on removeAllMatches

* use macros for the locking exercises

svn path=/trunk/KDE/kdebase/workspace/libs/plasma/; revision=803058
This commit is contained in:
Aaron J. Seigo 2008-05-01 19:43:22 +00:00
parent f55bbb73f9
commit f3e1fd618f

View File

@ -34,6 +34,14 @@
#include "searchmatch.h" #include "searchmatch.h"
#define LOCK_FOR_READ(context) if (context->d->policy == Shared) { context->d->lock.lockForRead(); }
#define LOCK_FOR_WRITE(context) if (context->d->policy == Shared) { context->d->lock.lockForWrite(); }
#define UNLOCK(context) if (context->d->policy == Shared) { context->d->lock.unlock(); }
/*
#define LOCK_FOR_READ(context) context->d->lock.lockForRead();
#define LOCK_FOR_WRITE(context) context->d->lock.lockForWrite();
#define UNLOCK(context) context->d->lock.unlock();
*/
namespace Plasma namespace Plasma
{ {
@ -60,29 +68,12 @@ class RunnerContext::Private : public QSharedData
} }
~Private() ~Private()
{
lockForWrite();
qDeleteAll(matches);
matches.clear();
unlock();
}
void lockForRead()
{
if (policy == Shared) {
lock.lockForRead();
}
}
void lockForWrite()
{ {
if (policy == Shared) { if (policy == Shared) {
lock.lockForWrite(); lock.lockForWrite();
} }
} qDeleteAll(matches);
matches.clear();
void unlock()
{
if (policy == Shared) { if (policy == Shared) {
lock.unlock(); lock.unlock();
} }
@ -93,7 +84,10 @@ class RunnerContext::Private : public QSharedData
*/ */
void determineType() void determineType()
{ {
lockForWrite(); if (policy == Shared) {
lock.lockForWrite();
}
QString path = KShell::tildeExpand(term); QString path = KShell::tildeExpand(term);
int space = term.indexOf(' '); int space = term.indexOf(' ');
@ -124,7 +118,10 @@ class RunnerContext::Private : public QSharedData
type = NetworkLocation; type = NetworkLocation;
} }
} }
unlock();
if (policy == Shared) {
lock.unlock();
}
} }
QReadWriteLock lock; QReadWriteLock lock;
@ -147,9 +144,9 @@ RunnerContext::RunnerContext(QObject *parent, DataPolicy policy)
RunnerContext::RunnerContext(RunnerContext &other, QObject *parent) RunnerContext::RunnerContext(RunnerContext &other, QObject *parent)
: QObject(parent) : QObject(parent)
{ {
other.d->lockForRead(); LOCK_FOR_READ((&other))
d=other.d; d = other.d;
other.d->unlock(); UNLOCK((&other))
} }
RunnerContext::~RunnerContext() RunnerContext::~RunnerContext()
@ -163,14 +160,14 @@ void RunnerContext::reset()
// We will detach if we are a copy of someone. But we will reset // We will detach if we are a copy of someone. But we will reset
// if we are the 'main' context others copied from. Resetting // if we are the 'main' context others copied from. Resetting
// one RunnerContext makes all the copies oneobsolete. // one RunnerContext makes all the copies oneobsolete.
LOCK_FOR_WRITE(this)
d.detach(); d.detach();
//kDebug() << "reset searchContext"; //kDebug() << "reset searchContext";
d->lockForWrite();
d->type = RunnerContext::UnknownType; d->type = RunnerContext::UnknownType;
d->term.clear(); d->term.clear();
d->mimeType.clear(); d->mimeType.clear();
d->unlock(); UNLOCK(this);
// we still have to remove all the matches, since if the // we still have to remove all the matches, since if the
// ref count was 1 (e.g. only the RunnerContext is using // ref count was 1 (e.g. only the RunnerContext is using
@ -188,18 +185,18 @@ void RunnerContext::setSearchTerm(const QString &term)
return; return;
} }
d->lockForWrite(); LOCK_FOR_WRITE(this)
d->term = term; d->term = term;
d->unlock(); UNLOCK(this);
d->determineType(); d->determineType();
} }
QString RunnerContext::searchTerm() const QString RunnerContext::searchTerm() const
{ {
d->lockForRead(); LOCK_FOR_READ(this)
QString term = d->term; QString term = d->term;
d->unlock(); UNLOCK(this);
return term; return term;
} }
@ -219,9 +216,9 @@ bool RunnerContext::addMatches(const QString& term, const QList<SearchMatch*> &m
return false; return false;
} }
d->lockForWrite(); LOCK_FOR_WRITE(this)
d->matches << matches; d->matches << matches;
d->unlock(); UNLOCK(this);
//kDebug()<< "add matches"; //kDebug()<< "add matches";
// A copied searchContext may share the d pointer, // A copied searchContext may share the d pointer,
// we always want to sent the signal of the object that created // we always want to sent the signal of the object that created
@ -236,9 +233,9 @@ bool RunnerContext::addMatch(const QString &term, SearchMatch *match)
return false; return false;
} }
d->lockForWrite(); LOCK_FOR_WRITE(this)
d->matches << match; d->matches << match;
d->unlock(); UNLOCK(this);
//kDebug()<< "added match" << match->text(); //kDebug()<< "added match" << match->text();
emit d->q->matchesChanged(); emit d->q->matchesChanged();
@ -248,27 +245,30 @@ bool RunnerContext::addMatch(const QString &term, SearchMatch *match)
QList<SearchMatch *> RunnerContext::matches() const QList<SearchMatch *> RunnerContext::matches() const
{ {
d->lockForRead(); LOCK_FOR_READ(this)
QList<SearchMatch *> matches = d->matches; QList<SearchMatch *> matches = d->matches;
d->unlock(); UNLOCK(this);
return matches; return matches;
} }
void RunnerContext::removeAllMatches() void RunnerContext::removeAllMatches()
{ {
d->lockForWrite(); LOCK_FOR_WRITE(this)
if (!d->matches.isEmpty()) { if (!d->matches.isEmpty()) {
QList<SearchMatch*> matches = d->matches;
d->matches.clear(); d->matches.clear();
d->unlock(); UNLOCK(this);
emit d->q->matchesChanged();
// in case someone is still holding on to the Matches // in case someone is still holding on to the Matches
// when we emit the matchesChanged() signal, we don't // when we emit the matchesChanged() signal, we don't
// delete the matches until after the signal is handled. // delete the matches until after the signal is handled.
// a bit safer. // a bit safer.
d->lockForWrite(); emit d->q->matchesChanged();
qDeleteAll(d->matches);
qDeleteAll(matches);
} else {
UNLOCK(this);
} }
d->unlock();
} }
} }