VirtualBox

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

Last change on this file since 29279 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.0 KB
Line 
1/* $Id: tstSemEvent.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
2/** @file
3 * IPRT Testcase - Event Semaphore Test.
4 */
5
6/*
7 * Copyright (C) 2009 Oracle Corporation
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/time.h>
35#include <iprt/initterm.h>
36#include <iprt/rand.h>
37#include <iprt/asm.h>
38#include <iprt/assert.h>
39
40
41/*******************************************************************************
42* Global Variables *
43*******************************************************************************/
44static RTSEMEVENTMULTI g_hSemEM = NIL_RTSEMEVENTMULTI;
45static uint32_t volatile g_cErrors;
46
47
48int PrintError(const char *pszFormat, ...)
49{
50 ASMAtomicIncU32(&g_cErrors);
51
52 RTPrintf("tstSemEvent: 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 ThreadTest1(RTTHREAD ThreadSelf, void *pvUser)
63{
64 int rc;
65 rc = RTSemEventMultiWait(g_hSemEM, 1000);
66 if (rc != VERR_TIMEOUT)
67 {
68 PrintError("Thread 1: unexpected result of first RTSemEventMultiWait %Rrc\n", rc);
69 return VINF_SUCCESS;
70 }
71
72 rc = RTSemEventMultiWait(g_hSemEM, 1000);
73 if (RT_FAILURE(rc))
74 {
75 PrintError("Thread 1: unexpected result of second RTSemEventMultiWait %Rrc\n", rc);
76 return VINF_SUCCESS;
77 }
78
79 RTPrintf("tstSemEvent: Thread 1 normal exit...\n");
80 return VINF_SUCCESS;
81}
82
83
84int ThreadTest2(RTTHREAD ThreadSelf, void *pvUser)
85{
86 int rc;
87 rc = RTSemEventMultiWait(g_hSemEM, RT_INDEFINITE_WAIT);
88 if (RT_FAILURE(rc))
89 {
90 PrintError("Thread 2: unexpected result of RTSemEventMultiWait %Rrc\n", rc);
91 return VINF_SUCCESS;
92 }
93
94 RTPrintf("tstSemEvent: Thread 2 normal exit...\n");
95 return VINF_SUCCESS;
96}
97
98
99static int Test1()
100{
101 int rc;
102 RTTHREAD Thread1, Thread2;
103
104 rc = RTSemEventMultiCreate(&g_hSemEM);
105 if (RT_FAILURE(rc))
106 return PrintError("RTSemEventMultiCreate failed (rc=%Rrc)\n", rc);
107
108 /*
109 * Create the threads and let them block on the event multi semaphore.
110 */
111 rc = RTSemEventMultiReset(g_hSemEM);
112 if (RT_FAILURE(rc))
113 return PrintError("RTSemEventMultiReset failed (rc=%Rrc)\n", rc);
114
115 rc = RTThreadCreate(&Thread2, ThreadTest2, NULL, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "test2");
116 if (RT_FAILURE(rc))
117 return PrintError("RTThreadCreate failed for thread 2 (rc=%Rrc)\n", rc);
118 RTThreadSleep(100);
119
120 rc = RTThreadCreate(&Thread1, ThreadTest1, NULL, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "test1");
121 if (RT_FAILURE(rc))
122 return PrintError("RTThreadCreate failed for thread 1 (rc=%Rrc)\n", rc);
123
124 /* Force first thread (which has a timeout of 1 second) to timeout in the
125 * first wait, and the second wait will succeed. */
126 RTThreadSleep(1500);
127 rc = RTSemEventMultiSignal(g_hSemEM);
128 if (RT_FAILURE(rc))
129 PrintError("RTSemEventMultiSignal failed (rc=%Rrc)\n", rc);
130
131 rc = RTThreadWait(Thread1, 5000, NULL);
132 if (RT_FAILURE(rc))
133 PrintError("RTThreadWait failed for thread 1 (rc=%Rrc)\n", rc);
134
135 rc = RTThreadWait(Thread2, 5000, NULL);
136 if (RT_FAILURE(rc))
137 PrintError("RTThreadWait failed for thread 2 (rc=%Rrc)\n", rc);
138
139 rc = RTSemEventMultiDestroy(g_hSemEM);
140 if (RT_FAILURE(rc))
141 PrintError("RTSemEventMultiDestroy failed - %Rrc\n", rc);
142 g_hSemEM = NIL_RTSEMEVENTMULTI;
143 if (g_cErrors)
144 RTThreadSleep(100);
145 return 0;
146}
147
148
149int main(int argc, char **argv)
150{
151 int rc = RTR3Init();
152 if (RT_FAILURE(rc))
153 {
154 RTPrintf("tstSemEvent: RTR3Init failed (rc=%Rrc)\n", rc);
155 return 1;
156 }
157 RTPrintf("tstSemEvent: TESTING...\n");
158 Test1();
159
160 if (!g_cErrors)
161 RTPrintf("tstSemEvent: SUCCESS\n");
162 else
163 RTPrintf("tstSemEvent: FAILURE - %u errors\n", g_cErrors);
164 return g_cErrors != 0;
165}
166
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