VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstSemEvent.cpp@ 25791

Last change on this file since 25791 was 25614, checked in by vboxsync, 15 years ago

iprt,pdmcritsect: More lock validator refactoring and debugging. Added hooks to semrw-generic.cpp. (Everything is still disabled.)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.2 KB
Line 
1/* $Id: tstSemEvent.cpp 25614 2010-01-01 14:19:06Z vboxsync $ */
2/** @file
3 * IPRT Testcase - Event Semaphore Test.
4 */
5
6/*
7 * Copyright (C) 2009 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31/*******************************************************************************
32* Header Files *
33*******************************************************************************/
34#include <iprt/semaphore.h>
35#include <iprt/string.h>
36#include <iprt/thread.h>
37#include <iprt/stream.h>
38#include <iprt/time.h>
39#include <iprt/initterm.h>
40#include <iprt/rand.h>
41#include <iprt/asm.h>
42#include <iprt/assert.h>
43
44
45/*******************************************************************************
46* Global Variables *
47*******************************************************************************/
48static RTSEMEVENTMULTI g_hSemEM = NIL_RTSEMEVENTMULTI;
49static uint32_t volatile g_cErrors;
50
51
52int PrintError(const char *pszFormat, ...)
53{
54 ASMAtomicIncU32(&g_cErrors);
55
56 RTPrintf("tstSemEvent: FAILURE - ");
57 va_list va;
58 va_start(va, pszFormat);
59 RTPrintfV(pszFormat, va);
60 va_end(va);
61
62 return 1;
63}
64
65
66int ThreadTest1(RTTHREAD ThreadSelf, void *pvUser)
67{
68 int rc;
69 rc = RTSemEventMultiWait(g_hSemEM, 1000);
70 if (rc != VERR_TIMEOUT)
71 {
72 PrintError("Thread 1: unexpected result of first RTSemEventMultiWait %Rrc\n", rc);
73 return VINF_SUCCESS;
74 }
75
76 rc = RTSemEventMultiWait(g_hSemEM, 1000);
77 if (RT_FAILURE(rc))
78 {
79 PrintError("Thread 1: unexpected result of second RTSemEventMultiWait %Rrc\n", rc);
80 return VINF_SUCCESS;
81 }
82
83 RTPrintf("tstSemEvent: Thread 1 normal exit...\n");
84 return VINF_SUCCESS;
85}
86
87
88int ThreadTest2(RTTHREAD ThreadSelf, void *pvUser)
89{
90 int rc;
91 rc = RTSemEventMultiWait(g_hSemEM, RT_INDEFINITE_WAIT);
92 if (RT_FAILURE(rc))
93 {
94 PrintError("Thread 2: unexpected result of RTSemEventMultiWait %Rrc\n", rc);
95 return VINF_SUCCESS;
96 }
97
98 RTPrintf("tstSemEvent: Thread 2 normal exit...\n");
99 return VINF_SUCCESS;
100}
101
102
103static int Test1()
104{
105 int rc;
106 RTTHREAD Thread1, Thread2;
107
108 rc = RTSemEventMultiCreate(&g_hSemEM);
109 if (RT_FAILURE(rc))
110 return PrintError("RTSemEventMultiCreate failed (rc=%Rrc)\n", rc);
111
112 /*
113 * Create the threads and let them block on the event multi semaphore.
114 */
115 rc = RTSemEventMultiReset(g_hSemEM);
116 if (RT_FAILURE(rc))
117 return PrintError("RTSemEventMultiReset failed (rc=%Rrc)\n", rc);
118
119 rc = RTThreadCreate(&Thread2, ThreadTest2, NULL, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "test2");
120 if (RT_FAILURE(rc))
121 return PrintError("RTThreadCreate failed for thread 2 (rc=%Rrc)\n", rc);
122 RTThreadSleep(100);
123
124 rc = RTThreadCreate(&Thread1, ThreadTest1, NULL, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "test1");
125 if (RT_FAILURE(rc))
126 return PrintError("RTThreadCreate failed for thread 1 (rc=%Rrc)\n", rc);
127
128 /* Force first thread (which has a timeout of 1 second) to timeout in the
129 * first wait, and the second wait will succeed. */
130 RTThreadSleep(1500);
131 rc = RTSemEventMultiSignal(g_hSemEM);
132 if (RT_FAILURE(rc))
133 PrintError("RTSemEventMultiSignal failed (rc=%Rrc)\n", rc);
134
135 rc = RTThreadWait(Thread1, 5000, NULL);
136 if (RT_FAILURE(rc))
137 PrintError("RTThreadWait failed for thread 1 (rc=%Rrc)\n", rc);
138
139 rc = RTThreadWait(Thread2, 5000, NULL);
140 if (RT_FAILURE(rc))
141 PrintError("RTThreadWait failed for thread 2 (rc=%Rrc)\n", rc);
142
143 rc = RTSemEventMultiDestroy(g_hSemEM);
144 if (RT_FAILURE(rc))
145 PrintError("RTSemEventMultiDestroy failed - %Rrc\n", rc);
146 g_hSemEM = NIL_RTSEMEVENTMULTI;
147 if (g_cErrors)
148 RTThreadSleep(100);
149 return 0;
150}
151
152
153int main(int argc, char **argv)
154{
155 int rc = RTR3Init();
156 if (RT_FAILURE(rc))
157 {
158 RTPrintf("tstSemEvent: RTR3Init failed (rc=%Rrc)\n", rc);
159 return 1;
160 }
161 RTPrintf("tstSemEvent: TESTING...\n");
162 Test1();
163
164 if (!g_cErrors)
165 RTPrintf("tstSemEvent: SUCCESS\n");
166 else
167 RTPrintf("tstSemEvent: FAILURE - %u errors\n", g_cErrors);
168 return g_cErrors != 0;
169}
170
Note: See TracBrowser for help on using the repository browser.

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