| 1 |
|
| 2 |
|
| 3 |
deadlock_detection_test |
| 4 |
|
| 5 |
This example contains two deadlock tests, mutex and rwlock tests. |
| 6 |
% ./deadlock_detection_test -u |
| 7 |
./deadlock_detection_test: |
| 8 |
[-r test readers/writer locks] |
| 9 |
[-n <iterations>] |
| 10 |
[-h <remote host>] |
| 11 |
[-p <remote port>] |
| 12 |
[-i ignore deadlock] |
| 13 |
|
| 14 |
For both mutex and rwlock tests, -h and -p specify to use remote |
| 15 |
mutexes. -i specifies to ignore deadlock. -n is repetitions through |
| 16 |
the respective algorithms (default 100). Both tests also use Token |
| 17 |
Invariants to ensure correctness of the mutexes and readers/writer |
| 18 |
locks. |
| 19 |
|
| 20 |
------------------------------------------------------------ |
| 21 |
|
| 22 |
If you run ./deadlock_detection_test without -r, then the following |
| 23 |
mutex test is run. |
| 24 |
|
| 25 |
The mutex test spawns two threads which attempt to deadlock. |
| 26 |
Logically, there are two tokens A and B. Here is what both threads |
| 27 |
try to do: |
| 28 |
|
| 29 |
Thread 1 Thread 2 |
| 30 |
-------- -------- |
| 31 |
Repeat 100 times Repeat 100 times |
| 32 |
acquire A acquire B |
| 33 |
acquire B acquire A |
| 34 |
release A and B release A and B |
| 35 |
repeat repeat |
| 36 |
|
| 37 |
Notice that A and B are reversed in 1 and 2. If the token manager |
| 38 |
(which is not in the public interface, but hidden behind |
| 39 |
ACE_Local_Mutex) works properly, they should detect the deadlock. If |
| 40 |
a thread detects deadlock, the resources held are released, and it |
| 41 |
starts the whole process over again. |
| 42 |
|
| 43 |
What can be confusing about the test program is all the other tricks |
| 44 |
I'm pulling to test other aspects of the library. For instance, I'm |
| 45 |
using both "global" and "local" ACE_Local_Mutexes. This is to test |
| 46 |
the ability to have multiple threads using one token proxy as well as |
| 47 |
multiple threads each using their own proxies. All the while, the |
| 48 |
same logical token is being used. If this doesn't make sense, don't |
| 49 |
worry about it. Just use the ACE_Local_Mutex any way you want. |
| 50 |
|
| 51 |
Another confusing trick is that I'm testing recursive acquisition. |
| 52 |
(Two acquires in a row.) I have to make sure that the token manager |
| 53 |
doesn't detect a recursive acquire as deadlock. |
| 54 |
|
| 55 |
To run a test, simply type: |
| 56 |
% ./deadlock_detection_test |
| 57 |
|
| 58 |
This should run 100 times through the above pseudo code. If the |
| 59 |
application halts, then we have trouble. It should never ever halt. |
| 60 |
I've included a little flag with the ACE_Local_Mutex class to allow |
| 61 |
deadlock detection to be ignored. So, if you run the test as follows, |
| 62 |
deadlock detection will be ignored. |
| 63 |
|
| 64 |
% ./deadlock_detection_test -i |
| 65 |
|
| 66 |
In this case, the application should only run about a second before |
| 67 |
deadlock occurs and the application halts. This is good. |
| 68 |
|
| 69 |
------------------------------------------------------------ |
| 70 |
|
| 71 |
If you run ./deadlock_detection_test *with* -r, then the following |
| 72 |
rwlock test is run: |
| 73 |
|
| 74 |
There are four tokens and four threads in the rwlock test. The |
| 75 |
readers/writer tokens are: |
| 76 |
|
| 77 |
reader first |
| 78 |
writer first 1 |
| 79 |
writer first 2 |
| 80 |
writer first 3 |
| 81 |
|
| 82 |
There are three reader threads that only acquire reader locks on the |
| 83 |
above tokens. Each of the reader threads first acquire "reader first" |
| 84 |
and then one "writer first <tid>" (where <tid> is the corresponding |
| 85 |
thread's id). So reader thread 1 acquires "reader first" and then |
| 86 |
"writer first 1". |
| 87 |
|
| 88 |
There is a single writer thread that uses the following algorithm: |
| 89 |
|
| 90 |
repeat 100 |
| 91 |
acquire "writer first 1" |
| 92 |
acquire "reader first" |
| 93 |
acquire "writer first 2" |
| 94 |
acquire "reader first" |
| 95 |
acquire "writer first 3" |
| 96 |
acquire "reader first" |
| 97 |
|
| 98 |
This strange mix of readers and writer create an interesting graph of |
| 99 |
tokens that the deadlock detection algorithm must traverse. |