From 130cbc32499ade9235873bc5d575816f80636916 Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Wed, 11 May 2011 23:15:05 -0600 Subject: Fix crash when deleting unused prtree caches. --- libdimension/prtree.c | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/libdimension/prtree.c b/libdimension/prtree.c index b8e2b4d..e73ea16 100644 --- a/libdimension/prtree.c +++ b/libdimension/prtree.c @@ -536,15 +536,6 @@ static pthread_key_t dmnsn_prtree_caches; /** Initialize the thread-specific pointer exactly once. */ static pthread_once_t dmnsn_prtree_caches_once = PTHREAD_ONCE_INIT; -/** Needed because pthreads doesn't destroy data from the main thread unless - it exits with pthread_exit(). */ -static void __attribute__((destructor)) -dmnsn_delete_main_prtree_caches(void) -{ - dmnsn_delete_array(pthread_getspecific(dmnsn_prtree_caches)); - pthread_key_delete(dmnsn_prtree_caches); -} - static void dmnsn_delete_prtree_caches(void *caches) { @@ -560,16 +551,30 @@ dmnsn_initialize_prtree_caches(void) } } -static dmnsn_intersection_cache * -dmnsn_get_intersection_cache(size_t id) +static dmnsn_array * +dmnsn_get_prtree_caches(void) { if (pthread_once(&dmnsn_prtree_caches_once, dmnsn_initialize_prtree_caches) != 0) { dmnsn_error("pthread_once() failed."); } + return pthread_getspecific(dmnsn_prtree_caches); +} - dmnsn_array *caches = pthread_getspecific(dmnsn_prtree_caches); +/** Needed because pthreads doesn't destroy data from the main thread unless + it exits with pthread_exit(). */ +static void __attribute__((destructor)) +dmnsn_delete_main_prtree_caches(void) +{ + dmnsn_delete_array(dmnsn_get_prtree_caches()); + pthread_key_delete(dmnsn_prtree_caches); +} + +static dmnsn_intersection_cache * +dmnsn_get_intersection_cache(size_t id) +{ + dmnsn_array *caches = dmnsn_get_prtree_caches(); if (!caches) { caches = dmnsn_new_array(sizeof(dmnsn_intersection_cache)); if (pthread_setspecific(dmnsn_prtree_caches, caches) != 0) { -- cgit v1.2.3