use proper locking/sleeping object for idx thread sync

This commit is contained in:
dockes 2009-06-22 15:25:49 +00:00
parent e4f683f219
commit 8a377d135a
3 changed files with 46 additions and 8 deletions

View File

@ -20,6 +20,7 @@
#include <qthread.h> #include <qthread.h>
#include <qmutex.h> #include <qmutex.h>
#include <qwaitcondition.h>
#include "indexer.h" #include "indexer.h"
#include "debuglog.h" #include "debuglog.h"
@ -35,10 +36,14 @@ static string indexingReason;
static int stopidxthread; static int stopidxthread;
static QMutex curfile_mutex; static QMutex curfile_mutex;
static QMutex action_mutex;
static QWaitCondition action_wait;
class IdxThread : public QThread , public DbIxStatusUpdater { class IdxThread : public QThread , public DbIxStatusUpdater {
virtual void run(); virtual void run();
public: public:
// This gets called at intervals by the file system walker to check for
// a requested interrupt and update the current status.
virtual bool update() { virtual bool update() {
QMutexLocker locker(&curfile_mutex); QMutexLocker locker(&curfile_mutex);
m_statusSnap = status; m_statusSnap = status;
@ -59,12 +64,12 @@ class IdxThread : public QThread , public DbIxStatusUpdater {
void IdxThread::run() void IdxThread::run()
{ {
recoll_threadinit(); recoll_threadinit();
action_mutex.lock();
for (;;) { for (;;) {
if (stopidxthread) {
return;
}
if (startindexing) { if (startindexing) {
startindexing = 0; startindexing = 0;
action_mutex.unlock();
m_interrupted = false; m_interrupted = false;
indexingstatus = IDXTS_NULL; indexingstatus = IDXTS_NULL;
// We have to make a copy of the config (setKeydir changes // We have to make a copy of the config (setKeydir changes
@ -84,13 +89,21 @@ void IdxThread::run()
} }
rezero = false; rezero = false;
delete indexer; delete indexer;
} action_mutex.lock();
msleep(100); }
if (stopidxthread) {
action_mutex.unlock();
return;
}
action_wait.wait(&action_mutex);
} }
} }
static IdxThread idxthread; static IdxThread idxthread;
// Functions called by the main thread
void start_idxthread(const RclConfig& cnf) void start_idxthread(const RclConfig& cnf)
{ {
idxthread.cnf = &cnf; idxthread.cnf = &cnf;
@ -99,32 +112,49 @@ void start_idxthread(const RclConfig& cnf)
void stop_idxthread() void stop_idxthread()
{ {
action_mutex.lock();
startindexing = 0;
stopindexing = 1; stopindexing = 1;
stopidxthread = 1; stopidxthread = 1;
action_mutex.unlock();
action_wait.wakeAll();
idxthread.wait(); idxthread.wait();
} }
void stop_indexing() void stop_indexing()
{ {
action_mutex.lock();
startindexing = 0;
stopindexing = 1; stopindexing = 1;
action_mutex.unlock();
action_wait.wakeAll();
} }
void start_indexing(bool raz) void start_indexing(bool raz)
{ {
action_mutex.lock();
startindexing = 1; startindexing = 1;
rezero = raz; rezero = raz;
action_mutex.unlock();
action_wait.wakeAll();
} }
DbIxStatus idxthread_idxStatus() DbIxStatus idxthread_idxStatus()
{ {
QMutexLocker locker(&curfile_mutex); QMutexLocker locker(&curfile_mutex);
return idxthread.m_statusSnap; return idxthread.m_statusSnap;
} }
bool idxthread_idxInterrupted() bool idxthread_idxInterrupted()
{ {
return idxthread.m_interrupted; return idxthread.m_interrupted;
} }
string idxthread_getReason() string idxthread_getReason()
{ {
return indexingReason; return indexingReason;
} }
IdxThreadStatus idxthread_getStatus() IdxThreadStatus idxthread_getStatus()
{ {
return indexingstatus; return indexingstatus;

View File

@ -124,8 +124,6 @@ static void recollCleanup()
{ {
LOGDEB(("recollCleanup: writing settings\n")); LOGDEB(("recollCleanup: writing settings\n"));
rwSettings(true); rwSettings(true);
LOGDEB2(("recollCleanup: stopping idx thread\n"));
stop_idxthread();
LOGDEB2(("recollCleanup: closing database\n")); LOGDEB2(("recollCleanup: closing database\n"));
deleteZ(rcldb); deleteZ(rcldb);
deleteZ(rclconfig); deleteZ(rclconfig);

View File

@ -172,7 +172,9 @@ void RclMain::init()
if (prefs.catgToolBar) { if (prefs.catgToolBar) {
catgCMB = new QComboBox(FALSE, new QToolBar(this), "catCMB"); catgCMB = new QComboBox(FALSE, new QToolBar(this), "catCMB");
catgCMB->insertItem(tr("All")); catgCMB->insertItem(tr("All"));
#if (QT_VERSION >= 0x040000)
catgCMB->setToolTip(tr("Document category filter")); catgCMB->setToolTip(tr("Document category filter"));
#endif
} }
// Document categories buttons // Document categories buttons
@ -379,7 +381,11 @@ void RclMain::fileExit()
prefs.ssearchTyp = sSearch->searchTypCMB->currentItem(); prefs.ssearchTyp = sSearch->searchTypCMB->currentItem();
if (asearchform) if (asearchform)
delete asearchform; delete asearchform;
// Let the exit handler clean up things // We'd prefer to do this in the exit handler, but it's apparently to late
// and some of the qt stuff is already dead at this point?
LOGDEB2(("RclMain::fileExit() : stopping idx thread\n"));
stop_idxthread();
// Let the exit handler clean up the rest (internal recoll stuff).
exit(0); exit(0);
} }
@ -417,6 +423,7 @@ void RclMain::periodic100()
// Indexing is running // Indexing is running
m_idxStatusAck = false; m_idxStatusAck = false;
fileToggleIndexingAction->setText(tr("Stop &Indexing")); fileToggleIndexingAction->setText(tr("Stop &Indexing"));
fileToggleIndexingAction->setEnabled(TRUE);
// The toggle thing is for the status to flash // The toggle thing is for the status to flash
if (toggle == 0) { if (toggle == 0) {
QString msg = tr("Indexing in progress: "); QString msg = tr("Indexing in progress: ");
@ -455,6 +462,9 @@ void RclMain::periodic100()
fileExit(); fileExit();
} }
// This gets called when the indexing action is activated. It starts
// the requested action, and disables the menu entry. This will be
// re-enabled by the indexing status check
void RclMain::toggleIndexing() void RclMain::toggleIndexing()
{ {
if (idxthread_getStatus() == IDXTS_NULL) { if (idxthread_getStatus() == IDXTS_NULL) {