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