This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
annotate S_ithread_create create_destruct_mutex use
authorJarkko Hietaniemi <jhi@iki.fi>
Wed, 18 Nov 2015 03:15:09 +0000 (22:15 -0500)
committerJarkko Hietaniemi <jhi@iki.fi>
Mon, 23 Nov 2015 11:55:12 +0000 (06:55 -0500)
If the pool is passed in, and the create_destruct_mutex unlocked also
on success (so that all the unlocks happen in S_ithread_create()),
the create_destruct_mutex use can be annotated.

threads.xs:757:9: warning: releasing mutex 'my_poolp->create_destruct_mutex' th
at was not held [-Wthread-safety-analysis]
        MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);

threads.xs:1000:9: warning: releasing mutex 'my_poolp->create_destruct_mutex' t
hat was not held [-Wthread-safety-analysis]
        MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);

threads.c:1319:1: warning: mutex 'my_poolp->create_destruct_mutex' is not held on every path through here [-Wthread-safety-analysis]
}
^

dist/threads/threads.xs

index 6c337ac..73f704b 100644 (file)
@@ -717,17 +717,20 @@ S_SV_to_ithread(pTHX_ SV *sv)
 
 /* threads->create()
  * Called in context of parent thread.
- * Called with MY_POOL.create_destruct_mutex locked.  (Unlocked on error.)
+ * Called with my_pool->create_destruct_mutex locked.
+ * (Unlocked both on error and on success.)
  */
 STATIC ithread *
 S_ithread_create(
         PerlInterpreter *parent_perl,
+        my_pool_t *my_pool,
         SV       *init_function,
         IV        stack_size,
         int       gimme,
         int       exit_opt,
         int       params_start,
         int       num_params)
+  PERL_TSA_RELEASE(my_pool->create_destruct_mutex)
 {
     dTHXa(parent_perl);
     ithread     *thread;
@@ -743,18 +746,17 @@ S_ithread_create(
     int          rc_stack_size = 0;
     int          rc_thread_create = 0;
 #endif
-    dMY_POOL;
 
     /* Allocate thread structure in context of the main thread's interpreter */
     {
-        PERL_SET_CONTEXT(MY_POOL.main_thread.interp);
+        PERL_SET_CONTEXT(my_pool->main_thread.interp);
         thread = (ithread *)PerlMemShared_malloc(sizeof(ithread));
     }
     PERL_SET_CONTEXT(aTHX);
     if (!thread) {
         /* This lock was acquired in ithread_create()
          * prior to calling S_ithread_create(). */
-        MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
+        MUTEX_UNLOCK(&my_pool->create_destruct_mutex);
         {
           int fd = PerlIO_fileno(Perl_error_log);
           if (fd >= 0) {
@@ -767,11 +769,11 @@ S_ithread_create(
     Zero(thread, 1, ithread);
 
     /* Add to threads list */
-    thread->next = &MY_POOL.main_thread;
-    thread->prev = MY_POOL.main_thread.prev;
-    MY_POOL.main_thread.prev = thread;
+    thread->next = &my_pool->main_thread;
+    thread->prev = my_pool->main_thread.prev;
+    my_pool->main_thread.prev = thread;
     thread->prev->next = thread;
-    MY_POOL.total_threads++;
+    my_pool->total_threads++;
 
     /* 1 ref to be held by the local var 'thread' in S_ithread_run().
      * 1 ref to be held by the threads object that we assume we will
@@ -787,7 +789,7 @@ S_ithread_create(
     MUTEX_INIT(&thread->mutex);
     MUTEX_LOCK(&thread->mutex); /* See S_ithread_run() for more detail. */
 
-    thread->tid = MY_POOL.tid_counter++;
+    thread->tid = my_pool->tid_counter++;
     thread->stack_size = S_good_stack_size(aTHX_ stack_size);
     thread->gimme = gimme;
     thread->state = exit_opt;
@@ -997,7 +999,7 @@ S_ithread_create(
         /* Must unlock mutex for destruct call */
         /* This lock was acquired in ithread_create()
          * prior to calling S_ithread_create(). */
-        MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
+        MUTEX_UNLOCK(&my_pool->create_destruct_mutex);
         thread->state |= PERL_ITHR_NONVIABLE;
         S_ithread_free(aTHX_ thread);   /* Releases MUTEX */
 #ifndef WIN32
@@ -1012,7 +1014,8 @@ S_ithread_create(
         return (NULL);
     }
 
-    MY_POOL.running_threads++;
+    my_pool->running_threads++;
+    MUTEX_UNLOCK(&my_pool->create_destruct_mutex);
     return (thread);
 }
 
@@ -1138,7 +1141,8 @@ ithread_create(...)
 
         /* Create thread */
         MUTEX_LOCK(&MY_POOL.create_destruct_mutex);
-        thread = S_ithread_create(aTHX_ function_to_call,
+        thread = S_ithread_create(aTHX_ &MY_POOL,
+                                        function_to_call,
                                         stack_size,
                                         context,
                                         exit_opt,
@@ -1148,7 +1152,6 @@ ithread_create(...)
             XSRETURN_UNDEF;     /* Mutex already unlocked */
         }
         ST(0) = sv_2mortal(S_ithread_to_SV(aTHX_ Nullsv, thread, classname, FALSE));
-        MUTEX_UNLOCK(&MY_POOL.create_destruct_mutex);
 
         /* Let thread run. */
         /* See S_ithread_run() for more detail. */