tstR0ThreadPreemptionDriver.cpp revision 176991ff77f632fbe0e92e3aa17cf249367a309b
4641N/A/* $Id$ */
4641N/A/** @file
4641N/A * IPRT R0 Testcase - Thread Preemption, driver program.
4641N/A */
4641N/A
4641N/A/*
4641N/A * Copyright (C) 2009 Sun Microsystems, Inc.
4641N/A *
4641N/A * This file is part of VirtualBox Open Source Edition (OSE), as
4641N/A * available from http://www.virtualbox.org. This file is free software;
4641N/A * you can redistribute it and/or modify it under the terms of the GNU
4641N/A * General Public License (GPL) as published by the Free Software
4641N/A * Foundation, in version 2 as it comes in the "COPYING" file of the
4641N/A * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
4641N/A * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
4641N/A *
4641N/A * The contents of this file may alternatively be used under the terms
4641N/A * of the Common Development and Distribution License Version 1.0
4641N/A * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
4641N/A * VirtualBox OSE distribution, in which case the provisions of the
4641N/A * CDDL are applicable instead of those of the GPL.
4641N/A *
4641N/A * You may elect to license modified versions of this file under the
4641N/A * terms and conditions of either the GPL or the CDDL or both.
5070N/A *
5327N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
4641N/A * Clara, CA 95054 USA or visit http://www.sun.com if you need
4641N/A * additional information or have any questions.
4641N/A */
4641N/A
4641N/A/*******************************************************************************
4641N/A* Header Files *
4641N/A*******************************************************************************/
4641N/A#include <iprt/initterm.h>
4641N/A
4641N/A#include <iprt/err.h>
4641N/A#include <iprt/path.h>
4641N/A#include <iprt/param.h>
4641N/A#include <iprt/stream.h>
4641N/A#include <iprt/string.h>
4641N/A#include <iprt/test.h>
4641N/A#include <iprt/thread.h>
4641N/A#ifdef VBOX
4641N/A# include <VBox/sup.h>
4641N/A# include "tstR0ThreadPreemption.h"
4641N/A#endif
4659N/A
4641N/A
4641N/Aint main(int argc, char **argv)
4641N/A{
4641N/A#ifndef VBOX
4641N/A RTPrintf("tstSup: SKIPPED\n");
4924N/A return 0;
4641N/A#else
4641N/A /*
4641N/A * Init.
4641N/A */
4641N/A RTTEST hTest;
4641N/A int rc = RTTestInitAndCreate("tstR0ThreadPreemption", &hTest);
4641N/A if (rc)
4641N/A return rc;
4641N/A RTTestBanner(hTest);
4641N/A
4641N/A PSUPDRVSESSION pSession;
4641N/A rc = SUPR3Init(&pSession);
4641N/A if (RT_FAILURE(rc))
4641N/A {
4641N/A RTTestFailed(hTest, "SUPR3Init failed with rc=%Rrc\n", rc);
4641N/A return RTTestSummaryAndDestroy(hTest);
4641N/A }
4641N/A
4641N/A char szPath[RTPATH_MAX];
4641N/A rc = RTPathExecDir(szPath, sizeof(szPath));
4641N/A if (RT_SUCCESS(rc))
4641N/A rc = RTPathAppend(szPath, sizeof(szPath), "tstR0ThreadPreemption.r0");
4641N/A if (RT_FAILURE(rc))
4641N/A {
4641N/A RTTestFailed(hTest, "Failed constructing .r0 filename (rc=%Rrc)", rc);
4641N/A return RTTestSummaryAndDestroy(hTest);
4641N/A }
4641N/A
4641N/A void *pvImageBase;
4641N/A rc = SUPR3LoadServiceModule(szPath, "tstR0ThreadPreemption",
4641N/A "TSTR0ThreadPreemptionSrvReqHandler",
4641N/A &pvImageBase);
4641N/A if (RT_FAILURE(rc))
4641N/A {
4641N/A RTTestFailed(hTest, "SUPR3LoadServiceModule(%s,,,) failed with rc=%Rrc\n", szPath, rc);
4641N/A return RTTestSummaryAndDestroy(hTest);
4641N/A }
4641N/A
4641N/A /* test request */
4641N/A struct
4641N/A {
4641N/A SUPR0SERVICEREQHDR Hdr;
4641N/A char szMsg[256];
4641N/A } Req;
4641N/A
4641N/A /*
4641N/A * Sanity checks.
4641N/A */
4641N/A RTTestSub(hTest, "Sanity");
4641N/A Req.Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
4641N/A Req.Hdr.cbReq = sizeof(Req);
4641N/A Req.szMsg[0] = '\0';
4641N/A RTTESTI_CHECK_RC(rc = SUPR3CallR0Service("tstR0ThreadPreemption", sizeof("tstR0ThreadPreemption") - 1,
4641N/A TSTR0THREADPREMEPTION_SANITY_OK, 0, &Req.Hdr), VINF_SUCCESS);
4641N/A if (RT_FAILURE(rc))
4641N/A return RTTestSummaryAndDestroy(hTest);
4641N/A RTTESTI_CHECK_MSG(Req.szMsg[0] == '\0', ("%s", Req.szMsg));
4641N/A if (Req.szMsg[0] != '\0')
4641N/A return RTTestSummaryAndDestroy(hTest);
4641N/A
4641N/A Req.Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
4807N/A Req.Hdr.cbReq = sizeof(Req);
4641N/A Req.szMsg[0] = '\0';
4641N/A RTTESTI_CHECK_RC(rc = SUPR3CallR0Service("tstR0ThreadPreemption", sizeof("tstR0ThreadPreemption") - 1,
4641N/A TSTR0THREADPREMEPTION_SANITY_FAILURE, 0, &Req.Hdr), VINF_SUCCESS);
4641N/A if (RT_FAILURE(rc))
4641N/A return RTTestSummaryAndDestroy(hTest);
4641N/A RTTESTI_CHECK_MSG(!strncmp(Req.szMsg, "!42failure42", sizeof("!42failure42") - 1), ("%s", Req.szMsg));
4641N/A if (strncmp(Req.szMsg, "!42failure42", sizeof("!42failure42") - 1))
4641N/A return RTTestSummaryAndDestroy(hTest);
4641N/A
4641N/A /*
4641N/A * Basic tests, bail out on failure.
4641N/A */
4641N/A RTTestSub(hTest, "Basics");
4641N/A Req.Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
4641N/A Req.Hdr.cbReq = sizeof(Req);
4641N/A Req.szMsg[0] = '\0';
4641N/A RTTESTI_CHECK_RC(rc = SUPR3CallR0Service("tstR0ThreadPreemption", sizeof("tstR0ThreadPreemption") - 1,
4641N/A TSTR0THREADPREMEPTION_BASIC, 0, &Req.Hdr), VINF_SUCCESS);
4641N/A if (RT_FAILURE(rc))
4641N/A return RTTestSummaryAndDestroy(hTest);
4641N/A if (Req.szMsg[0] == '!')
4641N/A {
4641N/A RTTestIFailed("%s", &Req.szMsg[1]);
4641N/A return RTTestSummaryAndDestroy(hTest);
4641N/A }
4641N/A if (Req.szMsg[0])
4641N/A RTTestIPrintf(RTTESTLVL_ALWAYS, "%s", Req.szMsg);
4641N/A
4641N/A /*
4659N/A * Stay in ring-0 until preemption is pending.
4659N/A */
4641N/A RTTestSub(hTest, "Pending Preemption");
4641N/A for (int i = 0; ; i++)
4641N/A {
4641N/A Req.Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
4641N/A Req.Hdr.cbReq = sizeof(Req);
4641N/A Req.szMsg[0] = '\0';
4641N/A RTTESTI_CHECK_RC(rc = SUPR3CallR0Service("tstR0ThreadPreemption", sizeof("tstR0ThreadPreemption") - 1,
4641N/A TSTR0THREADPREMEPTION_IS_PENDING, 0, &Req.Hdr), VINF_SUCCESS);
4641N/A if ( strcmp(Req.szMsg, "cLoops=1\n")
4641N/A || i >= 64)
4641N/A {
4641N/A if (Req.szMsg[0] == '!')
4807N/A RTTestIFailed("%s", &Req.szMsg[1]);
4641N/A else if (Req.szMsg[0])
4641N/A RTTestIPrintf(RTTESTLVL_ALWAYS, "%s", Req.szMsg);
4641N/A break;
4641N/A }
4641N/A if ((i % 3) == 0)
4641N/A RTThreadYield();
4641N/A }
4641N/A
4641N/A /*
4641N/A * Test nested RTThreadPreemptDisable calls.
4641N/A */
4641N/A RTTestSub(hTest, "Nested");
4641N/A Req.Hdr.u32Magic = SUPR0SERVICEREQHDR_MAGIC;
4641N/A Req.Hdr.cbReq = sizeof(Req);
4641N/A Req.szMsg[0] = '\0';
4641N/A RTTESTI_CHECK_RC(rc = SUPR3CallR0Service("tstR0ThreadPreemption", sizeof("tstR0ThreadPreemption") - 1,
4641N/A TSTR0THREADPREMEPTION_NESTED, 0, &Req.Hdr), VINF_SUCCESS);
4641N/A if (Req.szMsg[0] == '!')
4641N/A RTTestIFailed("%s", &Req.szMsg[1]);
4641N/A else if (Req.szMsg[0])
4641N/A RTTestIPrintf(RTTESTLVL_ALWAYS, "%s", Req.szMsg);
4641N/A
4641N/A /*
4641N/A * Done.
4641N/A */
4641N/A return RTTestSummaryAndDestroy(hTest);
4641N/A#endif
4641N/A}
4641N/A
4641N/A