VirtualBox

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

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

tstR0ThreadPreemption.cpp: use thousand separators to make the ns and loop count numbers readable.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.1 KB
Line 
1/* $Id: tstR0ThreadPreemption.cpp 19943 2009-05-23 15:46:49Z 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 <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 if (!RTThreadPreemptIsEnabled(NIL_RTTHREAD))
82 RTStrPrintf(pszErr, cchErr, "RTThreadPreemptIsEnabled returns false by default");
83 RTThreadPreemptDisable(&State);
84 if (RTThreadPreemptIsEnabled(NIL_RTTHREAD))
85 RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns true after RTThreadPreemptDisable");
86 else if (!(ASMGetFlags() & X86_EFL_IF))
87 RTStrPrintf(pszErr, cchErr, "!Interrupts disabled");
88 RTThreadPreemptRestore(&State);
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 (ASMGetFlags() & X86_EFL_IF)
99 {
100 uint64_t u64StartTS = RTTimeNanoTS();
101 uint64_t cLoops = 0;
102 uint64_t cNanosElapsed;
103 bool fPending;
104 do
105 {
106 fPending = RTThreadPreemptIsPending(NIL_RTTHREAD);
107 cNanosElapsed = RTTimeNanoTS() - u64StartTS;
108 cLoops++;
109 } while ( !fPending
110 && cNanosElapsed < UINT64_C(60)*1000U*1000U*1000U);
111 if (!fPending)
112 RTStrPrintf(pszErr, cchErr, "!Preempt not pending after %'llu loops / %'llu ns",
113 cLoops, cNanosElapsed);
114 else if (cLoops == 1)
115 RTStrPrintf(pszErr, cchErr, "!cLoops=0\n");
116 else
117 RTStrPrintf(pszErr, cchErr, "RTThreadPreemptIsPending returned true after %'llu loops / %'llu ns",
118 cLoops, cNanosElapsed);
119 }
120 else
121 RTStrPrintf(pszErr, cchErr, "!Interrupts disabled");
122 }
123 else
124 RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns true after RTThreadPreemptDisable");
125 RTThreadPreemptRestore(&State);
126 break;
127 }
128
129 case TSTR0THREADPREMEPTION_NESTED:
130 {
131 bool const fDefault = RTThreadPreemptIsEnabled(NIL_RTTHREAD);
132 RTTHREADPREEMPTSTATE State1 = RTTHREADPREEMPTSTATE_INITIALIZER;
133 RTThreadPreemptDisable(&State1);
134 if (!RTThreadPreemptIsEnabled(NIL_RTTHREAD))
135 {
136 RTTHREADPREEMPTSTATE State2 = RTTHREADPREEMPTSTATE_INITIALIZER;
137 RTThreadPreemptDisable(&State2);
138 if (!RTThreadPreemptIsEnabled(NIL_RTTHREAD))
139 {
140 RTTHREADPREEMPTSTATE State3 = RTTHREADPREEMPTSTATE_INITIALIZER;
141 RTThreadPreemptDisable(&State3);
142 if (RTThreadPreemptIsEnabled(NIL_RTTHREAD))
143 RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns true after 3rd RTThreadPreemptDisable");
144
145 RTThreadPreemptRestore(&State3);
146 if (RTThreadPreemptIsEnabled(NIL_RTTHREAD) && !*pszErr)
147 RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns true after 1st RTThreadPreemptRestore");
148 }
149 else
150 RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns true after 2nd RTThreadPreemptDisable");
151
152 RTThreadPreemptRestore(&State2);
153 if (RTThreadPreemptIsEnabled(NIL_RTTHREAD) && !*pszErr)
154 RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns true after 2nd RTThreadPreemptRestore");
155 }
156 else
157 RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns true after 1st RTThreadPreemptDisable");
158 RTThreadPreemptRestore(&State1);
159 if (RTThreadPreemptIsEnabled(NIL_RTTHREAD) != fDefault && !*pszErr)
160 RTStrPrintf(pszErr, cchErr, "!RTThreadPreemptIsEnabled returns false after 3rd RTThreadPreemptRestore");
161 break;
162 }
163
164 default:
165 RTStrPrintf(pszErr, cchErr, "!Unknown test #%d", uOperation);
166 break;
167 }
168
169 /* The error indicator is the '!' in the message buffer. */
170 return VINF_SUCCESS;
171}
172
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