On Mon, Jun 27, 2011, Henry Precheur wrote:
> According to pthread_key_create(3): Upon key creation, the value NULL is
> associated with the new key in all active threads.
> 
> 
> When pthread_key_create reuse an existing key, the old value is still
> here, which can create problems. The following program reproduces the
> problem:

Good catch.  Here's a fix.

Index: uthread/uthread_spec.c
===================================================================
RCS file: /home/tedu/cvs/src/lib/libpthread/uthread/uthread_spec.c,v
retrieving revision 1.9
diff -u -r1.9 uthread_spec.c
--- uthread/uthread_spec.c      20 Jul 2007 22:34:40 -0000      1.9
+++ uthread/uthread_spec.c      2 Jul 2011 14:57:41 -0000
@@ -78,6 +78,7 @@
 pthread_key_delete(pthread_key_t key)
 {
        int ret = 0;
+       pthread_t pthread;
 
        if (key < PTHREAD_KEYS_MAX) {
                /* Lock the key table entry: */
@@ -88,8 +89,19 @@
                else
                        ret = EINVAL;
 
+               _thread_kern_sig_defer();
+               TAILQ_FOREACH(pthread, &_thread_list, tle) {
+                       if (pthread->specific_data[key]) {
+                               pthread->specific_data[key] = NULL;
+                               pthread->specific_data_count--;
+                       }
+
+               }
+               _thread_kern_sig_undefer();
+
                /* Unlock the key table entry: */
                _SPINUNLOCK(&key_table[key].lock);
+
        } else
                ret = EINVAL;
        return (ret);

Reply via email to