tstOnce.cpp revision 33b0e5fd502b10034575ffa7c8a30c6816222ce2
/* $Id$ */
/** @file
* IPRT Testcase - RTOnce.
*/
/*
* Copyright (C) 2008 Sun Microsystems, Inc.
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <iprt/initterm.h>
#include <iprt/semaphore.h>
/*******************************************************************************
* Global Variables *
*******************************************************************************/
static int g_cErrors = 0;
static bool g_fOnceCB1 = false;
static uint32_t volatile g_cOnceCB2 = 0;
{
if (g_fOnceCB1)
return VERR_WRONG_ORDER;
{
g_cErrors++;
return VERR_INVALID_PARAMETER;
}
return VINF_SUCCESS;
}
{
{
RTPrintf("tstOnce: ERROR - Once2CB: g_cOnceCB2 not zero!\n");
g_cErrors++;
return VERR_WRONG_ORDER;
}
{
g_cErrors++;
return VERR_INVALID_PARAMETER;
}
return VINF_SUCCESS;
}
{
if (RT_FAILURE(rc))
return rc;
}
int main()
{
RTR3Init();
RTPrintf("tstOnce: SUCCESS\n");
/*
* Just a simple testcase.
*/
g_fOnceCB1 = false;
if (rc != VINF_SUCCESS)
g_fOnceCB1 = false;
if (rc != VINF_SUCCESS)
/*
* Throw a bunch of threads up against a init once thing.
*/
/* create the semaphore they'll be waiting on. */
if (RT_FAILURE(rc))
{
return 1;
}
/* create the threads */
for (unsigned i = 0; i < RT_ELEMENTS(aThreads); i++)
{
char szName[16];
int rc = RTThreadCreate(&aThreads[i], Once2Thread, NULL, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, szName);
if (RT_FAILURE(rc))
{
RTPrintf("tstOnce: ERROR - failed to create thread #%d\n", i);
g_cErrors++;
}
}
/* kick them off and yield */
if (RT_FAILURE(rc))
{
return 1;
}
/* wait for all of them to finish up, 30 seconds each. */
for (unsigned i = 0; i < RT_ELEMENTS(aThreads); i++)
if (aThreads[i] != NIL_RTTHREAD)
{
int rc2;
if (RT_FAILURE(rc))
{
g_cErrors++;
}
else if (RT_FAILURE(rc2))
{
g_cErrors++;
}
}
/*
* Summary.
*/
if (!g_cErrors)
RTPrintf("tstOnce: SUCCESS\n");
else
return !!g_cErrors;
}