From 3516bdf6a4a44464d658430a7293742a3f70eb1f Mon Sep 17 00:00:00 2001 From: Dave West Date: Fri, 30 Nov 2007 17:51:44 +0000 Subject: [PATCH] Added a macro to get a threads structure. Spead up CtdlThreadCheckStop() by requiring the caller pass in its own thread structure. --- citadel/include/ctdl_module.h | 9 +++++- citadel/modules/checkpoint/serv_checkpoint.c | 4 ++- citadel/modules/fulltext/serv_fulltext.c | 8 +++-- citadel/sysdep.c | 31 +++++++++++++------- 4 files changed, 37 insertions(+), 15 deletions(-) diff --git a/citadel/include/ctdl_module.h b/citadel/include/ctdl_module.h index e979830e8..c2a20630d 100644 --- a/citadel/include/ctdl_module.h +++ b/citadel/include/ctdl_module.h @@ -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; \ diff --git a/citadel/modules/checkpoint/serv_checkpoint.c b/citadel/modules/checkpoint/serv_checkpoint.c index 819f1989b..98c685856 100644 --- a/citadel/modules/checkpoint/serv_checkpoint.c +++ b/citadel/modules/checkpoint/serv_checkpoint.c @@ -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); } diff --git a/citadel/modules/fulltext/serv_fulltext.c b/citadel/modules/fulltext/serv_fulltext.c index 4a8bdc644..4737f66f0 100644 --- a/citadel/modules/fulltext/serv_fulltext.c +++ b/citadel/modules/fulltext/serv_fulltext.c @@ -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); } diff --git a/citadel/sysdep.c b/citadel/sysdep.c index 651912dde..5358fb3d4 100644 --- a/citadel/sysdep.c +++ b/citadel/sysdep.c @@ -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; } -- 2.30.2