[PATCH] libthr - pthread_set_name_np crash

Ed Maste ed.maste at gmail.com
Wed Apr 4 21:36:04 UTC 2007


It seems there is a bug in pthread_set_name_np for the thread != curthread
case.  That is, pthread_set_name_np called from outside of the thread
being named, as in the following example:

        pthread_create(&pt, 0, t_spin, (void *)1);
        [...]
        pthread_set_name_np(pt, "t1");

The bug is a race condition that leads to thread->locklevel being
incorrect.  Based on the other cases of locking a not-curthread thread,
I think the patch below is the fix.  Can a libthr expert comment?

Thanks,
Ed

Index: thr_info.c
===================================================================
RCS file: /usr/cvs/src/lib/libthr/thread/thr_info.c,v
retrieving revision 1.9
diff -u -r1.9 thr_info.c
--- thr_info.c  12 Jan 2007 07:26:20 -0000      1.9
+++ thr_info.c  23 Mar 2007 14:56:13 -0000
@@ -52,12 +52,12 @@
                        ret = errno;
        } else {
                if (_thr_ref_add(curthread, thread, 0) == 0) {
-                       THR_LOCK(thread);
+                       THR_THREAD_LOCK(curthread, thread);
                        if (thread->state != PS_DEAD) {
                                if (thr_set_name(thread->tid, name))
                                        ret = errno;
                        }
-                       THR_UNLOCK(thread);
+                       THR_THREAD_UNLOCK(curthread, thread);
                        _thr_ref_delete(curthread, thread);
                } else {
                        ret = ESRCH;


More information about the freebsd-threads mailing list