VirtualBox

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

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

Automated rebranding to Oracle copyright/license strings via filemuncher

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