tstOnce.cpp revision a438caaf732f7839dc66b4f8dad672527845a003
/* $Id$ */
/** @file
* IPRT Testcase - RTOnce.
*/
/*
* Copyright (C) 2008 Oracle Corporation
*
* 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.
*/
/*******************************************************************************
* 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_cOnce2CB = 0;
static bool volatile g_fOnce2Ready = false;
{
if (g_fOnceCB1)
return VERR_WRONG_ORDER;
if (pvUser != (void *)1)
{
g_cErrors++;
return VERR_INVALID_PARAMETER;
}
return VINF_SUCCESS;
}
{
{
RTPrintf("tstOnce: ERROR - Once2CB: g_cOnce2CB not zero!\n");
g_cErrors++;
return VERR_WRONG_ORDER;
}
if (pvUser != (void *)42)
{
g_cErrors++;
return VERR_INVALID_PARAMETER;
}
RTThreadSleep(2);
ASMAtomicWriteBool(&g_fOnce2Ready, true);
return VINF_SUCCESS;
}
{
if (RT_FAILURE(rc))
return rc;
if (RT_SUCCESS(rc))
{
if (!ASMAtomicUoReadBool(&g_fOnce2Ready))
{
RTPrintf("tstOnce: ERROR - Once2CB: Not initialized!\n");
g_cErrors++;
}
}
return rc;
}
int main()
{
/*
* Just a simple testcase.
*/
RTPrintf("tstOnce: TESTING - smoke...\n");
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.
*/
RTPrintf("tstOnce: TESTING - bunch of threads...\n");
/* 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];
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;
}