]> git.saurik.com Git - apple/libc.git/blobdiff - pthreads/pthread_tsd.c
Libc-763.13.tar.gz
[apple/libc.git] / pthreads / pthread_tsd.c
index 29e0efcb40057bb117af8bc38eeda5cd3eac0a7f..070fc2d64e8a2243027dcb8bb795daae34e8e0a3 100644 (file)
@@ -187,11 +187,10 @@ _pthread_tsd_cleanup(pthread_t self)
        int i, j;
        void *param;
 
+       /* destroy dynamic keys first */
        for (j = 0;  j < PTHREAD_DESTRUCTOR_ITERATIONS;  j++)
        {
-               /* The first slot is reserved for pthread_self() */
-
-               for (i = __pthread_tsd_first;  i <= __pthread_tsd_max;  i++)
+               for (i = __pthread_tsd_start;  i <= self->max_tsd_key;  i++)
                {
                        if (_pthread_keys[i].created && (param = self->tsd[i]))
                        {
@@ -202,7 +201,16 @@ _pthread_tsd_cleanup(pthread_t self)
                                }
                        }
                }
-               for (i = __pthread_tsd_start;  i <= self->max_tsd_key;  i++)
+       }
+
+       self->max_tsd_key = 0;
+
+       /* 
+        * The first slot is reserved for pthread_self() and there is no cleanup on it.
+        * Destroy rest of the static keys next only if any destructors registered.
+        */
+       for (j = 0;  j < PTHREAD_DESTRUCTOR_ITERATIONS;  j++) {
+               for (i = __pthread_tsd_first;  i <= __pthread_tsd_max;  i++)
                {
                        if (_pthread_keys[i].created && (param = self->tsd[i]))
                        {
@@ -214,7 +222,6 @@ _pthread_tsd_cleanup(pthread_t self)
                        }
                }
        }
-       self->max_tsd_key = 0;
 }
 
 int