VirtualBox

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

Last change on this file since 19936 was 19936, checked in by vboxsync, 16 years ago

tstR0ThreadPreemption: Improved the RTThreadPreemptIsPending test.

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