THREADS: re-install the max workers & min spare workers configs; lock thread count...
authorWilfried Goesgens <dothebart@citadel.org>
Sat, 20 Apr 2013 10:48:45 +0000 (12:48 +0200)
committerWilfried Goesgens <dothebart@citadel.org>
Sat, 20 Apr 2013 10:48:45 +0000 (12:48 +0200)
citadel/sysdep.c
citadel/threads.c
citadel/threads.h

index 1caf2ce10b403987d9d8e53df1a3f89cafe44dd7..4ea07f3edcd95ffc34802439689098d83d7245c8 100644 (file)
@@ -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);
 }
 
index 4a4fe5163ec2260335ca7e537e1a39ea7cf2f601..bea9665c3c5eba17ec14aa8af7fd6e16f59c431d 100644 (file)
@@ -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);
index 2119e02395ce816186c59359f81c169ec44318f7..11bad4f09e305606555a5f4930d5ebfe4b7d85ce 100644 (file)
@@ -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