1 | /* $Id: tstDeadlock.cpp 5999 2007-12-07 15:05:06Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - deadlock detection. Will never really "work".
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
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 |
|
---|
27 |
|
---|
28 | /*******************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *******************************************************************************/
|
---|
31 | #include <iprt/thread.h>
|
---|
32 | #include <iprt/critsect.h>
|
---|
33 | #include <iprt/stream.h>
|
---|
34 | #include <iprt/err.h>
|
---|
35 | #include <iprt/runtime.h>
|
---|
36 |
|
---|
37 |
|
---|
38 | /*******************************************************************************
|
---|
39 | * Global Variables *
|
---|
40 | *******************************************************************************/
|
---|
41 | static RTCRITSECT g_CritSect1;
|
---|
42 | static RTCRITSECT g_CritSect2;
|
---|
43 | static RTCRITSECT g_CritSect3;
|
---|
44 |
|
---|
45 | #define UNIT 250
|
---|
46 |
|
---|
47 | static DECLCALLBACK(int) Thread1(RTTHREAD ThreadSelf, void *pvUser)
|
---|
48 | {
|
---|
49 | RTCritSectEnter(&g_CritSect1);
|
---|
50 | RTPrintf("thread1: got 1\n");
|
---|
51 | RTThreadSleep(3*UNIT);
|
---|
52 | RTPrintf("thread1: taking 2\n");
|
---|
53 | RTCritSectEnter(&g_CritSect2);
|
---|
54 | RTPrintf("thread1: got 2!!!\n");
|
---|
55 | return VERR_DEADLOCK;
|
---|
56 | }
|
---|
57 |
|
---|
58 | static DECLCALLBACK(int) Thread2(RTTHREAD ThreadSelf, void *pvUser)
|
---|
59 | {
|
---|
60 | RTCritSectEnter(&g_CritSect2);
|
---|
61 | RTPrintf("thread2: got 2\n");
|
---|
62 | RTThreadSleep(1*UNIT);
|
---|
63 | RTPrintf("thread2: taking 3\n");
|
---|
64 | RTCritSectEnter(&g_CritSect3);
|
---|
65 | RTPrintf("thread2: got 3!!!\n");
|
---|
66 | return VERR_DEADLOCK;
|
---|
67 | }
|
---|
68 |
|
---|
69 | static DECLCALLBACK(int) Thread3(RTTHREAD ThreadSelf, void *pvUser)
|
---|
70 | {
|
---|
71 | RTCritSectEnter(&g_CritSect3);
|
---|
72 | RTPrintf("thread3: got 3\n");
|
---|
73 | RTThreadSleep(2*UNIT);
|
---|
74 | RTPrintf("thread3: taking 1\n");
|
---|
75 | RTCritSectEnter(&g_CritSect1);
|
---|
76 | RTPrintf("thread1: got 1!!!\n");
|
---|
77 | return VERR_DEADLOCK;
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 | int main()
|
---|
82 | {
|
---|
83 | /*
|
---|
84 | * Init.
|
---|
85 | */
|
---|
86 | RTR3Init();
|
---|
87 | int rc = RTCritSectInit(&g_CritSect1);
|
---|
88 | if (RT_SUCCESS(rc))
|
---|
89 | rc = RTCritSectInit(&g_CritSect2);
|
---|
90 | if (RT_SUCCESS(rc))
|
---|
91 | rc = RTCritSectInit(&g_CritSect3);
|
---|
92 | if (RT_FAILURE(rc))
|
---|
93 | {
|
---|
94 | RTPrintf("tstDeadlock: failed to initialize critsects: %Rra\n", rc);
|
---|
95 | return 1;
|
---|
96 | }
|
---|
97 | RTCritSectEnter(&g_CritSect1);
|
---|
98 | if (g_CritSect1.Strict.ThreadOwner == NIL_RTTHREAD)
|
---|
99 | {
|
---|
100 | RTPrintf("tstDeadlock: deadlock detection is not enabled in this build\n");
|
---|
101 | return 1;
|
---|
102 | }
|
---|
103 | RTCritSectLeave(&g_CritSect1);
|
---|
104 |
|
---|
105 | /*
|
---|
106 | * Start the threads and wait for them to deadlock.
|
---|
107 | */
|
---|
108 | RTPrintf("tstDeadlock: TESTING...\n");
|
---|
109 | RTThreadYield();
|
---|
110 | rc = RTThreadCreate(NULL, Thread1, NULL, 0, RTTHREADTYPE_DEFAULT, 0, "Thread1");
|
---|
111 | if (RT_SUCCESS(rc))
|
---|
112 | rc = RTThreadCreate(NULL, Thread2, NULL, 0, RTTHREADTYPE_DEFAULT, 0, "Thread2");
|
---|
113 | if (RT_SUCCESS(rc))
|
---|
114 | rc = RTThreadCreate(NULL, Thread3, NULL, 0, RTTHREADTYPE_DEFAULT, 0, "Thread3");
|
---|
115 | if (RT_FAILURE(rc))
|
---|
116 | {
|
---|
117 | RTPrintf("tstDeadlock: failed to create threads: %Rra\n");
|
---|
118 | return 1;
|
---|
119 | }
|
---|
120 | for (;;)
|
---|
121 | RTThreadSleep(60000);
|
---|
122 |
|
---|
123 | RTPrintf("tstDeadlock: Impossible!!!\n");
|
---|
124 | return 0;
|
---|
125 | }
|
---|
126 |
|
---|