VirtualBox

source: vbox/trunk/src/VBox/HostDrivers/Support/testcase/tstSupSem.cpp@ 25308

Last change on this file since 25308 was 20903, checked in by vboxsync, 15 years ago

SUP: Added a testcase to make sure threads exit when the process exits.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.2 KB
Line 
1/* $Id: tstSupSem.cpp 20903 2009-06-24 19:32:12Z vboxsync $ */
2/** @file
3 * Support Library Testcase - Ring-3 Semaphore interface.
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/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#include <VBox/sup.h>
36
37#include <VBox/param.h>
38#include <iprt/err.h>
39#include <iprt/initterm.h>
40#include <iprt/stream.h>
41#include <iprt/test.h>
42#include <iprt/thread.h>
43
44
45/*******************************************************************************
46* Structures and Typedefs *
47*******************************************************************************/
48static PSUPDRVSESSION g_pSession;
49static RTTEST g_hTest;
50static uint32_t g_cMillies; /* Used by the interruptible tests. */
51
52
53
54static DECLCALLBACK(int) tstSupSemInterruptibleSRE(RTTHREAD hSelf, void *pvUser)
55{
56 SUPSEMEVENT hEvent = (SUPSEMEVENT)pvUser;
57 RTThreadUserSignal(hSelf);
58 return SUPSemEventWaitNoResume(g_pSession, hEvent, g_cMillies);
59}
60
61
62static DECLCALLBACK(int) tstSupSemInterruptibleMRE(RTTHREAD hSelf, void *pvUser)
63{
64 SUPSEMEVENTMULTI hEventMulti = (SUPSEMEVENTMULTI)pvUser;
65 RTThreadUserSignal(hSelf);
66 return SUPSemEventMultiWaitNoResume(g_pSession, hEventMulti, g_cMillies);
67}
68
69
70int main(int argc, char **argv)
71{
72 /*
73 * Init.
74 */
75 int rc = RTR3InitAndSUPLib();
76 if (RT_FAILURE(rc))
77 {
78 RTPrintf("tstSupSem: fatal error: RTR3InitAndSUPLib failed with rc=%Rrc\n", rc);
79 return 1;
80 }
81
82 RTTEST hTest;
83 rc = RTTestCreate("tstSupSem", &hTest);
84 if (RT_FAILURE(rc))
85 {
86 RTPrintf("tstSupSem: fatal error: RTTestCreate failed with rc=%Rrc\n", rc);
87 return 1;
88 }
89 g_hTest = hTest;
90
91 PSUPDRVSESSION pSession;
92 rc = SUPR3Init(&pSession);
93 if (RT_FAILURE(rc))
94 {
95 RTTestFailed(hTest, "SUPR3Init failed with rc=%Rrc\n", rc);
96 return RTTestSummaryAndDestroy(hTest);
97 }
98 g_pSession = pSession;
99 RTTestBanner(hTest);
100
101 /*
102 * Basic API checks.
103 */
104 RTTestSub(hTest, "Single Release Event (SRE) API");
105 SUPSEMEVENT hEvent = NIL_SUPSEMEVENT;
106 RTTESTI_CHECK_RC(SUPSemEventCreate(pSession, &hEvent), VINF_SUCCESS);
107 RTTESTI_CHECK_RC(SUPSemEventWaitNoResume(pSession, hEvent, 0), VERR_TIMEOUT);
108 RTTESTI_CHECK_RC(SUPSemEventWaitNoResume(pSession, hEvent,20), VERR_TIMEOUT);
109 RTTESTI_CHECK_RC(SUPSemEventSignal(pSession, hEvent), VINF_SUCCESS);
110 RTTESTI_CHECK_RC(SUPSemEventWaitNoResume(pSession, hEvent, 0), VINF_SUCCESS);
111 RTTESTI_CHECK_RC(SUPSemEventSignal(pSession, hEvent), VINF_SUCCESS);
112 RTTESTI_CHECK_RC(SUPSemEventWaitNoResume(pSession, hEvent,20), VINF_SUCCESS);
113 RTTESTI_CHECK_RC(SUPSemEventSignal(pSession, hEvent), VINF_SUCCESS);
114 RTTESTI_CHECK_RC(SUPSemEventSignal(pSession, hEvent), VINF_SUCCESS);
115 RTTESTI_CHECK_RC(SUPSemEventWaitNoResume(pSession, hEvent, 0), VINF_SUCCESS);
116 RTTESTI_CHECK_RC(SUPSemEventWaitNoResume(pSession, hEvent, 0), VERR_TIMEOUT);
117 RTTESTI_CHECK_RC(SUPSemEventWaitNoResume(pSession, hEvent,20), VERR_TIMEOUT);
118 RTTESTI_CHECK_RC(SUPSemEventClose(pSession, hEvent), VINF_OBJECT_DESTROYED);
119 RTTESTI_CHECK_RC(SUPSemEventClose(pSession, hEvent), VERR_INVALID_HANDLE);
120 RTTESTI_CHECK_RC(SUPSemEventClose(pSession, NIL_SUPSEMEVENT), VINF_SUCCESS);
121
122 RTTestSub(hTest, "Multiple Release Event (MRE) API");
123 SUPSEMEVENTMULTI hEventMulti = NIL_SUPSEMEVENT;
124 RTTESTI_CHECK_RC(SUPSemEventMultiCreate(pSession, &hEventMulti), VINF_SUCCESS);
125 RTTESTI_CHECK_RC(SUPSemEventMultiWaitNoResume(pSession, hEventMulti, 0), VERR_TIMEOUT);
126 RTTESTI_CHECK_RC(SUPSemEventMultiWaitNoResume(pSession, hEventMulti,20), VERR_TIMEOUT);
127 RTTESTI_CHECK_RC(SUPSemEventMultiSignal(pSession, hEventMulti), VINF_SUCCESS);
128 RTTESTI_CHECK_RC(SUPSemEventMultiWaitNoResume(pSession, hEventMulti, 0), VINF_SUCCESS);
129 RTTESTI_CHECK_RC(SUPSemEventMultiWaitNoResume(pSession, hEventMulti, 0), VINF_SUCCESS);
130 RTTESTI_CHECK_RC(SUPSemEventMultiWaitNoResume(pSession, hEventMulti, 0), VINF_SUCCESS);
131 RTTESTI_CHECK_RC(SUPSemEventMultiSignal(pSession, hEventMulti), VINF_SUCCESS);
132 RTTESTI_CHECK_RC(SUPSemEventMultiSignal(pSession, hEventMulti), VINF_SUCCESS);
133 RTTESTI_CHECK_RC(SUPSemEventMultiWaitNoResume(pSession, hEventMulti, 0), VINF_SUCCESS);
134 RTTESTI_CHECK_RC(SUPSemEventMultiReset(pSession, hEventMulti), VINF_SUCCESS);
135 RTTESTI_CHECK_RC(SUPSemEventMultiWaitNoResume(pSession, hEventMulti, 0), VERR_TIMEOUT);
136 RTTESTI_CHECK_RC(SUPSemEventMultiWaitNoResume(pSession, hEventMulti,20), VERR_TIMEOUT);
137 RTTESTI_CHECK_RC(SUPSemEventMultiSignal(pSession, hEventMulti), VINF_SUCCESS);
138 RTTESTI_CHECK_RC(SUPSemEventMultiWaitNoResume(pSession, hEventMulti, 0), VINF_SUCCESS);
139 RTTESTI_CHECK_RC(SUPSemEventMultiClose(pSession, hEventMulti), VINF_OBJECT_DESTROYED);
140 RTTESTI_CHECK_RC(SUPSemEventMultiClose(pSession, hEventMulti), VERR_INVALID_HANDLE);
141 RTTESTI_CHECK_RC(SUPSemEventMultiClose(pSession, NIL_SUPSEMEVENTMULTI), VINF_SUCCESS);
142
143#if !defined(RT_OS_OS2) && !defined(RT_OS_WINDOWS)
144 RTTestSub(hTest, "SRE Interruptibility");
145 RTTESTI_CHECK_RC(SUPSemEventCreate(pSession, &hEvent), VINF_SUCCESS);
146 g_cMillies = RT_INDEFINITE_WAIT;
147 RTTHREAD hThread = NIL_RTTHREAD;
148 RTTESTI_CHECK_RC(RTThreadCreate(&hThread, tstSupSemInterruptibleSRE, (void *)hEvent, 0, RTTHREADTYPE_TIMER, RTTHREADFLAGS_WAITABLE, "IntSRE"), VINF_SUCCESS);
149 RTTESTI_CHECK_RC(RTThreadUserWait(hThread, 60*1000), VINF_SUCCESS);
150 RTThreadSleep(120);
151 RTThreadPoke(hThread);
152 int rcThread = VINF_SUCCESS;
153 RTTESTI_CHECK_RC(RTThreadWait(hThread, 60*1000, &rcThread), VINF_SUCCESS);
154 RTTESTI_CHECK_RC(rcThread, VERR_INTERRUPTED);
155 RTTESTI_CHECK_RC(SUPSemEventClose(pSession, hEvent), VINF_OBJECT_DESTROYED);
156
157 RTTESTI_CHECK_RC(SUPSemEventCreate(pSession, &hEvent), VINF_SUCCESS);
158 g_cMillies = 120*1000;
159 hThread = NIL_RTTHREAD;
160 RTTESTI_CHECK_RC(RTThreadCreate(&hThread, tstSupSemInterruptibleSRE, (void *)hEvent, 0, RTTHREADTYPE_TIMER, RTTHREADFLAGS_WAITABLE, "IntSRE"), VINF_SUCCESS);
161 RTTESTI_CHECK_RC(RTThreadUserWait(hThread, 60*1000), VINF_SUCCESS);
162 RTThreadSleep(120);
163 RTThreadPoke(hThread);
164 rcThread = VINF_SUCCESS;
165 RTTESTI_CHECK_RC(RTThreadWait(hThread, 60*1000, &rcThread), VINF_SUCCESS);
166 RTTESTI_CHECK_RC(rcThread, VERR_INTERRUPTED);
167 RTTESTI_CHECK_RC(SUPSemEventClose(pSession, hEvent), VINF_OBJECT_DESTROYED);
168
169
170 RTTestSub(hTest, "MRE Interruptibility");
171 RTTESTI_CHECK_RC(SUPSemEventMultiCreate(pSession, &hEventMulti), VINF_SUCCESS);
172 g_cMillies = RT_INDEFINITE_WAIT;
173 hThread = NIL_RTTHREAD;
174 RTTESTI_CHECK_RC(RTThreadCreate(&hThread, tstSupSemInterruptibleMRE, (void *)hEventMulti, 0, RTTHREADTYPE_TIMER, RTTHREADFLAGS_WAITABLE, "IntMRE"), VINF_SUCCESS);
175 RTTESTI_CHECK_RC(RTThreadUserWait(hThread, 60*1000), VINF_SUCCESS);
176 RTThreadSleep(120);
177 RTThreadPoke(hThread);
178 rcThread = VINF_SUCCESS;
179 RTTESTI_CHECK_RC(RTThreadWait(hThread, 60*1000, &rcThread), VINF_SUCCESS);
180 RTTESTI_CHECK_RC(rcThread, VERR_INTERRUPTED);
181 RTTESTI_CHECK_RC(SUPSemEventMultiClose(pSession, hEventMulti), VINF_OBJECT_DESTROYED);
182
183 RTTESTI_CHECK_RC(SUPSemEventMultiCreate(pSession, &hEventMulti), VINF_SUCCESS);
184 g_cMillies = 120*1000;
185 hThread = NIL_RTTHREAD;
186 RTTESTI_CHECK_RC(RTThreadCreate(&hThread, tstSupSemInterruptibleMRE, (void *)hEventMulti, 0, RTTHREADTYPE_TIMER, RTTHREADFLAGS_WAITABLE, "IntMRE"), VINF_SUCCESS);
187 RTTESTI_CHECK_RC(RTThreadUserWait(hThread, 60*1000), VINF_SUCCESS);
188 RTThreadSleep(120);
189 RTThreadPoke(hThread);
190 rcThread = VINF_SUCCESS;
191 RTTESTI_CHECK_RC(RTThreadWait(hThread, 60*1000, &rcThread), VINF_SUCCESS);
192 RTTESTI_CHECK_RC(rcThread, VERR_INTERRUPTED);
193 RTTESTI_CHECK_RC(SUPSemEventMultiClose(pSession, hEventMulti), VINF_OBJECT_DESTROYED);
194#endif /* !OS2 && !WINDOWS */
195
196 /*
197 * Done.
198 */
199 return RTTestSummaryAndDestroy(hTest);
200}
201
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