VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstDeadlock.cpp@ 25368

Last change on this file since 25368 was 25368, checked in by vboxsync, 15 years ago

RTCritSect,PDMCritSect,iprt/lockvalidator.h: Reworked the deadlocking detection for critical sections and preparing for lock order validation. This change generalizes the RTCRITSECT::Strict data and moves it out of the RTCRITSECT, leaving a pointer behind. This saves a bit of space in release builds.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.2 KB
Line 
1/* $Id: tstDeadlock.cpp 25368 2009-12-14 16:31:40Z 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
38#include <iprt/err.h>
39#include <iprt/initterm.h>
40#include <iprt/stream.h>
41#include <iprt/thread.h>
42
43
44/*******************************************************************************
45* Global Variables *
46*******************************************************************************/
47static RTCRITSECT g_CritSect1;
48static RTCRITSECT g_CritSect2;
49static RTCRITSECT g_CritSect3;
50
51#define UNIT 250
52
53static DECLCALLBACK(int) Thread1(RTTHREAD ThreadSelf, void *pvUser)
54{
55 RTCritSectEnter(&g_CritSect1);
56 RTPrintf("thread1: got 1\n");
57 RTThreadSleep(3*UNIT);
58 RTPrintf("thread1: taking 2\n");
59 RTCritSectEnter(&g_CritSect2);
60 RTPrintf("thread1: got 2!!!\n");
61 return VERR_DEADLOCK;
62}
63
64static DECLCALLBACK(int) Thread2(RTTHREAD ThreadSelf, void *pvUser)
65{
66 RTCritSectEnter(&g_CritSect2);
67 RTPrintf("thread2: got 2\n");
68 RTThreadSleep(1*UNIT);
69 RTPrintf("thread2: taking 3\n");
70 RTCritSectEnter(&g_CritSect3);
71 RTPrintf("thread2: got 3!!!\n");
72 return VERR_DEADLOCK;
73}
74
75static DECLCALLBACK(int) Thread3(RTTHREAD ThreadSelf, void *pvUser)
76{
77 RTCritSectEnter(&g_CritSect3);
78 RTPrintf("thread3: got 3\n");
79 RTThreadSleep(2*UNIT);
80 RTPrintf("thread3: taking 1\n");
81 RTCritSectEnter(&g_CritSect1);
82 RTPrintf("thread3: got 1!!!\n");
83 return VERR_DEADLOCK;
84}
85
86
87int main()
88{
89 /*
90 * Init.
91 */
92 RTR3Init();
93 int rc = RTCritSectInit(&g_CritSect1);
94 if (RT_SUCCESS(rc))
95 rc = RTCritSectInit(&g_CritSect2);
96 if (RT_SUCCESS(rc))
97 rc = RTCritSectInit(&g_CritSect3);
98 if (RT_FAILURE(rc))
99 {
100 RTPrintf("tstDeadlock: failed to initialize critsects: %Rra\n", rc);
101 return 1;
102 }
103 RTCritSectEnter(&g_CritSect1);
104 if (!g_CritSect1.pValidatorRec || g_CritSect1.pValidatorRec->hThread == NIL_RTTHREAD)
105 {
106 RTPrintf("tstDeadlock: deadlock detection is not enabled in this build\n");
107 return 1;
108 }
109 RTCritSectLeave(&g_CritSect1);
110
111 /*
112 * Start the threads and wait for them to deadlock.
113 */
114 RTPrintf("tstDeadlock: TESTING...\n");
115 RTThreadYield();
116 rc = RTThreadCreate(NULL, Thread1, NULL, 0, RTTHREADTYPE_DEFAULT, 0, "Thread1");
117 if (RT_SUCCESS(rc))
118 rc = RTThreadCreate(NULL, Thread2, NULL, 0, RTTHREADTYPE_DEFAULT, 0, "Thread2");
119 if (RT_SUCCESS(rc))
120 rc = RTThreadCreate(NULL, Thread3, NULL, 0, RTTHREADTYPE_DEFAULT, 0, "Thread3");
121 if (RT_FAILURE(rc))
122 {
123 RTPrintf("tstDeadlock: failed to create threads: %Rra\n");
124 return 1;
125 }
126 for (;;)
127 RTThreadSleep(60000);
128
129 RTPrintf("tstDeadlock: Impossible!!!\n");
130 return 0;
131}
132
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette