]> code.citadel.org Git - citadel.git/blobdiff - citadel/sysdep.c
Prevent an ocassional segflt on exit.
[citadel.git] / citadel / sysdep.c
index 2428152cef787f378efc9ccaf682d8f52ba77d13..cf12a75cddaf3d9f80b51d3a9b04e4bf1db966df 100644 (file)
@@ -1003,8 +1003,6 @@ int convert_login(char NameToConvert[]) {
  * If the thread is created *node will point to the thread control structure for the created thread.
  * If the thread creation fails *node remains NULL
  * Do not free the memory pointed to by *node, it doesn't belong to you.
- * If your thread function returns it will be started again without creating a new thread.
- * If your thread function wants to exit it should call CtdlThreadExit(ret_code);
  * This new interface duplicates much of the eCrash stuff. We should go for closer integration since that would
  * remove the need for the calls to eCrashRegisterThread and friends
  */
@@ -1022,6 +1020,8 @@ static pthread_t GC_thread;
 static char *CtdlThreadStates[CTDL_THREAD_LAST_STATE];
 double CtdlThreadLoadAvg = 0;
 double CtdlThreadWorkerAvg = 0;
+pthread_key_t ThreadKey;
+
 /*
  * Pinched the following bits regarding signals from Kannel.org
  */
@@ -1078,6 +1078,64 @@ static void ctdl_thread_internal_restore_signals(sigset_t *old_set)
 }
 
 
+
+/*
+ * A function to destroy the TSD
+ */
+static void ctdl_thread_internal_dest_tsd(void *arg)
+{
+       if (arg != NULL) {
+               check_handles(arg);
+               free(arg);
+       }
+}
+
+
+/*
+ * A function to initialise the thread TSD
+ */
+void ctdl_thread_internal_init_tsd(void)
+{
+       int ret;
+       
+       if ((ret = pthread_key_create(&ThreadKey, ctdl_thread_internal_dest_tsd))) {
+               lprintf(CTDL_EMERG, "pthread_key_create: %s\n",
+                       strerror(ret));
+               exit(CTDLEXIT_DB);
+       }
+}
+
+/*
+ * Ensure that we have a key for thread-specific data. 
+ *
+ * This should be called immediately after startup by any thread 
+ * 
+ */
+void CtdlThreadAllocTSD(void)
+{
+       ThreadTSD *tsd;
+
+       if (pthread_getspecific(ThreadKey) != NULL)
+               return;
+
+       tsd = malloc(sizeof(ThreadTSD));
+
+       tsd->tid = NULL;
+
+       memset(tsd->cursors, 0, sizeof tsd->cursors);
+       tsd->self = NULL;
+       
+       pthread_setspecific(ThreadKey, tsd);
+}
+
+
+void ctdl_thread_internal_free_tsd(void)
+{
+       ctdl_thread_internal_dest_tsd(pthread_getspecific(ThreadKey));
+       pthread_setspecific(ThreadKey, NULL);
+}
+
+
 void ctdl_thread_internal_cleanup(void)
 {
        int i;
@@ -1086,6 +1144,7 @@ void ctdl_thread_internal_cleanup(void)
        {
                free (CtdlThreadStates[i]);
        }
+       ctdl_thread_internal_free_tsd();
 }
 
 void ctdl_thread_internal_init(void)
@@ -1128,9 +1187,10 @@ void ctdl_thread_internal_init(void)
                return;
        }
 
-       this_thread->name = strdup("Garbage Collection Thread");
+       this_thread->name = "Garbage Collection Thread";
        
        this_thread->tid = GC_thread;
+       CT = this_thread;
        
        num_threads++;  // Increase the count of threads in the system.
 
@@ -1201,15 +1261,10 @@ void CtdlThreadStopAll(void)
        this_thread = CtdlThreadList;
        while(this_thread)
        {
-               if (this_thread->thread_func) // Don't tell garbage collector to stop
-               {
-                       ctdl_thread_internal_change_state (this_thread, CTDL_THREAD_STOP_REQ);
-//                     pthread_mutex_lock(&this_thread->ThreadMutex);
-                       pthread_cond_signal(&this_thread->ThreadCond);
-                       pthread_cond_signal(&this_thread->SleepCond);
-//                     pthread_mutex_unlock(&this_thread->ThreadMutex);
-                       CtdlLogPrintf(CTDL_DEBUG, "Thread system stopping thread \"%s\" (%ld).\n", this_thread->name, this_thread->tid);
-               }
+               ctdl_thread_internal_change_state (this_thread, CTDL_THREAD_STOP_REQ);
+               pthread_cond_signal(&this_thread->ThreadCond);
+               pthread_cond_signal(&this_thread->SleepCond);
+               CtdlLogPrintf(CTDL_DEBUG, "Thread system stopping thread \"%s\" (%ld).\n", this_thread->name, this_thread->tid);
                this_thread = this_thread->next;
        }
        end_critical_section(S_THREAD_LIST);
@@ -1231,10 +1286,8 @@ void CtdlThreadWakeAll(void)
        {
                if (!this_thread->thread_func)
                {
-//                     pthread_mutex_lock(&this_thread->ThreadMutex);
                        pthread_cond_signal(&this_thread->ThreadCond);
                        pthread_cond_signal(&this_thread->SleepCond);
-//                     pthread_mutex_unlock(&this_thread->ThreadMutex);
                }
                this_thread = this_thread->next;
        }
@@ -1285,64 +1338,27 @@ double CtdlThreadGetLoadAvg(void)
        return ret;
 }
 
-/*
- * A function to find the thread structure for this thread
- */
-struct CtdlThreadNode *CtdlThreadSelf(void)
-{
-       pthread_t self_tid;
-       struct CtdlThreadNode *this_thread;
-       
-       self_tid = pthread_self();
-       
-       begin_critical_section(S_THREAD_LIST);
-       this_thread = CtdlThreadList;
-       while(this_thread)
-       {
-               pthread_mutex_lock(&this_thread->ThreadMutex);
-               if (pthread_equal(self_tid, this_thread->tid))
-               {
-                       pthread_mutex_unlock(&this_thread->ThreadMutex);
-                       end_critical_section(S_THREAD_LIST);
-                       return this_thread;
-               }
-               pthread_mutex_unlock(&this_thread->ThreadMutex);
-               this_thread = this_thread->next;
-       }
-       end_critical_section(S_THREAD_LIST);
-       return NULL;
-}
-
 
 
 
 /*
  * A function to rename a thread
- * Returns a char * and the caller owns the memory and should free it
+ * Returns a const char *
  */
-char *CtdlThreadName(struct CtdlThreadNode *thread, char *name)
+const char *CtdlThreadName(const char *name)
 {
-       struct CtdlThreadNode *this_thread;
-       char *old_name;
+       const char *old_name;
        
-       if (!thread)
-               this_thread = CtdlThreadSelf();
-       else
-               this_thread = thread;
-       if (!this_thread)
+       if (!CT)
        {
                CtdlLogPrintf(CTDL_WARNING, "Thread system WARNING. Attempt to CtdlThreadRename() a non thread. %s\n", name);
                return NULL;
        }
-//     begin_critical_section(S_THREAD_LIST);
-       pthread_mutex_lock(&this_thread->ThreadMutex);
-       old_name = this_thread->name;
+       pthread_mutex_lock(&CT->ThreadMutex);
+       old_name = CT->name;
        if (name)
-               this_thread->name = strdup (name);
-       else
-               old_name = strdup(old_name);
-       pthread_mutex_unlock(&this_thread->ThreadMutex);
-//     end_critical_section (S_THREAD_LIST);
+               CT->name = name;
+       pthread_mutex_unlock(&CT->ThreadMutex);
        return (old_name);
 }      
 
@@ -1355,7 +1371,7 @@ void CtdlThreadCancel(struct CtdlThreadNode *thread)
        struct CtdlThreadNode *this_thread;
        
        if (!thread)
-               this_thread = CtdlThreadSelf();
+               this_thread = CT;
        else
                this_thread = thread;
        if (!this_thread)
@@ -1372,10 +1388,8 @@ void CtdlThreadCancel(struct CtdlThreadNode *thread)
                return;
        }
        
-//     begin_critical_section(S_THREAD_LIST);
        ctdl_thread_internal_change_state (this_thread, CTDL_THREAD_CANCELLED);
        pthread_cancel(this_thread->tid);
-//     end_critical_section (S_THREAD_LIST);
 }
 
 
@@ -1383,27 +1397,27 @@ void CtdlThreadCancel(struct CtdlThreadNode *thread)
 /*
  * A function for a thread to check if it has been asked to stop
  */
-int CtdlThreadCheckStop(struct CtdlThreadNode *this_thread)
+int CtdlThreadCheckStop(void)
 {
-       if (!this_thread)
+       if (!CT)
        {
                CtdlLogPrintf(CTDL_EMERG, "Thread system PANIC, CtdlThreadCheckStop() called by a non thread.\n");
                CtdlThreadStopAll();
                return -1;
        }
-       pthread_mutex_lock(&this_thread->ThreadMutex);
-       if(this_thread->state == CTDL_THREAD_STOP_REQ)
+       pthread_mutex_lock(&CT->ThreadMutex);
+       if(CT->state == CTDL_THREAD_STOP_REQ)
        {
-               this_thread->state = CTDL_THREAD_STOPPING;
-               pthread_mutex_unlock(&this_thread->ThreadMutex);
+               CT->state = CTDL_THREAD_STOPPING;
+               pthread_mutex_unlock(&CT->ThreadMutex);
                return -1;
        }
-       else if((this_thread->state < CTDL_THREAD_STOP_REQ) && (this_thread->state > CTDL_THREAD_CREATE))
+       else if((CT->state < CTDL_THREAD_STOP_REQ) && (CT->state > CTDL_THREAD_CREATE))
        {
-               pthread_mutex_unlock(&this_thread->ThreadMutex);
+               pthread_mutex_unlock(&CT->ThreadMutex);
                return -1;
        }
-       pthread_mutex_unlock(&this_thread->ThreadMutex);
+       pthread_mutex_unlock(&CT->ThreadMutex);
        return 0;
 }
 
@@ -1417,7 +1431,7 @@ void CtdlThreadStop(struct CtdlThreadNode *thread)
        struct CtdlThreadNode *this_thread;
        
        if (!thread)
-               this_thread = CtdlThreadSelf();
+               this_thread = CT;
        else
                this_thread = thread;
        if (!this_thread)
@@ -1425,13 +1439,9 @@ void CtdlThreadStop(struct CtdlThreadNode *thread)
        if (!(this_thread->thread_func))
                return;         // Don't stop garbage collector
                
-//     begin_critical_section (S_THREAD_LIST);
        ctdl_thread_internal_change_state (this_thread, CTDL_THREAD_STOP_REQ);
-//     pthread_mutex_lock(&this_thread->ThreadMutex);
        pthread_cond_signal(&this_thread->ThreadCond);
        pthread_cond_signal(&this_thread->SleepCond);
-//     pthread_mutex_unlock(&this_thread->ThreadMutex);
-//     end_critical_section(S_THREAD_LIST);
 }
 
 /*
@@ -1441,11 +1451,9 @@ void CtdlThreadSleep(int secs)
 {
        struct timespec wake_time;
        struct timeval time_now;
-       struct CtdlThreadNode *self;
        
        
-       self = CtdlThreadSelf();
-       if (!self)
+       if (!CT)
        {
                CtdlLogPrintf(CTDL_WARNING, "CtdlThreadSleep() called by something that is not a thread. Should we die?\n");
                return;
@@ -1456,19 +1464,13 @@ void CtdlThreadSleep(int secs)
        wake_time.tv_sec = time_now.tv_sec + secs;
        wake_time.tv_nsec = time_now.tv_usec * 10;
 
-//     begin_critical_section(S_THREAD_LIST);
-       ctdl_thread_internal_change_state (self, CTDL_THREAD_SLEEPING);
-//     end_critical_section(S_THREAD_LIST);
+       ctdl_thread_internal_change_state (CT, CTDL_THREAD_SLEEPING);
        
-//     pthread_mutex_lock(&self->SleepMutex); /* Prevent something asking us to awaken before we've gone to sleep */
-       pthread_mutex_lock(&self->ThreadMutex); /* Prevent something asking us to awaken before we've gone to sleep */
-       pthread_cond_timedwait(&self->SleepCond, &self->ThreadMutex, &wake_time);
-       pthread_mutex_unlock(&self->ThreadMutex);
-//     pthread_mutex_unlock(&self->SleepMutex);
+       pthread_mutex_lock(&CT->ThreadMutex); /* Prevent something asking us to awaken before we've gone to sleep */
+       pthread_cond_timedwait(&CT->SleepCond, &CT->ThreadMutex, &wake_time);
+       pthread_mutex_unlock(&CT->ThreadMutex);
        
-//     begin_critical_section(S_THREAD_LIST);
-       ctdl_thread_internal_change_state (self, CTDL_THREAD_RUNNING);
-//     end_critical_section(S_THREAD_LIST);
+       ctdl_thread_internal_change_state (CT, CTDL_THREAD_RUNNING);
 }
 
 
@@ -1477,22 +1479,19 @@ void CtdlThreadSleep(int secs)
  */
 static void ctdl_internal_thread_cleanup(void *arg)
 {
-       struct CtdlThreadNode *this_thread;
-       this_thread = CtdlThreadSelf();
        /*
         * In here we were called by the current thread because it is exiting
         * NB. WE ARE THE CURRENT THREAD
         */
-       CtdlLogPrintf(CTDL_NOTICE, "Thread \"%s\" (%ld) exited.\n", this_thread->name, this_thread->tid);
-//     begin_critical_section(S_THREAD_LIST);
+       CtdlLogPrintf(CTDL_NOTICE, "Thread \"%s\" (%ld) exited.\n", CT->name, CT->tid);
+       
        #ifdef HAVE_BACKTRACE
        eCrash_UnregisterThread();
        #endif
-       pthread_mutex_lock(&this_thread->ThreadMutex);
-       this_thread->state = CTDL_THREAD_EXITED;        // needs to be last thing else house keeping will unlink us too early
-       pthread_mutex_unlock(&this_thread->ThreadMutex);
-//     end_critical_section(S_THREAD_LIST);
-//     CtdlThreadGC();
+       
+       pthread_mutex_lock(&CT->ThreadMutex);
+       CT->state = CTDL_THREAD_EXITED; // needs to be last thing else house keeping will unlink us too early
+       pthread_mutex_unlock(&CT->ThreadMutex);
 }
 
 /*
@@ -1627,8 +1626,6 @@ void CtdlThreadGC (void)
                 * Now we own that thread entry
                 */
                CtdlLogPrintf(CTDL_INFO, "Garbage Collection for thread \"%s\" (%ld).\n", that_thread->name, that_thread->tid);
-               if(that_thread->name)
-                       free(that_thread->name);
                pthread_mutex_destroy(&that_thread->ThreadMutex);
                pthread_cond_destroy(&that_thread->ThreadCond);
                pthread_mutex_destroy(&that_thread->SleepMutex);
@@ -1675,6 +1672,8 @@ static void *ctdl_internal_thread_func (void *arg)
        // Register the cleanup function to take care of when we exit.
        pthread_cleanup_push(ctdl_internal_thread_cleanup, NULL);
        // Get our thread data structure
+       CtdlThreadAllocTSD();
+       CT = this_thread;
        this_thread->pid = getpid();
        memcpy(&this_thread->last_state_change, &this_thread->start_time, sizeof (struct timeval));     /* Changed state so mark it. */
        /* Only change to running state if we weren't asked to stop during the create cycle
@@ -1683,7 +1682,7 @@ static void *ctdl_internal_thread_func (void *arg)
         */
        pthread_mutex_unlock(&this_thread->ThreadMutex);
                
-       if (!CtdlThreadCheckStop(this_thread))
+       if (!CtdlThreadCheckStop())
        {
                pthread_mutex_lock(&this_thread->ThreadMutex);
                this_thread->state = CTDL_THREAD_RUNNING;
@@ -1704,7 +1703,7 @@ static void *ctdl_internal_thread_func (void *arg)
        /*
         * run the thread to do the work but only if we haven't been asked to stop
         */
-       if (!CtdlThreadCheckStop(this_thread))
+       if (!CtdlThreadCheckStop())
                ret = (this_thread->thread_func)(this_thread->user_args);
        
        /*
@@ -1792,11 +1791,11 @@ struct CtdlThreadNode *ctdl_internal_create_thread(char *name, long flags, void
         */
        if(name)
        {
-               this_thread->name = strdup(name);
+               this_thread->name = name;
        }
        else
        {
-               this_thread->name = strdup("Un-named Thread");
+               this_thread->name = "Un-named Thread";
        }
        
        this_thread->flags = flags;
@@ -1819,8 +1818,8 @@ struct CtdlThreadNode *ctdl_internal_create_thread(char *name, long flags, void
         * when no signals will be processed, but during that time they
         * should be queued by the operating system.
         */
-       if (pthread_equal(GC_thread, pthread_self())) 
-           sigtrick = ctdl_thread_internal_block_signals(&old_signal_set) == 0;
+//     if (pthread_equal(GC_thread, pthread_self())) 
+//         sigtrick = ctdl_thread_internal_block_signals(&old_signal_set) == 0;
 
        /*
         * We pass this_thread into the thread as its args so that it can find out information
@@ -1832,21 +1831,19 @@ struct CtdlThreadNode *ctdl_internal_create_thread(char *name, long flags, void
 
                CtdlLogPrintf(CTDL_ALERT, "Thread system, Can't create thread: %s\n",
                        strerror(ret));
-               if (this_thread->name)
-                       free (this_thread->name);
                pthread_mutex_destroy(&(this_thread->ThreadMutex));
                pthread_cond_destroy(&(this_thread->ThreadCond));
                pthread_mutex_destroy(&(this_thread->SleepMutex));
                pthread_cond_destroy(&(this_thread->SleepCond));
                pthread_attr_destroy(&this_thread->attr);
                free(this_thread);
-               if (sigtrick)
-                       ctdl_thread_internal_restore_signals(&old_signal_set);
+//             if (sigtrick)
+//                     ctdl_thread_internal_restore_signals(&old_signal_set);
                return NULL;
        }
        
-       if (sigtrick)
-               ctdl_thread_internal_restore_signals(&old_signal_set);
+//     if (sigtrick)
+//             ctdl_thread_internal_restore_signals(&old_signal_set);
        
        num_threads++;  // Increase the count of threads in the system.
        if(this_thread->flags & CTDLTHREAD_WORKER)
@@ -1884,7 +1881,7 @@ struct CtdlThreadNode *CtdlThreadCreate(char *name, long flags, void *(*thread_f
 /*
  * A warapper function for select so we can show a thread as blocked
  */
-int CtdlThreadSelect(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, const struct timeval *timeout, struct CtdlThreadNode *self)
+int CtdlThreadSelect(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout, struct CtdlThreadNode *self)
 {
        int ret;
        
@@ -1998,11 +1995,8 @@ void *worker_thread(void *arg) {
        int force_purge = 0;
        int m;
        
-       CT_PUSH();
-       
-       cdb_allocate_tsd();
 
-       while (!CtdlThreadCheckStop(CT)) {
+       while (!CtdlThreadCheckStop()) {
 
                /* make doubly sure we're not holding any stale db handles
                 * which might cause a deadlock.
@@ -2048,13 +2042,13 @@ do_select:      force_purge = 0;
                        }
                }
 
-               if (!CtdlThreadCheckStop(CT)) {
+               if (!CtdlThreadCheckStop()) {
                        tv.tv_sec = 1;          /* wake up every second if no input */
                        tv.tv_usec = 0;
                        retval = CtdlThreadSelect(highest + 1, &readfds, NULL, NULL, &tv, CT);
                }
 
-               if (CtdlThreadCheckStop(CT)) return(NULL);
+               if (CtdlThreadCheckStop()) return(NULL);
 
                /* Now figure out who made this select() unblock.
                 * First, check for an error or exit condition.
@@ -2068,14 +2062,14 @@ do_select:      force_purge = 0;
                        if (errno != EINTR) {
                                CtdlLogPrintf(CTDL_EMERG, "Exiting (%s)\n", strerror(errno));
                                CtdlThreadStopAll();
-                       } else if (!CtdlThreadCheckStop(CT)) {
+                       } else if (!CtdlThreadCheckStop()) {
                                CtdlLogPrintf(CTDL_DEBUG, "Un handled select failure.\n");
                                goto do_select;
                        }
                }
-//             else if(retval == 0) {
-//                     goto SKIP_SELECT;
-//             }
+               else if(retval == 0) {
+                       goto SKIP_SELECT;
+               }
                /* Next, check to see if it's a new client connecting
                 * on a master socket.
                 */