[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    playground/base/strigi/src/daemon
From:       Jos van den Oever <jos () vandenoever ! info>
Date:       2006-08-31 21:30:06
Message-ID: 1157059806.548951.1476.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 579346 by vandenoever:

Furter refactoring of threads in the daemon.

 M  +2 -0      daemon.cpp  
 M  +1 -1      eventlistener/eventlistener.h  
 M  +6 -7      eventlistener/inotifylistener.cpp  
 M  +15 -17    indexscheduler.cpp  
 M  +3 -3      indexscheduler.h  
 M  +1 -1      socketserver.h  
 M  +20 -5     strigithread.cpp  
 M  +11 -11    strigithread.h  


--- trunk/playground/base/strigi/src/daemon/daemon.cpp #579345:579346
@@ -64,7 +64,9 @@
 stopThreads() {
     vector<StrigiThread*>::const_iterator i;
     for (i=threads.begin(); i!=threads.end(); ++i) {
+        printf("stopping thread %s\n", (*i)->name);
         (*i)->stop();
+        printf("stopped another thread\n");
     }
 }
 
--- trunk/playground/base/strigi/src/daemon/eventlistener/eventlistener.h \
#579345:579346 @@ -29,7 +29,7 @@
 
 class EventListener : public StrigiThread {
 public:
-    EventListener() {
+    EventListener(const char* name) :StrigiThread(name) {
         m_eventQueue = 0;
         m_filterManager = 0;
     }
--- trunk/playground/base/strigi/src/daemon/eventlistener/inotifylistener.cpp \
#579345:579346 @@ -70,15 +70,14 @@
     pthread_exit(0);
 }
 
-InotifyListener::InotifyListener()
-{
+InotifyListener::InotifyListener() :EventListener("InotifyListener") {
     iListener = this;
     
     // listen only to interesting events
     m_iEvents = IN_CLOSE_WRITE | IN_MODIFY | IN_MOVED_FROM | IN_MOVED_TO | IN_CREATE \
| IN_DELETE | IN_DELETE_SELF | IN_MOVE_SELF;  
     m_bMonitor = true;
-    state = Idling;
+    setState(Idling);
     m_bInitialized = false;
     m_eventQueue = NULL;
     m_pIndexReader = NULL;
@@ -126,15 +125,15 @@
 
 void* InotifyListener::run(void*)
 {
-    while (state != Stopping)
+    while (getState() != Stopping)
     {
         watch();
         
-        if (state == Working)
-            state = Idling;
+        if (getState() == Working)
+            setState(Idling);
     }
     
-    printf("exit inotify: %i\n", state);
+    printf("exit inotify: %i\n", getState());
     return 0;
 }
 /*
--- trunk/playground/base/strigi/src/daemon/indexscheduler.cpp #579345:579346
@@ -38,7 +38,7 @@
 
 IndexScheduler* sched;
 
-IndexScheduler::IndexScheduler() {
+IndexScheduler::IndexScheduler() :StrigiThread("IndexScheduler") {
     sched = this;
     m_listenerEventQueue = NULL;
     m_filterManager = NULL;
@@ -48,7 +48,7 @@
 bool
 IndexScheduler::addFileCallback(const char* path, uint dirlen, uint len,
         time_t mtime) {
-    if (sched->state != Working) return false;
+    if (sched->getState() != Working) return false;
     // only read files that do not start with '.'
     if (strstr(path, "/.")) return true;
     // check filtering rules given by user
@@ -75,7 +75,8 @@
     return true;
 }
 std::string
-IndexScheduler::getState() {
+IndexScheduler::getStateString() {
+    State state = getState();
     if (state == Idling) return "idling";
     if (state == Working) return "indexing";
     return "stopping";
@@ -94,25 +95,22 @@
 }
 void *
 IndexScheduler::run(void*) {
-    while (state != Stopping) {
+    while (getState() != Stopping) {
         shortsleep(100000000);
-        if (state == Working) {
+        if (getState() == Working) {
             index();
-            if (state == Working) {
-                state = Idling;
+            if (getState() == Working) {
+                setState(Idling);
             }
-        }
-        else if (state == Idling)
-        {
+        } else if (getState() == Idling) {
             if (m_listenerEventQueue == NULL)
                 return 0;
             
             vector <Event*> events = m_listenerEventQueue->getEvents();
-            if (events.size() > 0)
-            {
-                state = Working;
+            if (events.size() > 0) {
+                setState(Working);
                 processListenerEvents(events);
-                state = Idling;
+                setState(Idling);
             }
         }
     }
@@ -151,7 +149,7 @@
 
     vector<string> todelete;
     map<string,time_t>::iterator it = dbfiles.begin();
-    while (state == Working && it != dbfiles.end()) {
+    while (getState() == Working && it != dbfiles.end()) {
         todelete.push_back(it->first);
 //        writer->deleteEntry(it->first);
         dbfiles.erase(it++);
@@ -159,14 +157,14 @@
     writer->deleteEntries(todelete);
 
     it = toindex.begin();
-    while (state == Working && it != toindex.end()) {
+    while (getState() == Working && it != toindex.end()) {
         streamindexer->indexFile(it->first);
         if (writer->itemsInCache() > 10000) {
             writer->commit();
         }
         toindex.erase(it++);
     }
-    if (state == Working) {
+    if (getState() == Working) {
         writer->commit();
         writer->optimize();
     }
--- trunk/playground/base/strigi/src/daemon/indexscheduler.h #579345:579346
@@ -61,9 +61,9 @@
     void setEventListenerQueue (EventListenerQueue* eventQueue) { \
                m_listenerEventQueue = eventQueue; }
     void setFilterManager (FilterManager* filterManager) { m_filterManager = \
filterManager;}  int getQueueSize();
-    void startIndexing() { state = Working; }
-    void stopIndexing() { state = Idling; }
-    std::string getState();
+    void startIndexing() { setState(Working); }
+    void stopIndexing() { setState(Idling); }
+    std::string getStateString();
     ~IndexScheduler();
     const std::set<std::string> &getIndexedDirectories() const {
         return dirstoindex;
--- trunk/playground/base/strigi/src/daemon/socketserver.h #579345:579346
@@ -39,7 +39,7 @@
 
     void* run(void*);
 public:
-    SocketServer(Interface* i) :interface(i) {}
+    SocketServer(Interface* i) :StrigiThread("SocketServer"), interface(i) {}
     void setSocketName(const std::string& name) {
         socketname = name;
     }
--- trunk/playground/base/strigi/src/daemon/strigithread.cpp #579345:579346
@@ -6,8 +6,6 @@
 #include <sys/resource.h>
 using namespace std;
 
-pthread_mutex_t StrigiThread::initlock = PTHREAD_MUTEX_INITIALIZER;
-
 void*
 threadstarter(void *d) {
     // give this thread job batch job priority
@@ -35,9 +33,26 @@
     pthread_exit(0);
 }
 
-StrigiThread::StrigiThread() {
-    state = Idling;
+StrigiThread::StrigiThread(const char* n) :state(Idling), name(n) {
+    pthread_mutex_init(&lock, NULL);
 }
+StrigiThread::~StrigiThread() {
+    pthread_mutex_destroy(&lock);
+}
+void
+StrigiThread::setState(State s) {
+    pthread_mutex_lock(&lock);
+    state = s;
+    pthread_mutex_unlock(&lock);
+}
+StrigiThread::State
+StrigiThread::getState() {
+    State s;
+    pthread_mutex_lock(&lock);
+    s = state;
+    pthread_mutex_unlock(&lock);
+    return s;
+}
 int
 StrigiThread::start() {
     // start the indexer thread
@@ -52,7 +67,7 @@
 StrigiThread::stop() {
     state = Stopping;
     if (thread) {
-        // wait for the indexer to finish
+        // wait for the thread to finish
         pthread_join(thread, 0);
     }
     thread = 0;
--- trunk/playground/base/strigi/src/daemon/strigithread.h #579345:579346
@@ -2,29 +2,29 @@
 #define STRIGITHREAD_H
 
 #include <pthread.h>
+
 class StrigiThread {
 friend void* threadstarter(void *);
-protected:
+public:
     enum State {Idling, Working, Stopping};
+private:
     State state;
+protected:
     pthread_mutex_t lock;
-    static pthread_mutex_t initlock;
     pthread_t thread;
+
+    void setState(State s);
     virtual void* run(void*) = 0;
 public:
-    StrigiThread();
-    virtual ~StrigiThread() {}
+    const char* const name;
+
+    StrigiThread(const char* name);
+    virtual ~StrigiThread();
     int start();
     void stop();
     void terminate();
+    State getState();
 };
 
-class Server;
-class ServerThread : public StrigiThread {
-public:
-    ServerThread(Server*);
-    void* run(void*);
-};
-
 #endif
 


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic