THREADS: re-install the max workers & min spare workers configs; lock thread count...
[citadel.git] / citadel / sysdep.c
index 193458d5667de921c4d0ead5b8cff7967c7823a8..4ea07f3edcd95ffc34802439689098d83d7245c8 100644 (file)
@@ -908,7 +908,8 @@ int client_getln(char *buf, int bufsize)
 void close_masters (void)
 {
        struct ServiceFunctionHook *serviceptr;
-       
+       const char *Text;
+
        /*
         * close all protocol master sockets
         */
@@ -917,8 +918,6 @@ void close_masters (void)
 
                if (serviceptr->tcp_port > 0)
                {
-                       const char *Text;
-
                        if (serviceptr->msock == -1)
                                Text = "not closing again";
                        else
@@ -1158,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) {
 
@@ -1340,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);
 
@@ -1374,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);
 }