To lock or not to lock before signalling a condition?
authorDave West <davew@uncensored.citadel.org>
Fri, 30 Nov 2007 17:19:24 +0000 (17:19 +0000)
committerDave West <davew@uncensored.citadel.org>
Fri, 30 Nov 2007 17:19:24 +0000 (17:19 +0000)
Some docs imply one should lock.
Some docs say to release all locks before signalling the condition.

citadel/sysdep.c

index c77c200ac98bd5cbcc4bcb20ff54fd122ee3715d..651912dde1f9a032bb4e928e422b216f67cc89da 100644 (file)
@@ -1188,6 +1188,9 @@ void ctdl_thread_internal_change_state (struct CtdlThreadNode *this_thread, enum
  */
 void CtdlThreadStopAll(void)
 {
+       //FIXME: The signalling of the condition should not be in the critical_section
+       // We need to build a list of threads we are going to signal and then signal them afterwards
+       
        struct CtdlThreadNode *this_thread;
        
        begin_critical_section(S_THREAD_LIST);
@@ -1197,7 +1200,9 @@ void CtdlThreadStopAll(void)
                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_mutex_unlock(&this_thread->ThreadMutex);
                        CtdlLogPrintf(CTDL_DEBUG, "Thread system stopping thread \"%s\" (%ld).\n", this_thread->name, this_thread->tid);
                }
                this_thread = this_thread->next;
@@ -1220,8 +1225,11 @@ void CtdlThreadGC(void)
        while(this_thread)
        {
                if (!this_thread->thread_func)
+               {
+//                     pthread_mutex_lock(&this_thread->ThreadMutex);
                        pthread_cond_signal(&this_thread->ThreadCond);
-                       
+//                     pthread_mutex_unlock(&this_thread->ThreadMutex);
+               }
                this_thread = this_thread->next;
        }
        end_critical_section(S_THREAD_LIST);
@@ -1371,7 +1379,9 @@ void CtdlThreadStop(struct CtdlThreadNode *thread)
                
        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_mutex_unlock(&this_thread->ThreadMutex);
        end_critical_section(S_THREAD_LIST);
 }