From: Wilfried Goesgens Date: Sat, 20 Apr 2013 10:48:45 +0000 (+0200) Subject: THREADS: re-install the max workers & min spare workers configs; lock thread count... X-Git-Tag: v8.20~56 X-Git-Url: https://code.citadel.org/?p=citadel.git;a=commitdiff_plain;h=f2f392abe416b03750cb053fd0780f62c85d3ae4 THREADS: re-install the max workers & min spare workers configs; lock thread count operations. --- diff --git a/citadel/sysdep.c b/citadel/sysdep.c index 1caf2ce10..4ea07f3ed 100644 --- a/citadel/sysdep.c +++ b/citadel/sysdep.c @@ -1157,7 +1157,9 @@ void *worker_thread(void *blah) { CitContext *con = NULL; /* Temporary context pointer */ int i; + pthread_mutex_lock(&ThreadCountMutex); ++num_workers; + pthread_mutex_unlock(&ThreadCountMutex); while (!server_shutting_down) { @@ -1339,7 +1341,10 @@ do_select: force_purge = 0; SKIP_SELECT: /* We're bound to a session */ + pthread_mutex_lock(&ThreadCountMutex); ++active_workers; + pthread_mutex_unlock(&ThreadCountMutex); + if (bind_me != NULL) { become_session(bind_me); @@ -1373,11 +1378,22 @@ SKIP_SELECT: dead_session_purge(force_purge); do_housekeeping(); + + pthread_mutex_lock(&ThreadCountMutex); --active_workers; + if (active_workers + config.c_min_workers < num_workers) + { + num_workers--; + pthread_mutex_unlock(&ThreadCountMutex); + return (NULL); + } + pthread_mutex_unlock(&ThreadCountMutex); } /* If control reaches this point, the server is shutting down */ + pthread_mutex_lock(&ThreadCountMutex); --num_workers; + pthread_mutex_unlock(&ThreadCountMutex); return(NULL); } diff --git a/citadel/threads.c b/citadel/threads.c index 4a4fe5163..bea9665c3 100644 --- a/citadel/threads.c +++ b/citadel/threads.c @@ -63,6 +63,7 @@ pthread_mutex_t Critters[MAX_SEMAPHORES]; /* Things needing locking */ struct thread_tsd masterTSD; int server_shutting_down = 0; /* set to nonzero during shutdown */ +pthread_mutex_t ThreadCountMutex;; void InitializeSemaphores(void) @@ -191,6 +192,8 @@ void go_threading(void) abort(); } + pthread_mutex_init(&ThreadCountMutex, NULL); + /* Second call to module init functions now that threading is up */ initialise_modules(1); @@ -198,10 +201,10 @@ void go_threading(void) CtdlThreadCreate(worker_thread); /* The supervisor thread monitors worker threads and spawns more of them if it finds that - * they are all in use. FIXME make the 256 max threads a configurable value. + * they are all in use. */ while (!server_shutting_down) { - if ((active_workers == num_workers) && (num_workers < 256)) { + if ((active_workers == num_workers) && (num_workers < config.c_max_workers)) { CtdlThreadCreate(worker_thread); } usleep(1000000); diff --git a/citadel/threads.h b/citadel/threads.h index 2119e0239..11bad4f09 100644 --- a/citadel/threads.h +++ b/citadel/threads.h @@ -45,4 +45,7 @@ void go_threading(void); void InitializeMasterTSD(void); void CtdlThreadCreate(void *(*start_routine)(void*)); + +extern pthread_mutex_t ThreadCountMutex;; + #endif // THREADS_H