Current-Users archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: threaded programs report bogus process size ?



In article <20090930142747.8ED9F63B140%mail.netbsd.org@localhost>,
Mindaugas Rasiukevicius  <rmind%netbsd.org@localhost> wrote:
>christos%astron.com@localhost (Christos Zoulas) wrote:
>> >To correct this the reclaim operation should loop over the queue
>> >and try to reclaim any of the dead threads.
>> 
>> This seems to fix it.
>> 
>
>Patch is wrong.  Apart from unnecessary re-lock in the loop and lock leak
>after - consider the case when 'first_thread' after re-insertion into the
>queue gets taken out by other pthread_create().  Infinite loop.

Here's a simpler one.

christos

Index: pthread.c
===================================================================
RCS file: /cvsroot/src/lib/libpthread/pthread.c,v
retrieving revision 1.112
diff -u -u -r1.112 pthread.c
--- pthread.c   2 Jul 2009 09:59:00 -0000       1.112
+++ pthread.c   30 Sep 2009 18:51:25 -0000
@@ -353,23 +353,18 @@
         */
        if (!PTQ_EMPTY(&pthread__deadqueue)) {
                pthread_mutex_lock(&pthread__deadqueue_lock);
-               newthread = PTQ_FIRST(&pthread__deadqueue);
-               if (newthread != NULL) {
-                       PTQ_REMOVE(&pthread__deadqueue, newthread, pt_deadq);
-                       pthread_mutex_unlock(&pthread__deadqueue_lock);
+               PTQ_FOREACH_REVERSE(newthread, &pthread__deadqueue,
+                   pthread_queue_struct_t, pt_deadq) {
                        /* Still running? */
-                       if (newthread->pt_lwpctl->lc_curcpu !=
-                           LWPCTL_CPU_EXITED &&
-                           (_lwp_kill(newthread->pt_lid, 0) == 0 ||
-                           errno != ESRCH)) {
-                               pthread_mutex_lock(&pthread__deadqueue_lock);
-                               PTQ_INSERT_TAIL(&pthread__deadqueue,
-                                   newthread, pt_deadq);
-                               pthread_mutex_unlock(&pthread__deadqueue_lock);
-                               newthread = NULL;
-                       }
-               } else
-                       pthread_mutex_unlock(&pthread__deadqueue_lock);
+                       if (newthread->pt_lwpctl->lc_curcpu ==
+                           LWPCTL_CPU_EXITED ||
+                           (_lwp_kill(newthread->pt_lid, 0) == -1 &&
+                           errno == ESRCH))
+                               break;
+               }
+               if (newthread)
+                       PTQ_REMOVE(&pthread__deadqueue, newthread, pt_deadq);
+               pthread_mutex_unlock(&pthread__deadqueue_lock);
        }
 
        /*



Home | Main Index | Thread Index | Old Index