VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstSemMutex.cpp@ 6740

Last change on this file since 6740 was 6740, checked in by vboxsync, 17 years ago

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

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.5 KB
Line 
1/* $Id: tstSemMutex.cpp 6740 2008-02-01 22:05:47Z vboxsync $ */
2/** @file
3 * innotek Portable Runtime Testcase - Simple Semaphore Smoke Test.
4 */
5
6/*
7 * Copyright (C) 2006-2007 innotek GmbH
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27/*******************************************************************************
28* Header Files *
29*******************************************************************************/
30#include <iprt/semaphore.h>
31#include <iprt/string.h>
32#include <iprt/thread.h>
33#include <iprt/stream.h>
34#include <iprt/initterm.h>
35#include <iprt/asm.h>
36
37#define SECONDS 10
38
39static RTSEMMUTEX g_mutex;
40static uint64_t g_au64[10];
41static bool g_fTerminate;
42static bool g_fYield = true;
43static 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}
60
61
62int ThreadTest(RTTHREAD ThreadSelf, void *pvUser)
63{
64 uint64_t *pu64 = (uint64_t *)pvUser;
65 for (;;)
66 {
67 int rc = RTSemMutexRequestNoResume(g_mutex, RT_INDEFINITE_WAIT);
68 if (RT_FAILURE(rc))
69 {
70 PrintError("%x: RTSemMutexRequestNoResume failed with %Rrc\n", rc);
71 break;
72 }
73 if (ASMAtomicIncU32(&g_cbConcurrent) != 1)
74 {
75 PrintError("g_cbConcurrent=%d after request!\n", g_cbConcurrent);
76 break;
77 }
78
79 /*
80 * Check for fairness: The values of the threads should not differ too much
81 */
82 (*pu64)++;
83
84 /*
85 * Check for correctness: Give other threads a chance. If the implementation is
86 * correct, no other thread will be able to enter this lock now.
87 */
88 if (g_fYield)
89 RTThreadYield();
90 if (ASMAtomicDecU32(&g_cbConcurrent) != 0)
91 {
92 PrintError("g_cbConcurrent=%d before release!\n", g_cbConcurrent);
93 break;
94 }
95 rc = RTSemMutexRelease(g_mutex);
96 if (RT_FAILURE(rc))
97 {
98 PrintError("%x: RTSemMutexRelease failed with %Rrc\n", rc);
99 break;
100 }
101 if (g_fTerminate)
102 break;
103 }
104 RTPrintf("tstSemMutex: Thread %08x exited with %lld\n", ThreadSelf, *pu64);
105 return VINF_SUCCESS;
106}
107
108int main()
109{
110 int rc;
111 unsigned u;
112 RTTHREAD aThreads[RT_ELEMENTS(g_au64)];
113
114 rc = RTR3Init(false, 0);
115 if (RT_FAILURE(rc))
116 {
117 RTPrintf("tstSemMutex: RTR3Init failed (rc=%Rrc)\n", rc);
118 return 1;
119 }
120
121
122 rc = RTSemMutexCreate(&g_mutex);
123 if (RT_FAILURE(rc))
124 {
125 RTPrintf("tstSemMutex: RTSemMutexCreate failed (rc=%Rrc)\n", rc);
126 return 1;
127 }
128 for (u = 0; u < RT_ELEMENTS(g_au64); u++)
129 {
130 rc = RTThreadCreate(&aThreads[u], ThreadTest, &g_au64[u], 0, RTTHREADTYPE_DEFAULT, 0, "test");
131 if (RT_FAILURE(rc))
132 {
133 RTPrintf("tstSemMutex: RTThreadCreate failed for thread %u (rc=%Rrc)\n", u, rc);
134 return 1;
135 }
136 }
137
138 RTPrintf("tstSemMutex: %zu Threads created. Racing them for %u seconds (%s) ...\n",
139 RT_ELEMENTS(g_au64), SECONDS, g_fYield ? "yielding" : "no yielding");
140 RTThreadSleep(SECONDS * 1000);
141 g_fTerminate = true;
142 RTThreadSleep(100);
143 RTSemMutexDestroy(g_mutex);
144 RTThreadSleep(100);
145
146 for (u = 1; u < RT_ELEMENTS(g_au64); u++)
147 g_au64[0] += g_au64[u];
148 RTPrintf("tstSemMutex: Done. In total: %lld\n", g_au64[0]);
149
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;
156}
157
Note: See TracBrowser for help on using the repository browser.

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