VirtualBox

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

Last change on this file since 4968 was 4071, checked in by vboxsync, 17 years ago

Biggest check-in ever. New source code headers for all (C) innotek files.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.5 KB
Line 
1/* $Id: tstDeadlock.cpp 4071 2007-08-07 17:07:59Z 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 as published by the Free Software Foundation,
13 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
14 * distribution. VirtualBox OSE is distributed in the hope that it will
15 * be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22#include <iprt/thread.h>
23#include <iprt/critsect.h>
24#include <iprt/stream.h>
25#include <iprt/err.h>
26#include <iprt/runtime.h>
27
28
29/*******************************************************************************
30* Global Variables *
31*******************************************************************************/
32static RTCRITSECT g_CritSect1;
33static RTCRITSECT g_CritSect2;
34static RTCRITSECT g_CritSect3;
35
36#define UNIT 250
37
38static DECLCALLBACK(int) Thread1(RTTHREAD ThreadSelf, void *pvUser)
39{
40 RTCritSectEnter(&g_CritSect1);
41 RTPrintf("thread1: got 1\n");
42 RTThreadSleep(3*UNIT);
43 RTPrintf("thread1: taking 2\n");
44 RTCritSectEnter(&g_CritSect2);
45 RTPrintf("thread1: got 2!!!\n");
46 return VERR_DEADLOCK;
47}
48
49static DECLCALLBACK(int) Thread2(RTTHREAD ThreadSelf, void *pvUser)
50{
51 RTCritSectEnter(&g_CritSect2);
52 RTPrintf("thread2: got 2\n");
53 RTThreadSleep(1*UNIT);
54 RTPrintf("thread2: taking 3\n");
55 RTCritSectEnter(&g_CritSect3);
56 RTPrintf("thread2: got 3!!!\n");
57 return VERR_DEADLOCK;
58}
59
60static DECLCALLBACK(int) Thread3(RTTHREAD ThreadSelf, void *pvUser)
61{
62 RTCritSectEnter(&g_CritSect3);
63 RTPrintf("thread3: got 3\n");
64 RTThreadSleep(2*UNIT);
65 RTPrintf("thread3: taking 1\n");
66 RTCritSectEnter(&g_CritSect1);
67 RTPrintf("thread1: got 1!!!\n");
68 return VERR_DEADLOCK;
69}
70
71
72int main()
73{
74 /*
75 * Init.
76 */
77 RTR3Init();
78 int rc = RTCritSectInit(&g_CritSect1);
79 if (RT_SUCCESS(rc))
80 rc = RTCritSectInit(&g_CritSect2);
81 if (RT_SUCCESS(rc))
82 rc = RTCritSectInit(&g_CritSect3);
83 if (RT_FAILURE(rc))
84 {
85 RTPrintf("tstDeadlock: failed to initialize critsects: %Rra\n", rc);
86 return 1;
87 }
88 RTCritSectEnter(&g_CritSect1);
89 if (g_CritSect1.Strict.ThreadOwner == NIL_RTTHREAD)
90 {
91 RTPrintf("tstDeadlock: deadlock detection is not enabled in this build\n");
92 return 1;
93 }
94 RTCritSectLeave(&g_CritSect1);
95
96 /*
97 * Start the threads and wait for them to deadlock.
98 */
99 RTPrintf("tstDeadlock: TESTING...\n");
100 RTThreadYield();
101 rc = RTThreadCreate(NULL, Thread1, NULL, 0, RTTHREADTYPE_DEFAULT, 0, "Thread1");
102 if (RT_SUCCESS(rc))
103 rc = RTThreadCreate(NULL, Thread2, NULL, 0, RTTHREADTYPE_DEFAULT, 0, "Thread2");
104 if (RT_SUCCESS(rc))
105 rc = RTThreadCreate(NULL, Thread3, NULL, 0, RTTHREADTYPE_DEFAULT, 0, "Thread3");
106 if (RT_FAILURE(rc))
107 {
108 RTPrintf("tstDeadlock: failed to create threads: %Rra\n");
109 return 1;
110 }
111 for (;;)
112 RTThreadSleep(60000);
113
114 RTPrintf("tstDeadlock: Impossible!!!\n");
115 return 0;
116}
117
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