VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/nsprpub/pr/tests/ntioto.c@ 22683

最後變更 在這個檔案從22683是 1,由 vboxsync 提交於 55 年 前

import

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 9.5 KB
 
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2/* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * The Original Code is the Netscape Portable Runtime (NSPR).
16 *
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998-2000
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 *
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
35 *
36 * ***** END LICENSE BLOCK ***** */
37
38/*
39** File: ntioto.c
40** Description:
41** This test, ntioto.c, was designed to reproduce a bug reported by NES
42** on WindowsNT (fibers implementation). NSPR was asserting in ntio.c
43** after PR_AcceptRead() had timed out. I/O performed subsequent to the
44** call to PR_AcceptRead() could complete on a CPU other than the one
45** on which it was started. The assert in ntio.c detected this, then
46** asserted.
47**
48** Design:
49** This test will fail with an assert in ntio.c if the problem it was
50** designed to catch occurs. It returns 0 otherwise.
51**
52** The main() thread initializes and tears things down. A file is
53** opened for writing; this file will be written to by AcceptThread()
54** and JitterThread(). Main() creates a socket for reading, listens
55** and binds the socket.
56**
57** ConnectThread() connects to the socket created by main, then polls
58** the "state" variable. When state is AllDone, ConnectThread() exits.
59**
60** AcceptThread() calls PR_AcceptRead() on the socket. He fully expects
61** it to time out. After the timeout, AccpetThread() interacts with
62** JitterThread() via a common condition variable and the state
63** variable. The two threads ping-pong back and forth, each thread
64** writes the the file opened by main. This should provoke the
65** condition reported by NES (if we didn't fix it).
66**
67** The failure is not solid. It may fail within a few ping-pongs between
68** AcceptThread() and JitterThread() or may take a while. The default
69** iteration count, jitter, is set by DEFAULT_JITTER. This may be
70** modified at the command line with the -j option.
71**
72*/
73
74#include <plgetopt.h>
75#include <nspr.h>
76#include <stdio.h>
77#include <stdlib.h>
78#include <string.h>
79
80/*
81** Test harness infrastructure
82*/
83PRLogModuleInfo *lm;
84PRLogModuleLevel msgLevel = PR_LOG_NONE;
85PRIntn debug = 0;
86PRIntn verbose = 0;
87PRUint32 failed_already = 0;
88/* end Test harness infrastructure */
89
90/* JITTER_DEFAULT: the number of times AcceptThread() and JitterThread() ping-pong */
91#define JITTER_DEFAULT 100000
92#define BASE_PORT 9867
93
94PRIntervalTime timeout;
95PRNetAddr listenAddr;
96PRFileDesc *listenSock;
97PRLock *ml;
98PRCondVar *cv;
99volatile enum {
100 RunJitter,
101 RunAcceptRead,
102 AllDone
103} state = RunAcceptRead;
104PRFileDesc *file1;
105PRIntn iCounter = 0;
106PRIntn jitter = JITTER_DEFAULT;
107PRBool resume = PR_FALSE;
108
109/*
110** Emit help text for this test
111*/
112static void Help( void )
113{
114 printf("Template: Help(): display your help message(s) here");
115 exit(1);
116} /* end Help() */
117
118
119/*
120** static computation of PR_AcceptRead() buffer size.
121*/
122#define ACCEPT_READ_DATASIZE 10
123#define ACCEPT_READ_BUFSIZE (PR_ACCEPT_READ_BUF_OVERHEAD + ACCEPT_READ_DATASIZE)
124
125static void AcceptThread(void *arg)
126{
127 PRIntn bytesRead;
128 char dataBuf[ACCEPT_READ_BUFSIZE];
129 PRFileDesc *arSock;
130 PRNetAddr *arAddr;
131
132 bytesRead = PR_AcceptRead( listenSock,
133 &arSock,
134 &arAddr,
135 dataBuf,
136 ACCEPT_READ_DATASIZE,
137 PR_SecondsToInterval(1));
138
139 if ( bytesRead == -1 && PR_GetError() == PR_IO_TIMEOUT_ERROR )
140 if ( debug ) printf("AcceptRead timed out\n");
141 else
142 if ( debug ) printf("Oops! read: %d, error: %d\n", bytesRead, PR_GetError());
143
144 while( state != AllDone ) {
145 PR_Lock( ml );
146 while( state != RunAcceptRead )
147 PR_WaitCondVar( cv, PR_INTERVAL_NO_TIMEOUT );
148 if ( ++iCounter >= jitter )
149 state = AllDone;
150 else
151 state = RunJitter;
152 if ( verbose ) printf(".");
153 PR_NotifyCondVar( cv );
154 PR_Unlock( ml );
155 PR_Write( file1, ".", 1 );
156 }
157
158 return;
159} /* end AcceptThread() */
160
161static void JitterThread(void *arg)
162{
163 while( state != AllDone ) {
164 PR_Lock( ml );
165 while( state != RunJitter && state != AllDone )
166 PR_WaitCondVar( cv, PR_INTERVAL_NO_TIMEOUT );
167 if ( state != AllDone)
168 state = RunAcceptRead;
169 if ( verbose ) printf("+");
170 PR_NotifyCondVar( cv );
171 PR_Unlock( ml );
172 PR_Write( file1, "+", 1 );
173 }
174 return;
175} /* end Goofy() */
176
177static void ConnectThread( void *arg )
178{
179 PRStatus rv;
180 PRFileDesc *clientSock;
181 PRNetAddr serverAddress;
182 clientSock = PR_NewTCPSocket();
183
184 PR_ASSERT(clientSock);
185
186 if ( resume ) {
187 if ( debug ) printf("pausing 3 seconds before connect\n");
188 PR_Sleep( PR_SecondsToInterval(3));
189 }
190
191 memset(&serverAddress, 0, sizeof(serverAddress));
192 rv = PR_InitializeNetAddr(PR_IpAddrLoopback, BASE_PORT, &serverAddress);
193 PR_ASSERT( PR_SUCCESS == rv );
194 rv = PR_Connect( clientSock,
195 &serverAddress,
196 PR_SecondsToInterval(1));
197 PR_ASSERT( PR_SUCCESS == rv );
198
199 /* that's all we do. ... Wait for the acceptread() to timeout */
200 while( state != AllDone )
201 PR_Sleep( PR_SecondsToInterval(1));
202 return;
203} /* end ConnectThread() */
204
205
206PRIntn main(PRIntn argc, char *argv[])
207{
208 PRThread *tJitter;
209 PRThread *tAccept;
210 PRThread *tConnect;
211 PRStatus rv;
212 /* This test if valid for WinNT only! */
213
214#if !defined(WINNT)
215 return 0;
216#endif
217
218 {
219 /*
220 ** Get command line options
221 */
222 PLOptStatus os;
223 PLOptState *opt = PL_CreateOptState(argc, argv, "hdrvj:");
224
225 while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
226 {
227 if (PL_OPT_BAD == os) continue;
228 switch (opt->option)
229 {
230 case 'd': /* debug */
231 debug = 1;
232 msgLevel = PR_LOG_ERROR;
233 break;
234 case 'v': /* verbose mode */
235 verbose = 1;
236 msgLevel = PR_LOG_DEBUG;
237 break;
238 case 'j':
239 jitter = atoi(opt->value);
240 if ( jitter == 0)
241 jitter = JITTER_DEFAULT;
242 break;
243 case 'r':
244 resume = PR_TRUE;
245 break;
246 case 'h': /* help message */
247 Help();
248 break;
249 default:
250 break;
251 }
252 }
253 PL_DestroyOptState(opt);
254 }
255
256 lm = PR_NewLogModule("Test"); /* Initialize logging */
257
258 /* set concurrency */
259 PR_SetConcurrency( 4 );
260
261 /* setup thread synchronization mechanics */
262 ml = PR_NewLock();
263 cv = PR_NewCondVar( ml );
264
265 /* setup a tcp socket */
266 memset(&listenAddr, 0, sizeof(listenAddr));
267 rv = PR_InitializeNetAddr(PR_IpAddrAny, BASE_PORT, &listenAddr);
268 PR_ASSERT( PR_SUCCESS == rv );
269
270 listenSock = PR_NewTCPSocket();
271 PR_ASSERT( listenSock );
272
273 rv = PR_Bind( listenSock, &listenAddr);
274 PR_ASSERT( PR_SUCCESS == rv );
275
276 rv = PR_Listen( listenSock, 5 );
277 PR_ASSERT( PR_SUCCESS == rv );
278
279 /* open a file for writing, provoke bug */
280 file1 = PR_Open("xxxTestFile", PR_CREATE_FILE | PR_RDWR, 666);
281
282 /* create Connect thread */
283 tConnect = PR_CreateThread(
284 PR_USER_THREAD, ConnectThread, NULL,
285 PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
286 PR_JOINABLE_THREAD, 0 );
287 PR_ASSERT( tConnect );
288
289 /* create jitter off thread */
290 tJitter = PR_CreateThread(
291 PR_USER_THREAD, JitterThread, NULL,
292 PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
293 PR_JOINABLE_THREAD, 0 );
294 PR_ASSERT( tJitter );
295
296 /* create acceptread thread */
297 tAccept = PR_CreateThread(
298 PR_USER_THREAD, AcceptThread, NULL,
299 PR_PRIORITY_NORMAL, PR_LOCAL_THREAD,
300 PR_JOINABLE_THREAD, 0 );
301 PR_ASSERT( tAccept );
302
303 /* wait for all threads to quit, then terminate gracefully */
304 PR_JoinThread( tConnect );
305 PR_JoinThread( tAccept );
306 PR_JoinThread( tJitter );
307 PR_Close( listenSock );
308 PR_DestroyCondVar(cv);
309 PR_DestroyLock(ml);
310 PR_Close( file1 );
311 PR_Delete( "xxxTestFile");
312
313 /* test return and exit */
314 if (debug) printf("%s\n", (failed_already)? "FAIL" : "PASS");
315 return( (failed_already == PR_TRUE )? 1 : 0 );
316} /* main() */
317/* end ntioto.c */
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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