1 | /* $Id: tstOnce.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - RTOnce.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2008 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/once.h>
|
---|
31 | #include <iprt/stream.h>
|
---|
32 | #include <iprt/initterm.h>
|
---|
33 | #include <iprt/thread.h>
|
---|
34 | #include <iprt/semaphore.h>
|
---|
35 | #include <iprt/string.h>
|
---|
36 | #include <iprt/err.h>
|
---|
37 | #include <iprt/asm.h>
|
---|
38 |
|
---|
39 |
|
---|
40 | /*******************************************************************************
|
---|
41 | * Global Variables *
|
---|
42 | *******************************************************************************/
|
---|
43 | static int g_cErrors = 0;
|
---|
44 | static bool g_fOnceCB1 = false;
|
---|
45 | static uint32_t volatile g_cOnce2CB = 0;
|
---|
46 | static bool volatile g_fOnce2Ready = false;
|
---|
47 | static RTONCE g_Once2 = RTONCE_INITIALIZER;
|
---|
48 | static RTSEMEVENTMULTI g_hEventMulti = NIL_RTSEMEVENTMULTI;
|
---|
49 |
|
---|
50 |
|
---|
51 | static DECLCALLBACK(int) Once1CB(void *pvUser1, void *pvUser2)
|
---|
52 | {
|
---|
53 | if (g_fOnceCB1)
|
---|
54 | return VERR_WRONG_ORDER;
|
---|
55 | if (pvUser1 != (void *)1 || pvUser2 != (void *)42)
|
---|
56 | {
|
---|
57 | RTPrintf("tstOnce: ERROR - Once1CB: pvUser1=%p pvUser2=%p!\n", pvUser1, pvUser2);
|
---|
58 | g_cErrors++;
|
---|
59 | return VERR_INVALID_PARAMETER;
|
---|
60 | }
|
---|
61 | return VINF_SUCCESS;
|
---|
62 | }
|
---|
63 |
|
---|
64 |
|
---|
65 | static DECLCALLBACK(int) Once2CB(void *pvUser1, void *pvUser2)
|
---|
66 | {
|
---|
67 | if (ASMAtomicIncU32(&g_cOnce2CB) != 1)
|
---|
68 | {
|
---|
69 | RTPrintf("tstOnce: ERROR - Once2CB: g_cOnce2CB not zero!\n");
|
---|
70 | g_cErrors++;
|
---|
71 | return VERR_WRONG_ORDER;
|
---|
72 | }
|
---|
73 | if (pvUser1 != (void *)42 || pvUser2 != (void *)1)
|
---|
74 | {
|
---|
75 | RTPrintf("tstOnce: ERROR - Once2CB: pvUser1=%p pvUser2=%p!\n", pvUser1, pvUser2);
|
---|
76 | g_cErrors++;
|
---|
77 | return VERR_INVALID_PARAMETER;
|
---|
78 | }
|
---|
79 | RTThreadSleep(2);
|
---|
80 | Assert(!g_fOnce2Ready);
|
---|
81 | ASMAtomicWriteBool(&g_fOnce2Ready, true);
|
---|
82 | return VINF_SUCCESS;
|
---|
83 | }
|
---|
84 |
|
---|
85 | static DECLCALLBACK(int) Once2Thread(RTTHREAD hThread, void *pvUser)
|
---|
86 | {
|
---|
87 | NOREF(hThread); NOREF(pvUser);
|
---|
88 |
|
---|
89 | int rc = RTSemEventMultiWait(g_hEventMulti, RT_INDEFINITE_WAIT);
|
---|
90 | if (RT_FAILURE(rc))
|
---|
91 | return rc;
|
---|
92 | rc = RTOnce(&g_Once2, Once2CB, (void *)42, (void *)1);
|
---|
93 | if (RT_SUCCESS(rc))
|
---|
94 | {
|
---|
95 | if (!ASMAtomicUoReadBool(&g_fOnce2Ready))
|
---|
96 | {
|
---|
97 | RTPrintf("tstOnce: ERROR - Once2CB: Not initialized!\n");
|
---|
98 | g_cErrors++;
|
---|
99 | }
|
---|
100 | }
|
---|
101 | return rc;
|
---|
102 | }
|
---|
103 |
|
---|
104 |
|
---|
105 | int main()
|
---|
106 | {
|
---|
107 | RTR3Init();
|
---|
108 |
|
---|
109 | /*
|
---|
110 | * Just a simple testcase.
|
---|
111 | */
|
---|
112 | RTPrintf("tstOnce: TESTING - smoke...\n");
|
---|
113 | RTONCE Once1 = RTONCE_INITIALIZER;
|
---|
114 | g_fOnceCB1 = false;
|
---|
115 | int rc = RTOnce(&Once1, Once1CB, (void *)1, (void *)42);
|
---|
116 | if (rc != VINF_SUCCESS)
|
---|
117 | RTPrintf("tstOnce: ERROR - Once1, 1 failed, rc=%Rrc\n", rc);
|
---|
118 | g_fOnceCB1 = false;
|
---|
119 | rc = RTOnce(&Once1, Once1CB, (void *)1, (void *)42);
|
---|
120 | if (rc != VINF_SUCCESS)
|
---|
121 | RTPrintf("tstOnce: ERROR - Once1, 2 failed, rc=%Rrc\n", rc);
|
---|
122 |
|
---|
123 | /*
|
---|
124 | * Throw a bunch of threads up against a init once thing.
|
---|
125 | */
|
---|
126 | RTPrintf("tstOnce: TESTING - bunch of threads...\n");
|
---|
127 | /* create the semaphore they'll be waiting on. */
|
---|
128 | rc = RTSemEventMultiCreate(&g_hEventMulti);
|
---|
129 | if (RT_FAILURE(rc))
|
---|
130 | {
|
---|
131 | RTPrintf("tstOnce: FATAL ERROR - RTSemEventMultiCreate returned %Rrc\n", rc);
|
---|
132 | return 1;
|
---|
133 | }
|
---|
134 |
|
---|
135 | /* create the threads */
|
---|
136 | RTTHREAD aThreads[32];
|
---|
137 | for (unsigned i = 0; i < RT_ELEMENTS(aThreads); i++)
|
---|
138 | {
|
---|
139 | char szName[16];
|
---|
140 | RTStrPrintf(szName, sizeof(szName), "ONCE2-%d\n", i);
|
---|
141 | rc = RTThreadCreate(&aThreads[i], Once2Thread, NULL, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, szName);
|
---|
142 | if (RT_FAILURE(rc))
|
---|
143 | {
|
---|
144 | RTPrintf("tstOnce: ERROR - failed to create thread #%d\n", i);
|
---|
145 | g_cErrors++;
|
---|
146 | }
|
---|
147 | }
|
---|
148 |
|
---|
149 | /* kick them off and yield */
|
---|
150 | rc = RTSemEventMultiSignal(g_hEventMulti);
|
---|
151 | if (RT_FAILURE(rc))
|
---|
152 | {
|
---|
153 | RTPrintf("tstOnce: FATAL ERROR - RTSemEventMultiSignal returned %Rrc\n", rc);
|
---|
154 | return 1;
|
---|
155 | }
|
---|
156 | RTThreadYield();
|
---|
157 |
|
---|
158 | /* wait for all of them to finish up, 30 seconds each. */
|
---|
159 | for (unsigned i = 0; i < RT_ELEMENTS(aThreads); i++)
|
---|
160 | if (aThreads[i] != NIL_RTTHREAD)
|
---|
161 | {
|
---|
162 | int rc2;
|
---|
163 | rc = RTThreadWait(aThreads[i], 30*1000, &rc2);
|
---|
164 | if (RT_FAILURE(rc))
|
---|
165 | {
|
---|
166 | RTPrintf("tstOnce: ERROR - RTThreadWait on thread #%u returned %Rrc\n", i, rc);
|
---|
167 | g_cErrors++;
|
---|
168 | }
|
---|
169 | else if (RT_FAILURE(rc2))
|
---|
170 | {
|
---|
171 | RTPrintf("tstOnce: ERROR - Thread #%u returned %Rrc\n", i, rc2);
|
---|
172 | g_cErrors++;
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | /*
|
---|
177 | * Summary.
|
---|
178 | */
|
---|
179 | if (!g_cErrors)
|
---|
180 | RTPrintf("tstOnce: SUCCESS\n");
|
---|
181 | else
|
---|
182 | RTPrintf("tstOnce: FAILURE - %d errors\n", g_cErrors);
|
---|
183 |
|
---|
184 | return !!g_cErrors;
|
---|
185 | }
|
---|
186 |
|
---|