VirtualBox

Changeset 6740 in vbox


Ignore:
Timestamp:
Feb 1, 2008 10:05:47 PM (17 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
27822
Message:

Removed VBox'isms and crt deps. Prefix output. Return a usable code.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/testcase/tstSemMutex.cpp

    r6739 r6740  
    3535#include <iprt/asm.h>
    3636
    37 #include <stdio.h>
    38 #include <stdlib.h>
    39 
    4037#define SECONDS 10
    4138
     
    4340static uint64_t   g_au64[10];
    4441static bool       g_fTerminate;
     42static bool       g_fYield = true;
    4543static uint32_t   g_cbConcurrent;
     44
     45static uint32_t volatile    g_cErrors;
     46
     47
     48int PrintError(const char *pszFormat, ...)
     49{
     50    ASMAtomicIncU32(&g_cErrors);
     51
     52    RTPrintf("tstSemMutex: FAILURE - ");
     53    va_list va;
     54    va_start(va, pszFormat);
     55    RTPrintfV(pszFormat, va);
     56    va_end(va);
     57
     58    return 1;
     59}
    4660
    4761
    4862int ThreadTest(RTTHREAD ThreadSelf, void *pvUser)
    4963{
    50     uint64_t *pu64 = (uint64_t*) pvUser;
     64    uint64_t *pu64 = (uint64_t *)pvUser;
    5165    for (;;)
    5266    {
     
    5468        if (RT_FAILURE(rc))
    5569        {
    56             RTPrintf("%x: RTSemMutexRequestNoResume failed with %Vrc\n", rc);
     70            PrintError("%x: RTSemMutexRequestNoResume failed with %Rrc\n", rc);
    5771            break;
    5872        }
    5973        if (ASMAtomicIncU32(&g_cbConcurrent) != 1)
    6074        {
    61             RTPrintf("g_cbConcurrent=%d after request!\n", g_cbConcurrent);
     75            PrintError("g_cbConcurrent=%d after request!\n", g_cbConcurrent);
    6276            break;
    6377        }
     
    6983
    7084        /*
    71          * Check for correctness: Give other threads a chance. If the implementation is 
     85         * Check for correctness: Give other threads a chance. If the implementation is
    7286         * correct, no other thread will be able to enter this lock now.
    7387         */
    74         RTThreadYield();
     88        if (g_fYield)
     89            RTThreadYield();
    7590        if (ASMAtomicDecU32(&g_cbConcurrent) != 0)
    7691        {
    77             RTPrintf("g_cbConcurrent=%d before release!\n", g_cbConcurrent);
     92            PrintError("g_cbConcurrent=%d before release!\n", g_cbConcurrent);
    7893            break;
    7994        }
     
    8196        if (RT_FAILURE(rc))
    8297        {
    83             RTPrintf("%x: RTSemMutexRelease failed with %Vrc\n", rc);
     98            PrintError("%x: RTSemMutexRelease failed with %Rrc\n", rc);
    8499            break;
    85100        }
     
    87102            break;
    88103    }
    89     RTPrintf("Thread %08x exited with %lld\n", ThreadSelf, *pu64);
     104    RTPrintf("tstSemMutex: Thread %08x exited with %lld\n", ThreadSelf, *pu64);
    90105    return VINF_SUCCESS;
    91106}
     
    95110    int rc;
    96111    unsigned u;
    97     RTTHREAD Thread[ELEMENTS(g_au64)];
     112    RTTHREAD aThreads[RT_ELEMENTS(g_au64)];
    98113
    99114    rc = RTR3Init(false, 0);
    100115    if (RT_FAILURE(rc))
    101116    {
    102         RTPrintf("RTR3Init failed (rc=%Vrc)\n", rc);
    103         exit(1);
     117        RTPrintf("tstSemMutex: RTR3Init failed (rc=%Rrc)\n", rc);
     118        return 1;
    104119    }
     120
     121
    105122    rc = RTSemMutexCreate(&g_mutex);
    106123    if (RT_FAILURE(rc))
    107124    {
    108         RTPrintf("RTSemMutexCreate failed (rc=%Vrc)\n", rc);
    109         exit(1);
     125        RTPrintf("tstSemMutex: RTSemMutexCreate failed (rc=%Rrc)\n", rc);
     126        return 1;
    110127    }
    111     for (u=0; u<ELEMENTS(g_au64); u++)
     128    for (u = 0; u < RT_ELEMENTS(g_au64); u++)
    112129    {
    113         rc = RTThreadCreate(&Thread[u], ThreadTest, &g_au64[u], 0, RTTHREADTYPE_DEFAULT, 0, "test");
     130        rc = RTThreadCreate(&aThreads[u], ThreadTest, &g_au64[u], 0, RTTHREADTYPE_DEFAULT, 0, "test");
    114131        if (RT_FAILURE(rc))
    115132        {
    116             RTPrintf("RTThreadCreate failed for thread %u (rc=%Vrc)\n", u, rc);
    117             exit(1);
     133            RTPrintf("tstSemMutex: RTThreadCreate failed for thread %u (rc=%Rrc)\n", u, rc);
     134            return 1;
    118135        }
    119136    }
    120137
    121     RTPrintf("%u Threads created. Waiting for %u seconds ...\n", ELEMENTS(g_au64), SECONDS);
     138    RTPrintf("tstSemMutex: %zu Threads created. Racing them for %u seconds (%s) ...\n",
     139             RT_ELEMENTS(g_au64), SECONDS, g_fYield ? "yielding" : "no yielding");
    122140    RTThreadSleep(SECONDS * 1000);
    123141    g_fTerminate = true;
     
    126144    RTThreadSleep(100);
    127145
    128     for (u=1; u<ELEMENTS(g_au64); u++)
     146    for (u = 1; u < RT_ELEMENTS(g_au64); u++)
    129147        g_au64[0] += g_au64[u];
     148    RTPrintf("tstSemMutex: Done. In total: %lld\n", g_au64[0]);
    130149
    131     RTPrintf("Done. In total: %lld\n", g_au64[0]);
     150
     151    if (!g_cErrors)
     152        RTPrintf("tstSemMutex: SUCCESS\n");
     153    else
     154        RTPrintf("tstSemMutex: FAILURE - %u errors\n", g_cErrors);
     155    return g_cErrors != 0;
    132156}
     157
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette