VirtualBox

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

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

fixed props.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.9 KB
Line 
1/* $Id: tstSemMutex.cpp 6739 2008-02-01 21:48:10Z 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#include <stdio.h>
38#include <stdlib.h>
39
40#define SECONDS 10
41
42static RTSEMMUTEX g_mutex;
43static uint64_t g_au64[10];
44static bool g_fTerminate;
45static uint32_t g_cbConcurrent;
46
47
48int ThreadTest(RTTHREAD ThreadSelf, void *pvUser)
49{
50 uint64_t *pu64 = (uint64_t*) pvUser;
51 for (;;)
52 {
53 int rc = RTSemMutexRequestNoResume(g_mutex, RT_INDEFINITE_WAIT);
54 if (RT_FAILURE(rc))
55 {
56 RTPrintf("%x: RTSemMutexRequestNoResume failed with %Vrc\n", rc);
57 break;
58 }
59 if (ASMAtomicIncU32(&g_cbConcurrent) != 1)
60 {
61 RTPrintf("g_cbConcurrent=%d after request!\n", g_cbConcurrent);
62 break;
63 }
64
65 /*
66 * Check for fairness: The values of the threads should not differ too much
67 */
68 (*pu64)++;
69
70 /*
71 * Check for correctness: Give other threads a chance. If the implementation is
72 * correct, no other thread will be able to enter this lock now.
73 */
74 RTThreadYield();
75 if (ASMAtomicDecU32(&g_cbConcurrent) != 0)
76 {
77 RTPrintf("g_cbConcurrent=%d before release!\n", g_cbConcurrent);
78 break;
79 }
80 rc = RTSemMutexRelease(g_mutex);
81 if (RT_FAILURE(rc))
82 {
83 RTPrintf("%x: RTSemMutexRelease failed with %Vrc\n", rc);
84 break;
85 }
86 if (g_fTerminate)
87 break;
88 }
89 RTPrintf("Thread %08x exited with %lld\n", ThreadSelf, *pu64);
90 return VINF_SUCCESS;
91}
92
93int main()
94{
95 int rc;
96 unsigned u;
97 RTTHREAD Thread[ELEMENTS(g_au64)];
98
99 rc = RTR3Init(false, 0);
100 if (RT_FAILURE(rc))
101 {
102 RTPrintf("RTR3Init failed (rc=%Vrc)\n", rc);
103 exit(1);
104 }
105 rc = RTSemMutexCreate(&g_mutex);
106 if (RT_FAILURE(rc))
107 {
108 RTPrintf("RTSemMutexCreate failed (rc=%Vrc)\n", rc);
109 exit(1);
110 }
111 for (u=0; u<ELEMENTS(g_au64); u++)
112 {
113 rc = RTThreadCreate(&Thread[u], ThreadTest, &g_au64[u], 0, RTTHREADTYPE_DEFAULT, 0, "test");
114 if (RT_FAILURE(rc))
115 {
116 RTPrintf("RTThreadCreate failed for thread %u (rc=%Vrc)\n", u, rc);
117 exit(1);
118 }
119 }
120
121 RTPrintf("%u Threads created. Waiting for %u seconds ...\n", ELEMENTS(g_au64), SECONDS);
122 RTThreadSleep(SECONDS * 1000);
123 g_fTerminate = true;
124 RTThreadSleep(100);
125 RTSemMutexDestroy(g_mutex);
126 RTThreadSleep(100);
127
128 for (u=1; u<ELEMENTS(g_au64); u++)
129 g_au64[0] += g_au64[u];
130
131 RTPrintf("Done. In total: %lld\n", g_au64[0]);
132}
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