]> code.citadel.org Git - citadel.git/blobdiff - citadel/server_main.c
Changed the way we create aditional threads. One for every connection is
[citadel.git] / citadel / server_main.c
index 98d0162e575c628e014c5cca97945c2da9e7562f..3715bb356fba85b797906885d5c864317f92bb60 100644 (file)
@@ -368,8 +368,12 @@ void go_threading(void)
         */
        CtdlLogPrintf(CTDL_INFO, "Startup thread %d becoming garbage collector,\n", pthread_self());
 
-       /* Sleep 10 seconds before first garbage collection */  
-       CtdlThreadSleep(10);
+       /*
+        * We do a lot of locking and unlocking of the thread list in here.
+        * We do this so that we can repeatedly release time for other threads
+        * that may be waiting on the thread list.
+        * We are a low priority thread so we can afford to do this
+        */
        
        while (CtdlThreadGetCount())
        {
@@ -379,16 +383,75 @@ void go_threading(void)
                begin_critical_section(S_THREAD_LIST);
                ctdl_thread_internal_calc_loadavg();
                end_critical_section(S_THREAD_LIST);
-               CtdlThreadSleep(1);
+       
+               /* Reduce the size of the worker thread pool if necessary. */
+               if ((CtdlThreadGetWorkers() > config.c_min_workers) && (CtdlThreadWorkerAvg < 20))
+               {
+                       /* Ask a worker thread to stop as we no longer need it */
+                       begin_critical_section(S_THREAD_LIST);
+                       last_worker = CtdlThreadList;
+                       while (last_worker)
+                       {
+                               if (last_worker->flags & CTDLTHREAD_WORKER && last_worker->state > CTDL_THREAD_STOPPING)
+                                       break;
+                               else
+                                       last_worker = last_worker->next;
+                       }
+                       end_critical_section(S_THREAD_LIST);
+                       if (last_worker)
+                       {
+#ifdef WITH_THREADLOG
+                               CtdlLogPrintf(CTDL_DEBUG, "Thread system, stopping excess worker thread \"%s\" (%ld).\n",
+                                       last_worker->name,
+                                       last_worker->tid
+                                       );
+#endif
+                               CtdlThreadStop(last_worker);
+                       }
+               }
+       
+               /*
+                * If all our workers are working hard, start some more to help out
+                * with things
+                */
+               begin_critical_section(S_THREAD_LIST);
+               /* FIXME: come up with a better way to dynamically alter the number of threads
+                * based on the system load
+                */
+//             if ((CtdlThreadGetWorkers() < config.c_max_workers) && (CtdlThreadGetWorkers() < num_sessions))
+               // && (CtdlThreadLoadAvg < 90) )
+               if ((CtdlThreadGetWorkers() < config.c_max_workers) && (CtdlThreadWorkerAvg > 60) && (CtdlThreadLoadAvg < 90) )
+               {
+                       end_critical_section(S_THREAD_LIST);
+                       for (i=0; i<5 ; i++)
+//                     for (i=0; i< (num_sessions - CtdlThreadGetWorkers()) ; i++)
+//                     for (i=0; i< (10 - (55 - CtdlThreadWorkerAvg) / CtdlThreadWorkerAvg / CtdlThreadGetWorkers()) ; i++)
+                       {
+                               begin_critical_section(S_THREAD_LIST);
+                               ctdl_internal_create_thread("Worker Thread",
+                                       CTDLTHREAD_BIGSTACK + CTDLTHREAD_WORKER,
+                                       worker_thread,
+                                       NULL
+                                       );
+                               end_critical_section(S_THREAD_LIST);
+                       }
+               }
+               else
+                       end_critical_section(S_THREAD_LIST);
+               
                begin_critical_section(S_THREAD_LIST);
                ctdl_internal_thread_gc();
                end_critical_section(S_THREAD_LIST);
+               
                if (CtdlThreadGetCount() <= 1) // Shutting down clean up the garbage collector
                {
                        begin_critical_section(S_THREAD_LIST);
                        ctdl_internal_thread_gc();
                        end_critical_section(S_THREAD_LIST);
                }
+               
+               if (CtdlThreadGetCount())
+                       CtdlThreadSleep(1);
        }
        /*
         * If the above loop exits we must be shutting down since we obviously have no threads