Added a macro to get a threads structure.
authorDave West <davew@uncensored.citadel.org>
Fri, 30 Nov 2007 17:51:44 +0000 (17:51 +0000)
committerDave West <davew@uncensored.citadel.org>
Fri, 30 Nov 2007 17:51:44 +0000 (17:51 +0000)
Spead up CtdlThreadCheckStop() by requiring the caller pass in its own
thread structure.

citadel/include/ctdl_module.h
citadel/modules/checkpoint/serv_checkpoint.c
citadel/modules/fulltext/serv_fulltext.c
citadel/sysdep.c

index e979830e8506da47b2903ff33170a1070786e702..c2a20630d9ac10bea526f4849ed5bdd1a52b5d32 100644 (file)
@@ -111,13 +111,20 @@ void CtdlModuleStartCryptoMsgs(char *ok_response, char *nosup_response, char *er
 struct CtdlThreadNode *CtdlThreadCreate(char *name, long flags, void *(*thread_func) (void *arg), void *args);
 void CtdlThreadSleep(int secs);
 void CtdlThreadStop(struct CtdlThreadNode *thread);
-int CtdlThreadCheckStop(void);
+int CtdlThreadCheckStop(struct CtdlThreadNode *thread);
 void CtdlThreadCancel(struct CtdlThreadNode *thread);
 char *CtdlThreadName(struct CtdlThreadNode *thread, char *name);
 struct CtdlThreadNode *CtdlThreadSelf(void);
 int CtdlThreadGetCount(void);
 void CtdlThreadGC(void);
 void CtdlThreadStopAll(void);
+/* Macros to speed up getting outr thread */
+#define CT _this_cit_thread
+#define CT_PUSH() \
+       struct CtdlThreadNode *_this_cit_thread;\
+       _this_cit_thread = CtdlThreadSelf()
+
+
 #ifdef WITH_THREADLOG
 #define CtdlThreadPushName(NAME) \
        char *_push_name; \
index 819f1989b7bc20822feadb8b6478837926c6cf92..98c685856e056fce248f8d0bcf3c6207189a2e3f 100644 (file)
@@ -44,6 +44,8 @@
 void *checkpoint_thread(void *arg) {
        struct CitContext checkpointCC;
 
+       CT_PUSH();
+       
        CtdlLogPrintf(CTDL_DEBUG, "checkpoint_thread() initializing\n");
 
        memset(&checkpointCC, 0, sizeof(struct CitContext));
@@ -53,7 +55,7 @@ void *checkpoint_thread(void *arg) {
 
        cdb_allocate_tsd();
 
-       while (!CtdlThreadCheckStop()) {
+       while (!CtdlThreadCheckStop(CT)) {
                cdb_checkpoint();
                CtdlThreadSleep(60);
        }
index 4a8bdc6440e03b8695ddacd4005cc22b69964901..4737f66f07a7069a5f7e16b174c35cec0f23107f 100644 (file)
@@ -222,6 +222,8 @@ void do_fulltext_indexing(void) {
        time_t run_time = 0L;
        time_t end_time = 0L;
        
+       CT_PUSH();
+       
        /*
         * Don't do this if the site doesn't have it enabled.
         */
@@ -297,7 +299,7 @@ void do_fulltext_indexing(void) {
                        ft_index_message(ft_newmsgs[i], 1);
 
                        /* Check to see if we need to quit early */
-                       if (CtdlThreadCheckStop()) {
+                       if (CtdlThreadCheckStop(CT)) {
                                lprintf(CTDL_DEBUG, "Indexer quitting early\n");
                                ft_newhighest = ft_newmsgs[i];
                                break;
@@ -339,6 +341,8 @@ void do_fulltext_indexing(void) {
 void *indexer_thread(void *arg) {
        struct CitContext indexerCC;
 
+       CT_PUSH();
+       
        lprintf(CTDL_DEBUG, "indexer_thread() initializing\n");
 
        memset(&indexerCC, 0, sizeof(struct CitContext));
@@ -348,7 +352,7 @@ void *indexer_thread(void *arg) {
 
        cdb_allocate_tsd();
 
-       while (!CtdlThreadCheckStop()) {
+       while (!CtdlThreadCheckStop(CT)) {
                do_fulltext_indexing();
                CtdlThreadSleep(300);
        }
index 651912dde1f9a032bb4e928e422b216f67cc89da..5358fb3d403c4e93ffb515211d8819c984af74e6 100644 (file)
@@ -1244,6 +1244,11 @@ int CtdlThreadGetCount(void)
        return num_threads;
 }
 
+int CtdlThreadGetWorkers(void)
+{
+       return num_workers;
+}
+
 /*
  * A function to find the thread structure for this thread
  */
@@ -1287,7 +1292,7 @@ char *CtdlThreadName(struct CtdlThreadNode *thread, char *name)
                this_thread = thread;
        if (!this_thread)
        {
-               CtdlLogPrintf(CTDL_WARNING, "Thread system WARNING. Attempt to CtdlThreadRename() a non thread.\n");
+               CtdlLogPrintf(CTDL_WARNING, "Thread system WARNING. Attempt to CtdlThreadRename() a non thread. %s\n", name);
                return NULL;
        }
        begin_critical_section(S_THREAD_LIST);
@@ -1337,25 +1342,27 @@ void CtdlThreadCancel(struct CtdlThreadNode *thread)
 /*
  * A function for a thread to check if it has been asked to stop
  */
-int CtdlThreadCheckStop(void)
+int CtdlThreadCheckStop(struct CtdlThreadNode *this_thread)
 {
-       struct CtdlThreadNode *this_thread;
-       
-       this_thread = CtdlThreadSelf();
        if (!this_thread)
        {
                CtdlLogPrintf(CTDL_EMERG, "Thread system PANIC, CtdlThreadCheckStop() called by a non thread.\n");
                CtdlThreadStopAll();
                return -1;
        }
+       pthread_mutex_lock(&this_thread->ThreadMutex);
        if(this_thread->state == CTDL_THREAD_STOP_REQ)
        {
                this_thread->state = CTDL_THREAD_STOPPING;
+               pthread_mutex_unlock(&this_thread->ThreadMutex);
                return -1;
        }
        else if(this_thread->state < CTDL_THREAD_STOP_REQ)
+       {
+               pthread_mutex_unlock(&this_thread->ThreadMutex);
                return -1;
-               
+       }
+       pthread_mutex_unlock(&this_thread->ThreadMutex);
        return 0;
 }
 
@@ -1908,10 +1915,12 @@ void *worker_thread(void *arg) {
        struct timeval tv;
        int force_purge = 0;
        int m;
-
+       
+       CT_PUSH();
+       
        cdb_allocate_tsd();
 
-       while (!CtdlThreadCheckStop()) {
+       while (!CtdlThreadCheckStop(CT)) {
 
                /* make doubly sure we're not holding any stale db handles
                 * which might cause a deadlock.
@@ -1957,14 +1966,14 @@ do_select:      force_purge = 0;
                        }
                }
 
-               if (!CtdlThreadCheckStop()) {
+               if (!CtdlThreadCheckStop(CT)) {
                        tv.tv_sec = 1;          /* wake up every second if no input */
                        tv.tv_usec = 0;
                        retval = CtdlThreadSelect(highest + 1, &readfds, NULL, NULL, &tv);
 //                     retval = select(highest + 1, &readfds, NULL, NULL, &tv);
                }
 
-               if (CtdlThreadCheckStop()) return(NULL);
+               if (CtdlThreadCheckStop(CT)) return(NULL);
 
                /* Now figure out who made this select() unblock.
                 * First, check for an error or exit condition.
@@ -1978,7 +1987,7 @@ do_select:        force_purge = 0;
                        if (errno != EINTR) {
                                CtdlLogPrintf(CTDL_EMERG, "Exiting (%s)\n", strerror(errno));
                                CtdlThreadStopAll();
-                       } else if (!CtdlThreadCheckStop()) {
+                       } else if (!CtdlThreadCheckStop(CT)) {
                                CtdlLogPrintf(CTDL_DEBUG, "Un handled select failure.\n");
                                goto do_select;
                        }