VirtualBox

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

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

header (C) fixes

  • 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 44529 2013-02-04 15:54:15Z 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.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* 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_PENDING:
95 {
96 RTTHREADPREEMPTSTATE State = RTTHREADPREEMPTSTATE_INITIALIZER;
97 RTThreadPreemptDisable(&State);
98 if (!RTThreadPreemptIsEnabled(NIL_RTTHREAD))
99 {
100 if (ASMIntAreEnabled())
101 {
102 uint64_t u64StartTS = RTTimeNanoTS();
103 uint64_t u64StartSysTS = RTTimeSystemNanoTS();
104 uint64_t cLoops = 0;
105 uint64_t cNanosSysElapsed;
106 uint64_t cNanosElapsed;
107 bool fPending;
108 do
109 {
110 fPending = RTThreadPreemptIsPending(NIL_RTTHREAD);
111 cNanosElapsed = RTTimeNanoTS() - u64StartTS;
112 cNanosSysElapsed = RTTimeSystemNanoTS() - u64StartSysTS;
113 cLoops++;
114 } while ( !fPending
115 && cNanosElapsed < UINT64_C(2)*1000U*1000U*1000U
116 && cNanosSysElapsed < UINT64_C(2)*1000U*1000U*1000U
117 && cLoops < 100U*_1M);
118 if (!fPending)
119 RTStrPrintf(pszErr, cchErr, "!Preempt not pending after %'llu loops / %'llu ns / %'llu ns (sys)",
120 cLoops, cNanosElapsed, cNanosSysElapsed);
121 else if (cLoops == 1)
122 RTStrPrintf(pszErr, cchErr, "!cLoops=1\n");
123 else
124 RTStrPrintf(pszErr, cchErr, "RTThreadPreemptIsPending returned true after %'llu loops / %'llu ns / %'llu ns (sys)",
125 cLoops, cNanosElapsed, cNanosSysElapsed);
126 }
127 else
128 RTStrPrintf(pszErr, cchErr, "!Interrupts disabled");
129 }
130 else
131 RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns true after RTThreadPreemptDisable");
132 RTThreadPreemptRestore(&State);
133 break;
134 }
135
136 case TSTR0THREADPREMEPTION_NESTED:
137 {
138 bool const fDefault = RTThreadPreemptIsEnabled(NIL_RTTHREAD);
139 RTTHREADPREEMPTSTATE State1 = RTTHREADPREEMPTSTATE_INITIALIZER;
140 RTThreadPreemptDisable(&State1);
141 if (!RTThreadPreemptIsEnabled(NIL_RTTHREAD))
142 {
143 RTTHREADPREEMPTSTATE State2 = RTTHREADPREEMPTSTATE_INITIALIZER;
144 RTThreadPreemptDisable(&State2);
145 if (!RTThreadPreemptIsEnabled(NIL_RTTHREAD))
146 {
147 RTTHREADPREEMPTSTATE State3 = RTTHREADPREEMPTSTATE_INITIALIZER;
148 RTThreadPreemptDisable(&State3);
149 if (RTThreadPreemptIsEnabled(NIL_RTTHREAD))
150 RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns true after 3rd RTThreadPreemptDisable");
151
152 RTThreadPreemptRestore(&State3);
153 if (RTThreadPreemptIsEnabled(NIL_RTTHREAD) && !*pszErr)
154 RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns true after 1st RTThreadPreemptRestore");
155 }
156 else
157 RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns true after 2nd RTThreadPreemptDisable");
158
159 RTThreadPreemptRestore(&State2);
160 if (RTThreadPreemptIsEnabled(NIL_RTTHREAD) && !*pszErr)
161 RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns true after 2nd RTThreadPreemptRestore");
162 }
163 else
164 RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns true after 1st RTThreadPreemptDisable");
165 RTThreadPreemptRestore(&State1);
166 if (RTThreadPreemptIsEnabled(NIL_RTTHREAD) != fDefault && !*pszErr)
167 RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns false after 3rd RTThreadPreemptRestore");
168 break;
169 }
170
171 default:
172 RTStrPrintf(pszErr, cchErr, "!Unknown test #%d", uOperation);
173 break;
174 }
175
176 /* The error indicator is the '!' in the message buffer. */
177 return VINF_SUCCESS;
178}
179
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