VirtualBox

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

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

tstR0ThreadPreemption: use ASMIntAreEnabled.

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