From 54cffbe192aaf287bcef297991d10de59399163f Mon Sep 17 00:00:00 2001
From: Guilhem Bichot <guilhem@mysql.com>
Date: Thu, 12 Feb 2009 16:27:33 +0100
Subject: [PATCH] Fixing problems of previous 5.1-main->5.1-maria merge: -
 adding back Serg's "mtr --list-options" - safe_mutex deadlock detector
 started raising wrong deadlock warnings, fixed here by a backport from
 6.0-main.

include/my_pthread.h:
  Porting changes done to 6.0-main which satisfy the safe_mutex deadlock detector (those
  in 5.1-main don't), see chad@mysql.com-20090126155607-n0j3zbmgbfepnmmo for explanations
mysql-test/mysql-test-run.pl:
  adding back Serg's --list-options
mysys/my_init.c:
  Porting changes done to 6.0-main which satisfy the safe_mutex deadlock detector (those
  in 5.1-main don't), see chad@mysql.com-20090126155607-n0j3zbmgbfepnmmo for explanations
mysys/my_thr_init.c:
  Porting changes done to 6.0-main which satisfy the safe_mutex deadlock detector (those
  in 5.1-main don't), see chad@mysql.com-20090126155607-n0j3zbmgbfepnmmo for explanations
---
 include/my_pthread.h         |  1 +
 mysql-test/mysql-test-run.pl | 21 +++++++++++++++++++--
 mysys/my_init.c              | 10 +++++-----
 mysys/my_thr_init.c          | 32 ++++++++++++++++++++++++++++++++
 4 files changed, 57 insertions(+), 7 deletions(-)

diff --git a/include/my_pthread.h b/include/my_pthread.h
index 538457a523a..f7d81f95107 100644
--- a/include/my_pthread.h
+++ b/include/my_pthread.h
@@ -671,6 +671,7 @@ extern pthread_mutexattr_t my_errorcheck_mutexattr;
 
 typedef ulong my_thread_id;
 
+extern void my_threadattr_global_init(void);
 extern my_bool my_thread_global_init(void);
 extern void my_thread_global_end(void);
 extern my_bool my_thread_init(void);
diff --git a/mysql-test/mysql-test-run.pl b/mysql-test/mysql-test-run.pl
index dbbc68527a7..f242df9991e 100755
--- a/mysql-test/mysql-test-run.pl
+++ b/mysql-test/mysql-test-run.pl
@@ -118,6 +118,7 @@ our $opt_vs_config = $ENV{'MTR_VS_CONFIG'};
 my $DEFAULT_SUITES= "main,binlog,federated,rpl,rpl_ndb,ndb,maria";
 
 our $opt_usage;
+our $opt_list_options;
 our $opt_suites;
 our $opt_suites_default= "main,backup,backup_engines,binlog,rpl,rpl_ndb,ndb"; # Default suites to run
 our $opt_script_debug= 0;  # Script debugging, enable with --script-debug
@@ -770,7 +771,7 @@ sub command_line_setup {
   # Read the command line options
   # Note: Keep list, and the order, in sync with usage at end of this file
   Getopt::Long::Configure("pass_through");
-  GetOptions(
+  my %options=(
              # Control what engine/variation to run
              'embedded-server'          => \$opt_embedded_server,
              'ps-protocol'              => \$opt_ps_protocol,
@@ -891,9 +892,13 @@ sub command_line_setup {
 	     'timediff'                 => \&report_option,
 
              'help|h'                   => \$opt_usage,
-            ) or usage("Can't read options");
+             'list-options'             => \$opt_list_options,
+            );
+
+  GetOptions(%options) or usage("Can't read options");
 
   usage("") if $opt_usage;
+  list_options(\%options) if $opt_list_options;
 
   # --------------------------------------------------------------------------
   # Setup verbosity
@@ -5125,3 +5130,15 @@ HERE
 
 }
 
+
+sub list_options ($) {
+  my $hash= shift;
+
+  for (keys %$hash) {
+    s/(=.*|!)$//;
+    s/\|/\n--/g;
+    print "--$_\n";
+  }
+
+  exit(1);
+}
diff --git a/mysys/my_init.c b/mysys/my_init.c
index 2401671687b..e05536bf3ea 100644
--- a/mysys/my_init.c
+++ b/mysys/my_init.c
@@ -82,20 +82,20 @@ my_bool my_init(void)
     my_progname_short= my_progname + dirname_length(my_progname);
 
 #if defined(THREAD)
-  if (my_thread_global_init())
-    return 1;
+  (void) my_threadattr_global_init();
 #  if defined(SAFE_MUTEX)
   safe_mutex_global_init();		/* Must be called early */
-#  endif
-#endif
-#if defined(THREAD) && defined(MY_PTHREAD_FASTMUTEX) && !defined(SAFE_MUTEX)
+#  elif defined(MY_PTHREAD_FASTMUTEX)
   fastmutex_global_init();              /* Must be called early */
+#  endif
 #endif
   netware_init();
 #ifdef THREAD
 #if defined(HAVE_PTHREAD_INIT)
   pthread_init();			/* Must be called before DBUG_ENTER */
 #endif
+  if (my_thread_global_init())
+    return 1;
 #if !defined( __WIN__) && !defined(__NETWARE__)
   sigfillset(&my_signals);		/* signals blocked by mf_brkhant */
 #endif
diff --git a/mysys/my_thr_init.c b/mysys/my_thr_init.c
index 3f4c4a4d638..4617d0e2277 100644
--- a/mysys/my_thr_init.c
+++ b/mysys/my_thr_init.c
@@ -65,6 +65,38 @@ nptl_pthread_exit_hack_handler(void *arg __attribute((unused)))
 #endif /* TARGET_OS_LINUX */
 
 
+
+/**
+  Initialize thread attributes.
+*/
+
+void my_threadattr_global_init(void)
+{
+#ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
+  /*
+    Set mutex type to "fast" a.k.a "adaptive"
+
+    In this case the thread may steal the mutex from some other thread
+    that is waiting for the same mutex.  This will save us some
+    context switches but may cause a thread to 'starve forever' while
+    waiting for the mutex (not likely if the code within the mutex is
+    short).
+  */
+  pthread_mutexattr_init(&my_fast_mutexattr);  /* ?= MY_MUTEX_INIT_FAST */
+  pthread_mutexattr_settype(&my_fast_mutexattr,
+                            PTHREAD_MUTEX_ADAPTIVE_NP);
+#endif
+#ifdef PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
+  /*
+    Set mutex type to "errorcheck"
+  */
+  pthread_mutexattr_init(&my_errorcheck_mutexattr);
+  pthread_mutexattr_settype(&my_errorcheck_mutexattr,
+                            PTHREAD_MUTEX_ERRORCHECK);
+#endif
+}
+
+
 static uint get_thread_lib(void);
 
 /*
-- 
2.30.9