From 6d1e07322d92dc526c4b2242441296d287766d01 Mon Sep 17 00:00:00 2001
From: Sergey Vojtovich <svoj@sun.com>
Date: Thu, 1 Apr 2010 15:21:13 +0400
Subject: [PATCH] Applying InnoDB snapshot

Detailed revision comments:

r6446 | marko | 2010-01-13 17:20:10 +0200 (Wed, 13 Jan 2010) | 3 lines
branches/zip: Treat mem_hash_mutex specially in mutex_free(),
and explicitly free mem_hash_mutex in mem_close().
This fixes the breakage of UNIV_MEM_DEBUG that was filed as Issue #434.
---
 storage/innodb_plugin/include/mem0dbg.h  |  7 +++++++
 storage/innodb_plugin/include/mem0dbg.ic |  3 ---
 storage/innodb_plugin/mem/mem0dbg.c      |  4 ++++
 storage/innodb_plugin/sync/sync0sync.c   | 19 ++++++++++++++++++-
 4 files changed, 29 insertions(+), 4 deletions(-)

diff --git a/storage/innodb_plugin/include/mem0dbg.h b/storage/innodb_plugin/include/mem0dbg.h
index a064af5c678..8ddf4a13cba 100644
--- a/storage/innodb_plugin/include/mem0dbg.h
+++ b/storage/innodb_plugin/include/mem0dbg.h
@@ -28,6 +28,13 @@ Created 6/9/1994 Heikki Tuuri
 check fields whose sizes are given below */
 
 #ifdef UNIV_MEM_DEBUG
+# ifndef UNIV_HOTBACKUP
+/* The mutex which protects in the debug version the hash table
+containing the list of live memory heaps, and also the global
+variables in mem0dbg.c. */
+extern mutex_t	mem_hash_mutex;
+# endif /* !UNIV_HOTBACKUP */
+
 #define MEM_FIELD_HEADER_SIZE	ut_calc_align(2 * sizeof(ulint),\
 						UNIV_MEM_ALIGNMENT)
 #define MEM_FIELD_TRAILER_SIZE	sizeof(ulint)
diff --git a/storage/innodb_plugin/include/mem0dbg.ic b/storage/innodb_plugin/include/mem0dbg.ic
index cb9245411dc..9c6e5a78263 100644
--- a/storage/innodb_plugin/include/mem0dbg.ic
+++ b/storage/innodb_plugin/include/mem0dbg.ic
@@ -25,9 +25,6 @@ Created 6/8/1994 Heikki Tuuri
 *************************************************************************/
 
 #ifdef UNIV_MEM_DEBUG
-# ifndef UNIV_HOTBACKUP
-extern mutex_t	mem_hash_mutex;
-# endif /* !UNIV_HOTBACKUP */
 extern ulint	mem_current_allocated_memory;
 
 /******************************************************************//**
diff --git a/storage/innodb_plugin/mem/mem0dbg.c b/storage/innodb_plugin/mem/mem0dbg.c
index 01eda20ec45..4973ead4213 100644
--- a/storage/innodb_plugin/mem/mem0dbg.c
+++ b/storage/innodb_plugin/mem/mem0dbg.c
@@ -180,6 +180,10 @@ mem_close(void)
 {
 	mem_pool_free(mem_comm_pool);
 	mem_comm_pool = NULL;
+#ifdef UNIV_MEM_DEBUG
+	mutex_free(&mem_hash_mutex);
+	mem_hash_initialized = FALSE;
+#endif /* UNIV_MEM_DEBUG */
 }
 #endif /* !UNIV_HOTBACKUP */
 
diff --git a/storage/innodb_plugin/sync/sync0sync.c b/storage/innodb_plugin/sync/sync0sync.c
index 569fc6328c4..f1527907ef6 100644
--- a/storage/innodb_plugin/sync/sync0sync.c
+++ b/storage/innodb_plugin/sync/sync0sync.c
@@ -315,6 +315,15 @@ mutex_free(
 	ut_a(mutex_get_lock_word(mutex) == 0);
 	ut_a(mutex_get_waiters(mutex) == 0);
 
+#ifdef UNIV_MEM_DEBUG
+	if (mutex == &mem_hash_mutex) {
+		ut_ad(UT_LIST_GET_LEN(mutex_list) == 1);
+		ut_ad(UT_LIST_GET_FIRST(mutex_list) == &mem_hash_mutex);
+		UT_LIST_REMOVE(list, mutex_list, mutex);
+		goto func_exit;
+	}
+#endif /* UNIV_MEM_DEBUG */
+
 	if (mutex != &mutex_list_mutex
 #ifdef UNIV_SYNC_DEBUG
 	    && mutex != &sync_thread_mutex
@@ -336,7 +345,9 @@ mutex_free(
 	}
 
 	os_event_free(mutex->event);
-
+#ifdef UNIV_MEM_DEBUG
+func_exit:
+#endif /* UNIV_MEM_DEBUG */
 #if !defined(HAVE_ATOMIC_BUILTINS)
 	os_fast_mutex_free(&(mutex->os_fast_mutex));
 #endif
@@ -1370,6 +1381,12 @@ sync_close(void)
 	mutex = UT_LIST_GET_FIRST(mutex_list);
 
 	while (mutex) {
+#ifdef UNIV_MEM_DEBUG
+		if (mutex == &mem_hash_mutex) {
+			mutex = UT_LIST_GET_NEXT(list, mutex);
+			continue;
+		}
+#endif /* UNIV_MEM_DEBUG */
 		mutex_free(mutex);
 		mutex = UT_LIST_GET_FIRST(mutex_list);
 	}
-- 
2.30.9