You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
5.1 KiB

  1. /* Copyright (C) 2008 Sun Microsystems, Inc
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; version 2 of the License.
  5. This program is distributed in the hope that it will be useful,
  6. but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8. GNU General Public License for more details.
  9. You should have received a copy of the GNU General Public License
  10. along with this program; if not, write to the Free Software
  11. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
  12. /* Testing of deadlock detector */
  13. #include <my_global.h>
  14. #include <mysys_priv.h>
  15. int main(int argc __attribute__((unused)), char** argv)
  16. {
  17. pthread_mutex_t LOCK_A, LOCK_B, LOCK_C, LOCK_D, LOCK_E, LOCK_F, LOCK_G;
  18. pthread_mutex_t LOCK_H, LOCK_I;
  19. MY_INIT(argv[0]);
  20. DBUG_ENTER("main");
  21. DBUG_PUSH("d:t:O,/tmp/trace");
  22. printf("This program is testing the mutex deadlock detection.\n"
  23. "It should print out different failures of wrong mutex usage"
  24. "on stderr\n\n");
  25. safe_mutex_deadlock_detector= 1;
  26. pthread_mutex_init(&LOCK_A, MY_MUTEX_INIT_FAST);
  27. pthread_mutex_init(&LOCK_B, MY_MUTEX_INIT_FAST);
  28. pthread_mutex_init(&LOCK_C, MY_MUTEX_INIT_FAST);
  29. pthread_mutex_init(&LOCK_D, MY_MUTEX_INIT_FAST);
  30. pthread_mutex_init(&LOCK_E, MY_MUTEX_INIT_FAST);
  31. pthread_mutex_init(&LOCK_F, MY_MUTEX_INIT_FAST);
  32. pthread_mutex_init(&LOCK_G, MY_MUTEX_INIT_FAST);
  33. pthread_mutex_init(&LOCK_H, MY_MUTEX_INIT_FAST);
  34. pthread_mutex_init(&LOCK_I, MY_MUTEX_INIT_FAST);
  35. printf("Testing A->B and B->A\n");
  36. fflush(stdout);
  37. pthread_mutex_lock(&LOCK_A);
  38. pthread_mutex_lock(&LOCK_B);
  39. pthread_mutex_unlock(&LOCK_A);
  40. pthread_mutex_unlock(&LOCK_B);
  41. /* Test different (wrong) lock order */
  42. pthread_mutex_lock(&LOCK_B);
  43. pthread_mutex_lock(&LOCK_A); /* Should give warning */
  44. pthread_mutex_unlock(&LOCK_A);
  45. pthread_mutex_unlock(&LOCK_B);
  46. /* Check that we don't get another warning for same lock */
  47. printf("Testing A->B and B->A again (should not give a warning)\n");
  48. pthread_mutex_lock(&LOCK_B);
  49. pthread_mutex_lock(&LOCK_A);
  50. pthread_mutex_unlock(&LOCK_A);
  51. pthread_mutex_unlock(&LOCK_B);
  52. /*
  53. Test of ring with many mutex
  54. We also unlock mutex in different orders to get the unlock code properly
  55. tested.
  56. */
  57. printf("Testing A->C and C->D and D->A\n");
  58. pthread_mutex_lock(&LOCK_A);
  59. pthread_mutex_lock(&LOCK_C);
  60. pthread_mutex_unlock(&LOCK_A);
  61. pthread_mutex_unlock(&LOCK_C);
  62. pthread_mutex_lock(&LOCK_C);
  63. pthread_mutex_lock(&LOCK_D);
  64. pthread_mutex_unlock(&LOCK_D);
  65. pthread_mutex_unlock(&LOCK_C);
  66. pthread_mutex_lock(&LOCK_D);
  67. pthread_mutex_lock(&LOCK_A); /* Should give warning */
  68. pthread_mutex_unlock(&LOCK_A);
  69. pthread_mutex_unlock(&LOCK_D);
  70. printf("Testing E -> F ; H -> I ; F -> H ; H -> I -> E\n");
  71. fflush(stdout);
  72. pthread_mutex_lock(&LOCK_E);
  73. pthread_mutex_lock(&LOCK_F);
  74. pthread_mutex_unlock(&LOCK_E);
  75. pthread_mutex_unlock(&LOCK_F);
  76. pthread_mutex_lock(&LOCK_H);
  77. pthread_mutex_lock(&LOCK_I);
  78. pthread_mutex_unlock(&LOCK_I);
  79. pthread_mutex_unlock(&LOCK_H);
  80. pthread_mutex_lock(&LOCK_F);
  81. pthread_mutex_lock(&LOCK_H);
  82. pthread_mutex_unlock(&LOCK_H);
  83. pthread_mutex_unlock(&LOCK_F);
  84. pthread_mutex_lock(&LOCK_H);
  85. pthread_mutex_lock(&LOCK_I);
  86. pthread_mutex_lock(&LOCK_E); /* Should give warning */
  87. pthread_mutex_unlock(&LOCK_E);
  88. pthread_mutex_unlock(&LOCK_I);
  89. pthread_mutex_unlock(&LOCK_H);
  90. printf("\nFollowing shouldn't give any warnings\n");
  91. printf("Testing A->B and B->A without deadlock detection\n");
  92. fflush(stdout);
  93. /* Reinitialize mutex to get rid of old wrong usage markers */
  94. pthread_mutex_destroy(&LOCK_A);
  95. pthread_mutex_destroy(&LOCK_B);
  96. pthread_mutex_init(&LOCK_A, MY_MUTEX_INIT_FAST);
  97. pthread_mutex_init(&LOCK_B, MY_MUTEX_INIT_FAST);
  98. /* Start testing */
  99. my_pthread_mutex_lock(&LOCK_A, MYF(MYF_NO_DEADLOCK_DETECTION));
  100. pthread_mutex_lock(&LOCK_B);
  101. pthread_mutex_unlock(&LOCK_A);
  102. pthread_mutex_unlock(&LOCK_B);
  103. pthread_mutex_lock(&LOCK_A);
  104. my_pthread_mutex_lock(&LOCK_B, MYF(MYF_NO_DEADLOCK_DETECTION));
  105. pthread_mutex_unlock(&LOCK_A);
  106. pthread_mutex_unlock(&LOCK_B);
  107. printf("Testing A -> C ; B -> C ; A->B\n");
  108. fflush(stdout);
  109. pthread_mutex_lock(&LOCK_A);
  110. pthread_mutex_lock(&LOCK_C);
  111. pthread_mutex_unlock(&LOCK_C);
  112. pthread_mutex_unlock(&LOCK_A);
  113. pthread_mutex_lock(&LOCK_B);
  114. pthread_mutex_lock(&LOCK_C);
  115. pthread_mutex_unlock(&LOCK_C);
  116. pthread_mutex_unlock(&LOCK_B);
  117. pthread_mutex_lock(&LOCK_A);
  118. pthread_mutex_lock(&LOCK_B);
  119. pthread_mutex_unlock(&LOCK_B);
  120. pthread_mutex_unlock(&LOCK_A);
  121. /* Cleanup */
  122. pthread_mutex_destroy(&LOCK_A);
  123. pthread_mutex_destroy(&LOCK_B);
  124. pthread_mutex_destroy(&LOCK_C);
  125. pthread_mutex_destroy(&LOCK_D);
  126. pthread_mutex_destroy(&LOCK_E);
  127. pthread_mutex_destroy(&LOCK_F);
  128. pthread_mutex_destroy(&LOCK_G);
  129. pthread_mutex_destroy(&LOCK_H);
  130. pthread_mutex_destroy(&LOCK_I);
  131. my_end(MY_DONT_FREE_DBUG);
  132. exit(0);
  133. }