forked from hailinzeng/Programming-POSIX-Threads
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backoff.c
199 lines (188 loc) · 6.11 KB
/
backoff.c
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
* backoff.c
*
* Demonstrate deadlock avoidance using "mutex backoff".
*
* Special notes: On a Solaris 2.5 uniprocessor, this test will
* not produce interleaved output unless extra LWPs are created
* by calling thr_setconcurrency(), because threads are not
* timesliced.
*/
#include <pthread.h>
#include <sched.h>
#include "errors.h"
#define ITERATIONS 10
/*
* Initialize a static array of 3 mutexes.
*/
pthread_mutex_t mutex[3] = {
PTHREAD_MUTEX_INITIALIZER,
PTHREAD_MUTEX_INITIALIZER,
PTHREAD_MUTEX_INITIALIZER
};
int backoff = 1; /* Whether to backoff or deadlock */
int yield_flag = 0; /* 0: no yield, >0: yield, <0: sleep */
/*
* This is a thread start routine that locks all mutexes in
* order, to ensure a conflict with lock_reverse, which does the
* opposite.
*/
void *lock_forward (void *arg)
{
int i, iterate, backoffs;
int status;
for (iterate = 0; iterate < ITERATIONS; iterate++) {
backoffs = 0;
for (i = 0; i < 3; i++) {
if (i == 0) {
status = pthread_mutex_lock (&mutex[i]);
if (status != 0)
err_abort (status, "First lock");
} else {
if (backoff)
status = pthread_mutex_trylock (&mutex[i]);
else
status = pthread_mutex_lock (&mutex[i]);
if (status == EBUSY) {
backoffs++;
DPRINTF ((
" [forward locker backing off at %d]\n",
i));
for (; i >= 0; i--) {
status = pthread_mutex_unlock (&mutex[i]);
if (status != 0)
err_abort (status, "Backoff");
}
} else {
if (status != 0)
err_abort (status, "Lock mutex");
DPRINTF ((" forward locker got %d\n", i));
}
}
/*
* Yield processor, if needed to be sure locks get
* interleaved on a uniprocessor.
*/
if (yield_flag) {
if (yield_flag > 0)
sched_yield ();
else
sleep (1);
}
}
/*
* Report that we got 'em, and unlock to try again.
*/
printf (
"lock forward got all locks, %d backoffs\n", backoffs);
pthread_mutex_unlock (&mutex[2]);
pthread_mutex_unlock (&mutex[1]);
pthread_mutex_unlock (&mutex[0]);
sched_yield ();
}
return NULL;
}
/*
* This is a thread start routine that locks all mutexes in
* reverse order, to ensure a conflict with lock_forward, which
* does the opposite.
*/
void *lock_backward (void *arg)
{
int i, iterate, backoffs;
int status;
for (iterate = 0; iterate < ITERATIONS; iterate++) {
backoffs = 0;
for (i = 2; i >= 0; i--) {
if (i == 2) {
status = pthread_mutex_lock (&mutex[i]);
if (status != 0)
err_abort (status, "First lock");
} else {
if (backoff)
status = pthread_mutex_trylock (&mutex[i]);
else
status = pthread_mutex_lock (&mutex[i]);
if (status == EBUSY) {
backoffs++;
DPRINTF ((
" [backward locker backing off at %d]\n",
i));
for (; i < 3; i++) {
status = pthread_mutex_unlock (&mutex[i]);
if (status != 0)
err_abort (status, "Backoff");
}
} else {
if (status != 0)
err_abort (status, "Lock mutex");
DPRINTF ((" backward locker got %d\n", i));
}
}
/*
* Yield processor, if needed to be sure locks get
* interleaved on a uniprocessor.
*/
if (yield_flag) {
if (yield_flag > 0)
sched_yield ();
else
sleep (1);
}
}
/*
* Report that we got 'em, and unlock to try again.
*/
printf (
"lock backward got all locks, %d backoffs\n", backoffs);
pthread_mutex_unlock (&mutex[0]);
pthread_mutex_unlock (&mutex[1]);
pthread_mutex_unlock (&mutex[2]);
sched_yield ();
}
return NULL;
}
int main (int argc, char *argv[])
{
pthread_t forward, backward;
int status;
#ifdef sun
/*
* On Solaris 2.5, threads are not timesliced. To ensure
* that our threads can run concurrently, we need to
* increase the concurrency level.
*/
DPRINTF (("Setting concurrency level to 2\n"));
thr_setconcurrency (2);
#endif
/*
* If the first argument is absent, or nonzero, a backoff
* algorithm will be used to avoid deadlock. If the first
* argument is zero, the program will deadlock on a lock
* "collision."
*/
if (argc > 1)
backoff = atoi (argv[1]);
/*
* If the second argument is absent, or zero, the two
* threads run "at speed." On some systems, especially
* uniprocessors, one thread may complete before the other
* has a chance to run, and you won't see a deadlock or
* backoffs. In that case, try running with the argument set
* to a positive number to cause the threads to call
* sched_yield() at each lock; or, to make it even more
* obvious, set to a negative number to cause the threads to
* call sleep(1) instead.
*/
if (argc > 2)
yield_flag = atoi (argv[2]);
status = pthread_create (
&forward, NULL, lock_forward, NULL);
if (status != 0)
err_abort (status, "Create forward");
status = pthread_create (
&backward, NULL, lock_backward, NULL);
if (status != 0)
err_abort (status, "Create backward");
pthread_exit (NULL);
}