]> code.citadel.org Git - citadel.git/blobdiff - citadel/threads.c
* by counting up threads starting at 3, we seem to get what top shows in the LWPID...
[citadel.git] / citadel / threads.c
index 40c4cca4c8c55cfde88f00cf6598689a36a6b858..f6221019a9e95377935d181d87defa955ad85150 100644 (file)
@@ -58,6 +58,7 @@
  * remove the need for the calls to eCrashRegisterThread and friends
  */
 
+static int next_tid = 3;                        /* offset LWPID to PID */
 static int num_threads = 0;                    /* Current number of threads */
 static int num_workers = 0;                    /* Current number of worker threads */
 long statcount = 0;            /* are we doing a stats check? */
@@ -345,10 +346,9 @@ void CtdlThreadStopAll(void)
        GC_thread->state = CTDL_THREAD_STOP_REQ;
        while(this_thread)
        {
-#ifdef THREADS_USESIGNALS
                if (!citthread_equal(this_thread->tid, GC_thread->tid))
                        citthread_kill(this_thread->tid, SIGHUP);
-#endif
+
                ctdl_thread_internal_change_state (this_thread, CTDL_THREAD_STOP_REQ);
                citthread_cond_signal(&this_thread->ThreadCond);
                citthread_cond_signal(&this_thread->SleepCond);
@@ -410,16 +410,18 @@ double CtdlThreadGetWorkerAvg(void)
 
 double CtdlThreadGetLoadAvg(void)
 {
-       double load_avg[3] ;
+       double load_avg[3] = {0.0, 0.0, 0.0};
 
-       int ret;
+       int ret = 0;
        int smp_num_cpus;
 
        /* Borrowed this straight from procps */
        smp_num_cpus = sysconf(_SC_NPROCESSORS_ONLN);
        if(smp_num_cpus<1) smp_num_cpus=1; /* SPARC glibc is buggy */
 
+#ifdef HAVE_GETLOADAVG
        ret = getloadavg(load_avg, 3);
+#endif
        if (ret < 0)
                return 0;
        return load_avg[0] / smp_num_cpus;
@@ -508,13 +510,13 @@ int CtdlThreadCheckStop(void)
        
        state = CT->state;
 
-#ifdef THREADS_USESIGNALS
        if (CT->signal)
        {
                CtdlLogPrintf(CTDL_DEBUG, "Thread \"%s\" caught signal %d.\n", CT->name, CT->signal);
+               if (CT->signal == SIGHUP)
+                       CT->state = CTDL_THREAD_STOP_REQ;
                CT->signal = 0;
        }
-#endif
        if(state == CTDL_THREAD_STOP_REQ)
        {
                CT->state = CTDL_THREAD_STOPPING;
@@ -544,10 +546,10 @@ void CtdlThreadStop(CtdlThreadNode *thread)
                return;
        if (!(this_thread->thread_func))
                return;         // Don't stop garbage collector
-#ifdef THREADS_USESIGNALS
+
        if (!citthread_equal(this_thread->tid, GC_thread->tid))
                citthread_kill(this_thread->tid, SIGHUP);
-#endif
+
        ctdl_thread_internal_change_state (this_thread, CTDL_THREAD_STOP_REQ);
        citthread_cond_signal(&this_thread->ThreadCond);
        citthread_cond_signal(&this_thread->SleepCond);
@@ -593,10 +595,20 @@ static void ctdl_internal_thread_cleanup(void *arg)
         * In here we were called by the current thread because it is exiting
         * NB. WE ARE THE CURRENT THREAD
         */
-       CtdlLogPrintf(CTDL_NOTICE, "Thread \"%s\" (0x%08lx) exited.\n", CT->name, CT->tid);
+       if (CT)
+       {
+               const char *name = CT->name;
+               const pid_t tid = CT->tid;
+
+               CtdlLogPrintf(CTDL_NOTICE, "Thread \"%s\" (0x%08lx) exited.\n", name, tid);
+       }
+       else 
+       {
+               CtdlLogPrintf(CTDL_NOTICE, "some ((unknown ? ? ?) Thread exited.\n");
+       }
        
        #ifdef HAVE_BACKTRACE
-       eCrash_UnregisterThread();
+///    eCrash_UnregisterThread();
        #endif
        
        citthread_mutex_lock(&CT->ThreadMutex);
@@ -661,7 +673,7 @@ void CtdlThreadGC (void)
        CtdlThreadNode *this_thread, *that_thread;
        int workers = 0, sys_workers;
        int ret=0;
-       
+
        begin_critical_section(S_THREAD_LIST);
        
        /* Handle exiting of garbage collector thread */
@@ -694,13 +706,13 @@ void CtdlThreadGC (void)
                
                if (that_thread->stop_ticker + 5 == time(NULL))
                {
-                       CtdlLogPrintf(CTDL_DEBUG, "Thread System: The thread \"%s\" (0x%08lx) failed to self terminate within 5 ticks. Canceling now.\n", that_thread->name, that_thread->tid);
+                       CtdlLogPrintf(CTDL_DEBUG, "Thread System: The thread \"%s\" (0x%08lx) failed to self terminate within 5 ticks. It would be cancelled now.\n", that_thread->name, that_thread->tid);
                        if ((that_thread->flags & CTDLTHREAD_WORKER) == 0)
                                CtdlLogPrintf(CTDL_INFO, "Thread System: A non worker thread would have been canceled this may cause message loss.\n");
-                       that_thread->state = CTDL_THREAD_CANCELLED;
+//                     that_thread->state = CTDL_THREAD_CANCELLED;
                        that_thread->stop_ticker++;
-                       citthread_cancel(that_thread->tid);
-                       continue;
+//                     citthread_cancel(that_thread->tid);
+//                     continue;
                }
                
                /* Do we need to clean up this thread? */
@@ -824,7 +836,7 @@ static void *ctdl_internal_thread_func (void *arg)
        
        // Register for tracing
        #ifdef HAVE_BACKTRACE
-       eCrash_RegisterThread(this_thread->name, 0);
+///    eCrash_RegisterThread(this_thread->name, 0);
        #endif
        
        // Tell the world we are here
@@ -978,7 +990,8 @@ CtdlThreadNode *ctdl_internal_create_thread(char *name, long flags, void *(*thre
                free(this_thread);
                return NULL;
        }
-       
+       this_thread->reltid = next_tid;
+       next_tid++;
        num_threads++;  // Increase the count of threads in the system.
        if(this_thread->flags & CTDLTHREAD_WORKER)
                num_workers++;
@@ -1265,8 +1278,10 @@ void go_threading(void)
        ctdl_thread_internal_init();
 
        /* Second call to module init functions now that threading is up */
-       if (!statcount)
+       if (!statcount) {
                initialise_modules(1);
+               CtdlThreadCreate("select_on_master", CTDLTHREAD_BIGSTACK, select_on_master, NULL);
+       }
        else {
                CtdlLogPrintf(CTDL_EMERG, "Running connection simulation stats\n");
                gettimeofday(&start, NULL);