tstSemMutex.cpp revision e241ca160129baabbd80f3ad0eb2361ced172af6
/* $Id$ */
/** @file
* innotek Portable Runtime Testcase - Simple Semaphore Smoke Test.
*/
/*
* Copyright (C) 2006-2007 innotek GmbH
*
* 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/semaphore.h>
#include <iprt/initterm.h>
/*******************************************************************************
* Global Variables *
*******************************************************************************/
static bool volatile g_fTerminate;
static bool g_fYield;
static bool g_fQuiet;
static uint32_t volatile g_cbConcurrent;
int PrintError(const char *pszFormat, ...)
{
RTPrintf("tstSemMutex: FAILURE - ");
return 1;
}
{
for (;;)
{
if (RT_FAILURE(rc))
{
break;
}
{
break;
}
/*
* Check for fairness: The values of the threads should not differ too much
*/
(*pu64)++;
/*
* Check for correctness: Give other threads a chance. If the implementation is
* correct, no other thread will be able to enter this lock now.
*/
if (g_fYield)
if (ASMAtomicDecU32(&g_cbConcurrent) != 0)
{
break;
}
if (RT_FAILURE(rc))
{
break;
}
if (g_fTerminate)
break;
}
if (!g_fQuiet)
return VINF_SUCCESS;
}
{
int rc;
unsigned i;
/*
* Init globals.
*/
g_fTerminate = false;
if (RT_FAILURE(rc))
/*
* Create the threads and let them block on the mutex.
*/
if (RT_FAILURE(rc))
for (i = 0; i < cThreads; i++)
{
g_au64[i] = 0;
rc = RTThreadCreate(&aThreads[i], ThreadTest1, &g_au64[i], 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "test");
if (RT_FAILURE(rc))
}
if (!fQuiet)
RTPrintf("tstSemMutex: %zu Threads created. Racing them for %u seconds (%s) ...\n",
if (RT_FAILURE(rc))
ASMAtomicXchgBool(&g_fTerminate, true);
for (i = 0; i < cThreads; i++)
{
if (RT_FAILURE(rc))
}
if (RT_FAILURE(rc))
if (g_cErrors)
RTThreadSleep(100);
/*
* Collect and display the results.
*/
for (i = 1; i < cThreads; i++)
uint64_t MaxDiviation = 0;
for (i = 0; i < cThreads; i++)
{
RTPrintf("tstSemMutex: Warning! Thread %d diviates by more than 50%% - %llu (it) vs. %llu (avg)\n",
if (Delta > MaxDiviation)
}
RTPrintf("tstSemMutex: Threads: %u Total: %llu Per Sec: %llu Avg: %llu ns Max div: %llu%%\n",
);
return 0;
}
int main()
{
if (RT_FAILURE(rc))
{
return 1;
}
RTPrintf("tstSemMutex: TESTING...\n");
/* threads, seconds, yield, quiet */
RTPrintf("tstSemMutex: benchmarking... \n");
if (!g_cErrors)
RTPrintf("tstSemMutex: SUCCESS\n");
else
return g_cErrors != 0;
}