All pastes #1169665 Raw Edit

32220.patch

public text v1 · immutable
#1169665 ·published 2008-08-13 12:55 UTC
rendered paste body
Index: TSRM.c
===================================================================
RCS file: /repository/TSRM/TSRM.c,v
retrieving revision 1.63
diff -u -r1.63 TSRM.c
--- TSRM.c	21 Feb 2005 10:05:07 -0000	1.63
+++ TSRM.c	7 Mar 2005 13:40:44 -0000
@@ -149,6 +149,17 @@
 TSRM_API void tsrm_shutdown(void)
 {
 	int i;
+#if defined(GNUPTH)
+	pth_cleanup_pop(0);
+#elif defined(PTHREADS)
+	pthread_cleanup_pop(0);
+#elif defined(TSRM_ST)
+	//Not sure about the thread cleanup_pop routine
+#elif defined(TSRM_WIN32)
+	//Not sure about the thread cleanup_pop routine
+#elif defined(BETHREADS)
+	//Not sure about the thread cleanup_pop routine
+#endif
 
 	if (tsrm_tls_table) {
 		for (i=0; i<tsrm_tls_table_size; i++) {
@@ -265,12 +276,18 @@
 #if defined(PTHREADS)
 	/* Set thread local storage to this new thread resources structure */
 	pthread_setspecific(tls_key, (void *) *thread_resources_ptr);
+	pthread_cleanup_push(ts_free_thread, NULL);
+#elif defined(GNUPTH)
+	pth_cleanup_push(ts_free_thread, NULL);
 #elif defined(TSRM_ST)
 	st_thread_setspecific(tls_key, (void *) *thread_resources_ptr);
+	//Not sure about the thread cleanup_push routine
 #elif defined(TSRM_WIN32)
 	TlsSetValue(tls_key, (void *) *thread_resources_ptr);
+	//Not sure about the thread cleanup_push routine
 #elif defined(BETHREADS)
 	tls_set(tls_key, (void*) *thread_resources_ptr);
+	//Not sure about the thread cleanup_push routine
 #endif
 
 	if (tsrm_new_thread_begin_handler) {
@@ -381,51 +398,50 @@
 
 
 /* frees all resources allocated for the current thread */
-void ts_free_thread(void)
+TSRM_API void ts_free_thread(void)
 {
-	tsrm_tls_entry *thread_resources;
-	int i;
-	THREAD_T thread_id = tsrm_thread_id();
-	int hash_value;
-	tsrm_tls_entry *last=NULL;
-
+	int j=0;
+	int hash_value=0;
+	tsrm_tls_entry *thread_resources, *tmp_resource, *prev_entry=NULL;
+
+	thread_resources = pthread_getspecific(tls_key);
+	for (j=0; j<thread_resources->count; j++) {
+		if (thread_resources->storage[j]) {
+			if (resource_types_table && !resource_types_table[j].done && resource_types_table[j].dtor) {
+				resource_types_table[j].dtor(thread_resources->storage[j], &thread_resources->storage);
+			}
+			free(thread_resources->storage[j]);
+		}
+	}
 	tsrm_mutex_lock(tsmm_mutex);
-	hash_value = THREAD_HASH_OF(thread_id, tsrm_tls_table_size);
-	thread_resources = tsrm_tls_table[hash_value];
-
-	while (thread_resources) {
-		if (thread_resources->thread_id == thread_id) {
-			for (i=0; i<thread_resources->count; i++) {
-				if (resource_types_table[i].dtor) {
-					resource_types_table[i].dtor(thread_resources->storage[i], &thread_resources->storage);
+	if (tsrm_tls_table) {
+		hash_value = THREAD_HASH_OF(tsrm_thread_id(), tsrm_tls_table_size);
+		tmp_resource = tsrm_tls_table[hash_value];
+		while(tmp_resource) {
+			if(tmp_resource->thread_id == tsrm_thread_id()) {
+				if(prev_entry) {
+					prev_entry->next = tmp_resource->next;
+				} else {
+					tsrm_tls_table[hash_value] = tmp_resource->next;
 				}
+				break;
 			}
-			for (i=0; i<thread_resources->count; i++) {
-				free(thread_resources->storage[i]);
-			}
-			free(thread_resources->storage);
-			if (last) {
-				last->next = thread_resources->next;
-			} else {
-				tsrm_tls_table[hash_value] = thread_resources->next;
-			}
-#if defined(PTHREADS)
-			pthread_setspecific(tls_key, 0);
-#elif defined(TSRM_WIN32)
-			TlsSetValue(tls_key, 0);
-#endif
-			free(thread_resources);
-			break;
-		}
-		if (thread_resources->next) {
-			last = thread_resources;
+			prev_entry = tmp_resource;
+			tmp_resource = tmp_resource->next;
 		}
-		thread_resources = thread_resources->next;
 	}
 	tsrm_mutex_unlock(tsmm_mutex);
+	free(thread_resources->storage);
+	thread_resources->next=NULL;
+	free(thread_resources);
+	thread_resources = NULL;
+#if defined(PTHREADS)
+	pthread_setspecific(tls_key, 0);
+#elif defined(TSRM_WIN32)
+	TlsSetValue(tls_key, 0);
+#endif
 }
 
-
 /* deallocates all occurrences of a given id */
 void ts_free_id(ts_rsrc_id id)
 {