VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstOnce.cpp@ 10940

最後變更 在這個檔案從10940是 10940,由 vboxsync 提交於 17 年 前

IPRT: Implemented the RTOnce API.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 5.5 KB
 
1/* $Id: tstOnce.cpp 10940 2008-07-29 16:32:25Z vboxsync $ */
2/** @file
3 * IPRT Testcase - RTOnce.
4 */
5
6/*
7 * Copyright (C) 2008 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.alldomusa.eu.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/once.h>
35#include <iprt/stream.h>
36#include <iprt/initterm.h>
37#include <iprt/thread.h>
38#include <iprt/semaphore.h>
39#include <iprt/string.h>
40#include <iprt/err.h>
41#include <iprt/asm.h>
42
43
44/*******************************************************************************
45* Global Variables *
46*******************************************************************************/
47static int g_cErrors = 0;
48static bool g_fOnceCB1 = false;
49static uint32_t volatile g_cOnceCB2 = 0;
50static RTONCE g_Once2 = RTONCE_INITIALIZER;
51static RTSEMEVENTMULTI g_hEventMulti = NIL_RTSEMEVENTMULTI;
52
53
54static DECLCALLBACK(int) Once1CB(void *pvUser1, void *pvUser2)
55{
56 if (g_fOnceCB1)
57 return VERR_WRONG_ORDER;
58 if (pvUser1 != (void *)1 || pvUser2 != (void *)42)
59 {
60 RTPrintf("tstOnce: ERROR - Once1CB: pvUser1=%p pvUser2=%p!\n", pvUser1, pvUser2);
61 g_cErrors++;
62 return VERR_INVALID_PARAMETER;
63 }
64 return VINF_SUCCESS;
65}
66
67
68static DECLCALLBACK(int) Once2CB(void *pvUser1, void *pvUser2)
69{
70 if (ASMAtomicIncU32(&g_cOnceCB2) != 1)
71 {
72 RTPrintf("tstOnce: ERROR - Once2CB: g_cOnceCB2 not zero!\n");
73 g_cErrors++;
74 return VERR_WRONG_ORDER;
75 }
76 if (pvUser1 != (void *)42 || pvUser2 != (void *)1)
77 {
78 RTPrintf("tstOnce: ERROR - Once2CB: pvUser1=%p pvUser2=%p!\n", pvUser1, pvUser2);
79 g_cErrors++;
80 return VERR_INVALID_PARAMETER;
81 }
82 return VINF_SUCCESS;
83}
84
85static 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 return RTOnce(&g_Once2, Once2CB, (void *)42, (void *)1);
93}
94
95
96int main()
97{
98 RTR3Init();
99 RTPrintf("tstOnce: SUCCESS\n");
100
101 /*
102 * Just a simple testcase.
103 */
104 RTONCE Once1 = RTONCE_INITIALIZER;
105 g_fOnceCB1 = false;
106 int rc = RTOnce(&Once1, Once1CB, (void *)1, (void *)42);
107 if (rc != VINF_SUCCESS)
108 RTPrintf("tstOnce: ERROR - Once1, 1 failed, rc=%Rrc\n", rc);
109 g_fOnceCB1 = false;
110 rc = RTOnce(&Once1, Once1CB, (void *)1, (void *)42);
111 if (rc != VINF_SUCCESS)
112 RTPrintf("tstOnce: ERROR - Once1, 2 failed, rc=%Rrc\n", rc);
113
114 /*
115 * Throw a bunch of threads up against a init once thing.
116 */
117 /* create the semaphore they'll be waiting on. */
118 rc = RTSemEventMultiCreate(&g_hEventMulti);
119 if (RT_FAILURE(rc))
120 {
121 RTPrintf("tstOnce: FATAL ERROR - RTSemEventMultiCreate returned %Rrc\n", rc);
122 return 1;
123 }
124
125 /* create the threads */
126 RTTHREAD aThreads[32];
127 for (unsigned i = 0; i < RT_ELEMENTS(aThreads); i++)
128 {
129 char szName[16];
130 RTStrPrintf(szName, sizeof(szName), "ONCE2-%d\n", i);
131 int rc = RTThreadCreate(&aThreads[i], Once2Thread, NULL, 0, RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, szName);
132 if (RT_FAILURE(rc))
133 {
134 RTPrintf("tstOnce: ERROR - failed to create thread #%d\n", i);
135 g_cErrors++;
136 }
137 }
138
139 /* kick them off and yield */
140 rc = RTSemEventMultiSignal(g_hEventMulti);
141 if (RT_FAILURE(rc))
142 {
143 RTPrintf("tstOnce: FATAL ERROR - RTSemEventMultiSignal returned %Rrc\n", rc);
144 return 1;
145 }
146 RTThreadYield();
147
148 /* wait for all of them to finish up, 30 seconds each. */
149 for (unsigned i = 0; i < RT_ELEMENTS(aThreads); i++)
150 if (aThreads[i] != NIL_RTTHREAD)
151 {
152 int rc2;
153 int rc = RTThreadWait(aThreads[i], 30*1000, &rc2);
154 if (RT_FAILURE(rc))
155 {
156 RTPrintf("tstOnce: ERROR - RTThreadWait on thread #%u returned %Rrc\n", i, rc);
157 g_cErrors++;
158 }
159 else if (RT_FAILURE(rc2))
160 {
161 RTPrintf("tstOnce: ERROR - Thread #%u returned %Rrc\n", i, rc2);
162 g_cErrors++;
163 }
164 }
165
166 /*
167 * Summary.
168 */
169 if (!g_cErrors)
170 RTPrintf("tstOnce: SUCCESS\n");
171 else
172 RTPrintf("tstOnce: FAILURE - %d errors\n", g_cErrors);
173
174 return !!g_cErrors;
175}
176
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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