Back to index

lightning-sunbird  0.9+nobinonly
ntioto.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039 ** File: ntioto.c
00040 ** Description: 
00041 ** This test, ntioto.c, was designed to reproduce a bug reported by NES
00042 ** on WindowsNT (fibers implementation). NSPR was asserting in ntio.c
00043 ** after PR_AcceptRead() had timed out. I/O performed subsequent to the
00044 ** call to PR_AcceptRead() could complete on a CPU other than the one
00045 ** on which it was started. The assert in ntio.c detected this, then
00046 ** asserted.
00047 **
00048 ** Design:
00049 ** This test will fail with an assert in ntio.c if the problem it was
00050 ** designed to catch occurs. It returns 0 otherwise.
00051 ** 
00052 ** The main() thread initializes and tears things down. A file is
00053 ** opened for writing; this file will be written to by AcceptThread()
00054 ** and JitterThread().  Main() creates a socket for reading, listens
00055 ** and binds the socket.
00056 ** 
00057 ** ConnectThread() connects to the socket created by main, then polls
00058 ** the "state" variable. When state is AllDone, ConnectThread() exits.
00059 **
00060 ** AcceptThread() calls PR_AcceptRead() on the socket. He fully expects
00061 ** it to time out. After the timeout, AccpetThread() interacts with
00062 ** JitterThread() via a common condition variable and the state
00063 ** variable. The two threads ping-pong back and forth, each thread
00064 ** writes the the file opened by main. This should provoke the
00065 ** condition reported by NES (if we didn't fix it).
00066 **
00067 ** The failure is not solid. It may fail within a few ping-pongs between
00068 ** AcceptThread() and JitterThread() or may take a while. The default
00069 ** iteration count, jitter, is set by DEFAULT_JITTER. This may be
00070 ** modified at the command line with the -j option.
00071 ** 
00072 */
00073 
00074 #include <plgetopt.h> 
00075 #include <nspr.h> 
00076 #include <stdio.h>
00077 #include <stdlib.h>
00078 #include <string.h>
00079 
00080 /*
00081 ** Test harness infrastructure
00082 */
00083 PRLogModuleInfo *lm;
00084 PRLogModuleLevel msgLevel = PR_LOG_NONE;
00085 PRIntn  debug = 0;
00086 PRIntn  verbose = 0;
00087 PRUint32  failed_already = 0;
00088 /* end Test harness infrastructure */
00089 
00090 /* JITTER_DEFAULT: the number of times AcceptThread() and JitterThread() ping-pong */
00091 #define JITTER_DEFAULT  100000
00092 #define BASE_PORT 9867
00093 
00094 PRIntervalTime timeout;
00095 PRNetAddr   listenAddr;
00096 PRFileDesc  *listenSock;
00097 PRLock      *ml;
00098 PRCondVar   *cv;
00099 volatile enum  {
00100     RunJitter,
00101     RunAcceptRead,
00102     AllDone
00103 } state = RunAcceptRead;
00104 PRFileDesc  *file1;
00105 PRIntn  iCounter = 0;
00106 PRIntn  jitter = JITTER_DEFAULT;
00107 PRBool  resume = PR_FALSE;
00108 
00109 /*
00110 ** Emit help text for this test
00111 */
00112 static void Help( void )
00113 {
00114     printf("Template: Help(): display your help message(s) here");
00115     exit(1);
00116 } /* end Help() */
00117 
00118 
00119 /*
00120 ** static computation of PR_AcceptRead() buffer size.
00121 */
00122 #define ACCEPT_READ_DATASIZE 10
00123 #define ACCEPT_READ_BUFSIZE (PR_ACCEPT_READ_BUF_OVERHEAD + ACCEPT_READ_DATASIZE)
00124 
00125 static void AcceptThread(void *arg)
00126 {
00127     PRIntn bytesRead;
00128     char dataBuf[ACCEPT_READ_BUFSIZE];
00129     PRFileDesc  *arSock;
00130     PRNetAddr   *arAddr;
00131 
00132     bytesRead = PR_AcceptRead( listenSock, 
00133         &arSock,
00134         &arAddr,
00135         dataBuf,
00136         ACCEPT_READ_DATASIZE,
00137         PR_SecondsToInterval(1));
00138 
00139     if ( bytesRead == -1 && PR_GetError() == PR_IO_TIMEOUT_ERROR ) {
00140         if ( debug ) printf("AcceptRead timed out\n");
00141     } else {
00142         if ( debug ) printf("Oops! read: %d, error: %d\n", bytesRead, PR_GetError());
00143     }
00144 
00145     while( state != AllDone )  {
00146         PR_Lock( ml );
00147         while( state != RunAcceptRead )
00148             PR_WaitCondVar( cv, PR_INTERVAL_NO_TIMEOUT );
00149         if ( ++iCounter >= jitter )
00150             state = AllDone;
00151         else
00152             state = RunJitter;
00153         if ( verbose ) printf(".");
00154         PR_NotifyCondVar( cv );
00155         PR_Unlock( ml );
00156         PR_Write( file1, ".", 1 );
00157     }
00158 
00159     return;
00160 } /* end AcceptThread() */
00161 
00162 static void JitterThread(void *arg)
00163 {
00164     while( state != AllDone )  {
00165         PR_Lock( ml );
00166         while( state != RunJitter && state != AllDone )
00167             PR_WaitCondVar( cv, PR_INTERVAL_NO_TIMEOUT );
00168         if ( state != AllDone)
00169             state = RunAcceptRead;
00170         if ( verbose ) printf("+");
00171         PR_NotifyCondVar( cv );
00172         PR_Unlock( ml );
00173         PR_Write( file1, "+", 1 );
00174     }
00175     return;
00176 } /* end Goofy() */
00177 
00178 static void ConnectThread( void *arg )
00179 {
00180     PRStatus    rv;
00181     PRFileDesc  *clientSock;
00182     PRNetAddr   serverAddress;
00183     clientSock = PR_NewTCPSocket();
00184 
00185     PR_ASSERT(clientSock);
00186 
00187     if ( resume ) {
00188         if ( debug ) printf("pausing 3 seconds before connect\n");
00189         PR_Sleep( PR_SecondsToInterval(3));
00190     }
00191 
00192     memset(&serverAddress, 0, sizeof(serverAddress));
00193     rv = PR_InitializeNetAddr(PR_IpAddrLoopback, BASE_PORT, &serverAddress);
00194     PR_ASSERT( PR_SUCCESS == rv );
00195     rv = PR_Connect( clientSock, 
00196         &serverAddress, 
00197         PR_SecondsToInterval(1));
00198     PR_ASSERT( PR_SUCCESS == rv );
00199 
00200     /* that's all we do. ... Wait for the acceptread() to timeout */
00201     while( state != AllDone )
00202         PR_Sleep( PR_SecondsToInterval(1));
00203     return;
00204 } /* end ConnectThread() */
00205 
00206 
00207 PRIntn main(PRIntn argc, char *argv[])
00208 {
00209     PRThread *tJitter;
00210     PRThread *tAccept;
00211     PRThread *tConnect;
00212     PRStatus rv;
00213     /* This test if valid for WinNT only! */
00214 
00215 #if !defined(WINNT)
00216     return 0;
00217 #endif
00218 
00219     {
00220         /*
00221         ** Get command line options
00222         */
00223         PLOptStatus os;
00224         PLOptState *opt = PL_CreateOptState(argc, argv, "hdrvj:");
00225 
00226            while (PL_OPT_EOL != (os = PL_GetNextOpt(opt)))
00227         {
00228                   if (PL_OPT_BAD == os) continue;
00229             switch (opt->option)
00230             {
00231             case 'd':  /* debug */
00232                 debug = 1;
00233                          msgLevel = PR_LOG_ERROR;
00234                 break;
00235             case 'v':  /* verbose mode */
00236                 verbose = 1;
00237                          msgLevel = PR_LOG_DEBUG;
00238                 break;
00239             case 'j':
00240                 jitter = atoi(opt->value);
00241                 if ( jitter == 0)
00242                     jitter = JITTER_DEFAULT;
00243                 break;
00244             case 'r':
00245                 resume = PR_TRUE;
00246                 break;
00247             case 'h':  /* help message */
00248                          Help();
00249                 break;
00250              default:
00251                 break;
00252             }
00253         }
00254            PL_DestroyOptState(opt);
00255     }
00256 
00257     lm = PR_NewLogModule("Test");       /* Initialize logging */
00258 
00259     /* set concurrency */
00260     PR_SetConcurrency( 4 );
00261 
00262     /* setup thread synchronization mechanics */
00263     ml = PR_NewLock();
00264     cv = PR_NewCondVar( ml );
00265 
00266     /* setup a tcp socket */
00267     memset(&listenAddr, 0, sizeof(listenAddr));
00268     rv = PR_InitializeNetAddr(PR_IpAddrAny, BASE_PORT, &listenAddr);
00269     PR_ASSERT( PR_SUCCESS == rv );
00270 
00271     listenSock = PR_NewTCPSocket();
00272     PR_ASSERT( listenSock );
00273 
00274     rv = PR_Bind( listenSock, &listenAddr);
00275     PR_ASSERT( PR_SUCCESS == rv );
00276 
00277     rv = PR_Listen( listenSock, 5 );
00278     PR_ASSERT( PR_SUCCESS == rv );
00279 
00280     /* open a file for writing, provoke bug */
00281     file1 = PR_Open("xxxTestFile", PR_CREATE_FILE | PR_RDWR, 666);
00282 
00283     /* create Connect thread */
00284     tConnect = PR_CreateThread(
00285         PR_USER_THREAD, ConnectThread, NULL,
00286         PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
00287         PR_JOINABLE_THREAD, 0 );
00288     PR_ASSERT( tConnect );
00289 
00290     /* create jitter off thread */
00291     tJitter = PR_CreateThread(
00292         PR_USER_THREAD, JitterThread, NULL,
00293         PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD,
00294         PR_JOINABLE_THREAD, 0 );
00295     PR_ASSERT( tJitter );
00296 
00297     /* create acceptread thread */
00298     tAccept = PR_CreateThread(
00299         PR_USER_THREAD, AcceptThread, NULL,
00300         PR_PRIORITY_NORMAL, PR_LOCAL_THREAD,
00301         PR_JOINABLE_THREAD, 0 );
00302     PR_ASSERT( tAccept );
00303 
00304     /* wait for all threads to quit, then terminate gracefully */
00305     PR_JoinThread( tConnect );
00306     PR_JoinThread( tAccept );
00307     PR_JoinThread( tJitter );
00308     PR_Close( listenSock );
00309     PR_DestroyCondVar(cv);
00310     PR_DestroyLock(ml);
00311     PR_Close( file1 );
00312     PR_Delete( "xxxTestFile");
00313 
00314     /* test return and exit */
00315     if (debug) printf("%s\n", (failed_already)? "FAIL" : "PASS");
00316     return( (failed_already == PR_TRUE )? 1 : 0 );
00317 }  /* main() */
00318 /* end ntioto.c */