1 | /* $Id: tstDeadlock.cpp 25406 2009-12-15 14:23:53Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - deadlock detection. Will never really "work".
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | *
|
---|
26 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 |
|
---|
32 | /*******************************************************************************
|
---|
33 | * Header Files *
|
---|
34 | *******************************************************************************/
|
---|
35 | #include <iprt/asm.h> /* for return addresses */
|
---|
36 | #include <iprt/critsect.h>
|
---|
37 | #include <iprt/lockvalidator.h>
|
---|
38 |
|
---|
39 | #include <iprt/err.h>
|
---|
40 | #include <iprt/initterm.h>
|
---|
41 | #include <iprt/stream.h>
|
---|
42 | #include <iprt/thread.h>
|
---|
43 |
|
---|
44 |
|
---|
45 | /*******************************************************************************
|
---|
46 | * Global Variables *
|
---|
47 | *******************************************************************************/
|
---|
48 | static RTCRITSECT g_CritSect1;
|
---|
49 | static RTCRITSECT g_CritSect2;
|
---|
50 | static RTCRITSECT g_CritSect3;
|
---|
51 |
|
---|
52 | #define UNIT 250
|
---|
53 |
|
---|
54 | static DECLCALLBACK(int) Thread1(RTTHREAD ThreadSelf, void *pvUser)
|
---|
55 | {
|
---|
56 | RTCritSectEnter(&g_CritSect1);
|
---|
57 | RTPrintf("thread1: got 1\n");
|
---|
58 | RTThreadSleep(3*UNIT);
|
---|
59 | RTPrintf("thread1: taking 2\n");
|
---|
60 | RTCritSectEnter(&g_CritSect2);
|
---|
61 | RTPrintf("thread1: got 2!!!\n");
|
---|
62 | return VERR_DEADLOCK;
|
---|
63 | }
|
---|
64 |
|
---|
65 | static DECLCALLBACK(int) Thread2(RTTHREAD ThreadSelf, void *pvUser)
|
---|
66 | {
|
---|
67 | RTCritSectEnter(&g_CritSect2);
|
---|
68 | RTPrintf("thread2: got 2\n");
|
---|
69 | RTThreadSleep(1*UNIT);
|
---|
70 | RTPrintf("thread2: taking 3\n");
|
---|
71 | RTCritSectEnter(&g_CritSect3);
|
---|
72 | RTPrintf("thread2: got 3!!!\n");
|
---|
73 | return VERR_DEADLOCK;
|
---|
74 | }
|
---|
75 |
|
---|
76 | static DECLCALLBACK(int) Thread3(RTTHREAD ThreadSelf, void *pvUser)
|
---|
77 | {
|
---|
78 | RTCritSectEnter(&g_CritSect3);
|
---|
79 | RTPrintf("thread3: got 3\n");
|
---|
80 | RTThreadSleep(2*UNIT);
|
---|
81 | RTPrintf("thread3: taking 1\n");
|
---|
82 | RTCritSectEnter(&g_CritSect1);
|
---|
83 | RTPrintf("thread3: got 1!!!\n");
|
---|
84 | return VERR_DEADLOCK;
|
---|
85 | }
|
---|
86 |
|
---|
87 |
|
---|
88 | int main()
|
---|
89 | {
|
---|
90 | /*
|
---|
91 | * Init.
|
---|
92 | */
|
---|
93 | RTR3Init();
|
---|
94 | int rc = RTCritSectInit(&g_CritSect1);
|
---|
95 | if (RT_SUCCESS(rc))
|
---|
96 | rc = RTCritSectInit(&g_CritSect2);
|
---|
97 | if (RT_SUCCESS(rc))
|
---|
98 | rc = RTCritSectInit(&g_CritSect3);
|
---|
99 | if (RT_FAILURE(rc))
|
---|
100 | {
|
---|
101 | RTPrintf("tstDeadlock: failed to initialize critsects: %Rra\n", rc);
|
---|
102 | return 1;
|
---|
103 | }
|
---|
104 | RTCritSectEnter(&g_CritSect1);
|
---|
105 | if (!g_CritSect1.pValidatorRec || g_CritSect1.pValidatorRec->hThread == NIL_RTTHREAD)
|
---|
106 | {
|
---|
107 | RTPrintf("tstDeadlock: deadlock detection is not enabled in this build\n");
|
---|
108 | return 1;
|
---|
109 | }
|
---|
110 | RTCritSectLeave(&g_CritSect1);
|
---|
111 |
|
---|
112 | /*
|
---|
113 | * Start the threads and wait for them to deadlock.
|
---|
114 | */
|
---|
115 | RTPrintf("tstDeadlock: TESTING...\n");
|
---|
116 | RTThreadYield();
|
---|
117 | rc = RTThreadCreate(NULL, Thread1, NULL, 0, RTTHREADTYPE_DEFAULT, 0, "Thread1");
|
---|
118 | if (RT_SUCCESS(rc))
|
---|
119 | rc = RTThreadCreate(NULL, Thread2, NULL, 0, RTTHREADTYPE_DEFAULT, 0, "Thread2");
|
---|
120 | if (RT_SUCCESS(rc))
|
---|
121 | rc = RTThreadCreate(NULL, Thread3, NULL, 0, RTTHREADTYPE_DEFAULT, 0, "Thread3");
|
---|
122 | if (RT_FAILURE(rc))
|
---|
123 | {
|
---|
124 | RTPrintf("tstDeadlock: failed to create threads: %Rra\n");
|
---|
125 | return 1;
|
---|
126 | }
|
---|
127 | for (;;)
|
---|
128 | RTThreadSleep(60000);
|
---|
129 |
|
---|
130 | RTPrintf("tstDeadlock: Impossible!!!\n");
|
---|
131 | return 0;
|
---|
132 | }
|
---|
133 |
|
---|