VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstR0ThreadPreemption.cpp@ 45539

Last change on this file since 45539 was 45539, checked in by vboxsync, 12 years ago

duh

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.7 KB
Line 
1/* $Id: tstR0ThreadPreemption.cpp 45539 2013-04-14 11:02:12Z vboxsync $ */
2/** @file
3 * IPRT R0 Testcase - Thread Preemption.
4 */
5
6/*
7 * Copyright (C) 2009-2011 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. 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* Header Files *
29*******************************************************************************/
30#include <iprt/thread.h>
31
32#include <iprt/asm-amd64-x86.h>
33#include <iprt/err.h>
34#include <iprt/time.h>
35#include <iprt/string.h>
36#include <VBox/sup.h>
37#include "tstR0ThreadPreemption.h"
38
39
40
41/**
42 * Service request callback function.
43 *
44 * @returns VBox status code.
45 * @param pSession The caller's session.
46 * @param u64Arg 64-bit integer argument.
47 * @param pReqHdr The request header. Input / Output. Optional.
48 */
49DECLEXPORT(int) TSTR0ThreadPreemptionSrvReqHandler(PSUPDRVSESSION pSession, uint32_t uOperation,
50 uint64_t u64Arg, PSUPR0SERVICEREQHDR pReqHdr)
51{
52 NOREF(pSession);
53 if (u64Arg)
54 return VERR_INVALID_PARAMETER;
55 if (!VALID_PTR(pReqHdr))
56 return VERR_INVALID_PARAMETER;
57 char *pszErr = (char *)(pReqHdr + 1);
58 size_t cchErr = pReqHdr->cbReq - sizeof(*pReqHdr);
59 if (cchErr < 32 || cchErr >= 0x10000)
60 return VERR_INVALID_PARAMETER;
61 *pszErr = '\0';
62
63 /*
64 * The big switch.
65 */
66 switch (uOperation)
67 {
68 case TSTR0THREADPREMEPTION_SANITY_OK:
69 break;
70
71 case TSTR0THREADPREMEPTION_SANITY_FAILURE:
72 RTStrPrintf(pszErr, cchErr, "!42failure42%1024s", "");
73 break;
74
75 case TSTR0THREADPREMEPTION_BASIC:
76 {
77 if (!ASMIntAreEnabled())
78 RTStrPrintf(pszErr, cchErr, "!Interrupts disabled");
79 else if (!RTThreadPreemptIsEnabled(NIL_RTTHREAD))
80 RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns false by default");
81 else
82 {
83 RTTHREADPREEMPTSTATE State = RTTHREADPREEMPTSTATE_INITIALIZER;
84 RTThreadPreemptDisable(&State);
85 if (RTThreadPreemptIsEnabled(NIL_RTTHREAD))
86 RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns true after RTThreadPreemptDisable");
87 else if (!ASMIntAreEnabled())
88 RTStrPrintf(pszErr, cchErr, "!Interrupts disabled");
89 RTThreadPreemptRestore(&State);
90 }
91 break;
92 }
93
94 case TSTR0THREADPREMEPTION_IS_TRUSTY:
95 if (!RTThreadPreemptIsPendingTrusty())
96 RTStrPrintf(pszErr, cchErr, "!Untrusty");
97 break;
98
99 case TSTR0THREADPREMEPTION_IS_PENDING:
100 {
101 RTTHREADPREEMPTSTATE State = RTTHREADPREEMPTSTATE_INITIALIZER;
102 RTThreadPreemptDisable(&State);
103 if (!RTThreadPreemptIsEnabled(NIL_RTTHREAD))
104 {
105 if (ASMIntAreEnabled())
106 {
107 uint64_t u64StartTS = RTTimeNanoTS();
108 uint64_t u64StartSysTS = RTTimeSystemNanoTS();
109 uint64_t cLoops = 0;
110 uint64_t cNanosSysElapsed;
111 uint64_t cNanosElapsed;
112 bool fPending;
113 do
114 {
115 fPending = RTThreadPreemptIsPending(NIL_RTTHREAD);
116 cNanosElapsed = RTTimeNanoTS() - u64StartTS;
117 cNanosSysElapsed = RTTimeSystemNanoTS() - u64StartSysTS;
118 cLoops++;
119 } while ( !fPending
120 && cNanosElapsed < UINT64_C(2)*1000U*1000U*1000U
121 && cNanosSysElapsed < UINT64_C(2)*1000U*1000U*1000U
122 && cLoops < 100U*_1M);
123 if (!fPending)
124 RTStrPrintf(pszErr, cchErr, "!Preempt not pending after %'llu loops / %'llu ns / %'llu ns (sys)",
125 cLoops, cNanosElapsed, cNanosSysElapsed);
126 else if (cLoops == 1)
127 RTStrPrintf(pszErr, cchErr, "!cLoops=1\n");
128 else
129 RTStrPrintf(pszErr, cchErr, "RTThreadPreemptIsPending returned true after %'llu loops / %'llu ns / %'llu ns (sys)",
130 cLoops, cNanosElapsed, cNanosSysElapsed);
131 }
132 else
133 RTStrPrintf(pszErr, cchErr, "!Interrupts disabled");
134 }
135 else
136 RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns true after RTThreadPreemptDisable");
137 RTThreadPreemptRestore(&State);
138 break;
139 }
140
141 case TSTR0THREADPREMEPTION_NESTED:
142 {
143 bool const fDefault = RTThreadPreemptIsEnabled(NIL_RTTHREAD);
144 RTTHREADPREEMPTSTATE State1 = RTTHREADPREEMPTSTATE_INITIALIZER;
145 RTThreadPreemptDisable(&State1);
146 if (!RTThreadPreemptIsEnabled(NIL_RTTHREAD))
147 {
148 RTTHREADPREEMPTSTATE State2 = RTTHREADPREEMPTSTATE_INITIALIZER;
149 RTThreadPreemptDisable(&State2);
150 if (!RTThreadPreemptIsEnabled(NIL_RTTHREAD))
151 {
152 RTTHREADPREEMPTSTATE State3 = RTTHREADPREEMPTSTATE_INITIALIZER;
153 RTThreadPreemptDisable(&State3);
154 if (RTThreadPreemptIsEnabled(NIL_RTTHREAD))
155 RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns true after 3rd RTThreadPreemptDisable");
156
157 RTThreadPreemptRestore(&State3);
158 if (RTThreadPreemptIsEnabled(NIL_RTTHREAD) && !*pszErr)
159 RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns true after 1st RTThreadPreemptRestore");
160 }
161 else
162 RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns true after 2nd RTThreadPreemptDisable");
163
164 RTThreadPreemptRestore(&State2);
165 if (RTThreadPreemptIsEnabled(NIL_RTTHREAD) && !*pszErr)
166 RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns true after 2nd RTThreadPreemptRestore");
167 }
168 else
169 RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns true after 1st RTThreadPreemptDisable");
170 RTThreadPreemptRestore(&State1);
171 if (RTThreadPreemptIsEnabled(NIL_RTTHREAD) != fDefault && !*pszErr)
172 RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns false after 3rd RTThreadPreemptRestore");
173 break;
174 }
175
176 default:
177 RTStrPrintf(pszErr, cchErr, "!Unknown test #%d", uOperation);
178 break;
179 }
180
181 /* The error indicator is the '!' in the message buffer. */
182 return VINF_SUCCESS;
183}
184
Note: See TracBrowser for help on using the repository browser.

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