1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/test/TestThreadedIO.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,284 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 +#include <stdio.h> 1.9 +#include "nsCOMPtr.h" 1.10 +#include "nsIEventQueueService.h" 1.11 +#include "nsIServiceManager.h" 1.12 +#include "nsIStreamListener.h" 1.13 +#include "nsIURI.h" 1.14 +#include "nsNetUtil.h" 1.15 +#include <algorithm> 1.16 +//#include "prthread.h" 1.17 + 1.18 +// This test attempts to load a URL on a separate thread. It is currently 1.19 +// designed simply to expose the problems inherent in such an ambitous task 1.20 +// (i.e., it don't work). 1.21 + 1.22 +// Utility functions... 1.23 + 1.24 +// Create event queue for current thread. 1.25 +static nsCOMPtr<nsIEventQueue> 1.26 +createEventQueue() { 1.27 + nsCOMPtr<nsIEventQueue> result; 1.28 + // Get event queue service. 1.29 + nsresult rv = NS_OK; 1.30 + nsCOMPtr<nsIEventQueueService> eqs = 1.31 + do_GetService(NS_EVENTQUEUESERVICE_CONTRACTID, &rv); 1.32 + if ( NS_SUCCEEDED( rv ) ) { 1.33 + eqs->GetThreadEventQueue(NS_CURRENT_THREAD, getter_AddRefs(result)); 1.34 + } else { 1.35 + printf( "%s %d: NS_WITH_SERVICE(nsIEventQueueService) failed, rv=0x%08X\n", 1.36 + (char*)__FILE__, (int)__LINE__, (int)rv ); 1.37 + } 1.38 + return result; 1.39 +} 1.40 + 1.41 +// Create channel for requested URL. 1.42 +static nsCOMPtr<nsIChannel> 1.43 +createChannel( const char *url ) { 1.44 + nsCOMPtr<nsIInputStream> result; 1.45 + 1.46 + nsCOMPtr<nsIURI> uri; 1.47 + printf( "Calling NS_NewURI for %s...\n", url ); 1.48 + nsresult rv = NS_NewURI( getter_AddRefs( uri ), url ); 1.49 + 1.50 + if ( NS_SUCCEEDED( rv ) ) { 1.51 + printf( "...NS_NewURI completed OK\n" ); 1.52 + 1.53 + // Allocate a new input channel on this thread. 1.54 + printf( "Calling NS_OpenURI...\n" ); 1.55 + nsresult rv = NS_OpenURI( getter_AddRefs( result ), uri, 0 ); 1.56 + 1.57 + if ( NS_SUCCEEDED( rv ) ) { 1.58 + printf( "...NS_OpenURI completed OK\n" ); 1.59 + } else { 1.60 + printf( "%s %d: NS_OpenURI failed, rv=0x%08X\n", 1.61 + (char*)__FILE__, (int)__LINE__, (int)rv ); 1.62 + } 1.63 + } else { 1.64 + printf( "%s %d: NS_NewURI failed, rv=0x%08X\n", 1.65 + (char*)__FILE__, (int)__LINE__, (int)rv ); 1.66 + } 1.67 + return result; 1.68 +} 1.69 + 1.70 +// Test listener. It basically dumps incoming data to console. 1.71 +class TestListener : public nsIStreamListener { 1.72 +public: 1.73 + NS_DECL_ISUPPORTS 1.74 + NS_DECL_NSISTREAMLISTENER 1.75 + NS_DECL_NSISTREAMOBSERVER 1.76 + 1.77 + TestListener(); 1.78 + ~TestListener(); 1.79 + static void IOThread( void *p ); 1.80 + 1.81 +private: 1.82 + bool mDone; 1.83 + int mThreadNo; 1.84 + FILE *mFile; 1.85 + static int threadCount; 1.86 +}; // class TestListener 1.87 + 1.88 +int TestListener::threadCount = 0; 1.89 + 1.90 +TestListener::TestListener() 1.91 + : mDone( false ), mThreadNo( ++threadCount ) { 1.92 + printf( "TestListener ctor called on thread %d\n", mThreadNo ); 1.93 +} 1.94 + 1.95 +TestListener::~TestListener() { 1.96 + printf( "TestListener dtor called on thread %d\n", mThreadNo ); 1.97 +} 1.98 + 1.99 +NS_IMPL_ISUPPORTS( TestListener, nsIStreamListener, nsIRequestObserver ) 1.100 + 1.101 +NS_IMETHODIMP 1.102 +TestListener::OnStartRequest( nsIChannel *aChannel, nsISupports *aContext ) { 1.103 + nsresult rv = NS_OK; 1.104 + 1.105 + printf( "TestListener::OnStartRequest called on thread %d\n", mThreadNo ); 1.106 + 1.107 + // Open output file. 1.108 + char fileName[32]; 1.109 + sprintf( fileName, "%s%d", "thread", mThreadNo ); 1.110 + mFile = fopen( fileName, "wb" ); 1.111 + setbuf( mFile, 0 ); 1.112 + 1.113 + return rv; 1.114 +} 1.115 + 1.116 +NS_IMETHODIMP 1.117 +TestListener::OnStopRequest( nsIChannel *aChannel, 1.118 + nsISupports *aContext, 1.119 + nsresult aStatus, 1.120 + const char16_t *aMsg ) { 1.121 + nsresult rv = NS_OK; 1.122 + 1.123 + printf( "TestListener::OnStopRequest called on thread %d\n", mThreadNo ); 1.124 + 1.125 + fclose( mFile ); 1.126 + mDone = true; 1.127 + 1.128 + return rv; 1.129 +} 1.130 + 1.131 +NS_IMETHODIMP 1.132 +TestListener::OnDataAvailable( nsIChannel *aChannel, 1.133 + nsISupports *aContext, 1.134 + nsIInputStream *aStream, 1.135 + uint64_t offset, 1.136 + uint32_t aLength ) { 1.137 + nsresult rv = NS_OK; 1.138 + 1.139 + printf( "TestListener::OnDataAvailable called on thread %d\n", mThreadNo ); 1.140 + 1.141 + // Write the data to the console. 1.142 + // Read a buffer full till aLength bytes have been processed. 1.143 + char buffer[ 8192 ]; 1.144 + unsigned long bytesRemaining = aLength; 1.145 + while ( bytesRemaining ) { 1.146 + unsigned int bytesRead; 1.147 + // Read a buffer full or the number remaining (whichever is smaller). 1.148 + rv = aStream->Read( buffer, 1.149 + std::min( sizeof( buffer ), bytesRemaining ), 1.150 + &bytesRead ); 1.151 + if ( NS_SUCCEEDED( rv ) ) { 1.152 + // Write the bytes just read to the output file. 1.153 + fwrite( buffer, 1, bytesRead, mFile ); 1.154 + bytesRemaining -= bytesRead; 1.155 + } else { 1.156 + printf( "%s %d: Read error, rv=0x%08X\n", 1.157 + (char*)__FILE__, (int)__LINE__, (int)rv ); 1.158 + break; 1.159 + } 1.160 + } 1.161 + printf( "\n" ); 1.162 + 1.163 + return rv; 1.164 +} 1.165 + 1.166 +// IOThread: this function creates a new TestListener object (on the new 1.167 +// thread), opens a channel, and does AsyncRead to it. 1.168 +void 1.169 +TestListener::IOThread( void *p ) { 1.170 + printf( "I/O thread (0x%08X) started...\n", (int)(void*)PR_GetCurrentThread() ); 1.171 + 1.172 + // Argument is pointer to the nsIEventQueue for the main thread. 1.173 + nsIEventQueue *mainThreadQ = static_cast<nsIEventQueue*>(p); 1.174 + 1.175 + // Create channel for random web page. 1.176 + nsCOMPtr<nsIChannel> channel = createChannel( (const char*)p ); 1.177 + 1.178 + if ( channel ) { 1.179 + // Create event queue. 1.180 + nsCOMPtr<nsIEventQueue> ioEventQ = createEventQueue(); 1.181 + 1.182 + if ( ioEventQ ) { 1.183 + // Create test listener. 1.184 + TestListener *testListener = new TestListener(); 1.185 + testListener->AddRef(); 1.186 + 1.187 + // Read the channel. 1.188 + printf( "Doing AsyncRead...\n" ); 1.189 + nsresult rv = channel->AsyncRead( testListener, 0 ); 1.190 + 1.191 + if ( NS_SUCCEEDED( rv ) ) { 1.192 + printf( "...AsyncRead completed OK\n" ); 1.193 + 1.194 + // Process events till testListener says stop. 1.195 + printf( "Start event loop on io thread %d...\n", testListener->mThreadNo ); 1.196 + while ( !testListener->mDone ) { 1.197 + PLEvent *event; 1.198 + ioEventQ->GetEvent( &event ); 1.199 + ioEventQ->HandleEvent( event ); 1.200 + } 1.201 + printf( "...io thread %d event loop exiting\n", testListener->mThreadNo ); 1.202 + } else { 1.203 + printf( "%s %d: AsyncRead failed on thread %d, rv=0x%08X\n", 1.204 + (char*)__FILE__, (int)__LINE__, testListener->mThreadNo, (int)rv ); 1.205 + } 1.206 + 1.207 + // Release the test listener. 1.208 + testListener->Release(); 1.209 + } 1.210 + } 1.211 + 1.212 + printf( "...I/O thread terminating\n" ); 1.213 +} 1.214 + 1.215 +static const int maxThreads = 5; 1.216 + 1.217 +int 1.218 +main( int argc, char* argv[] ) { 1.219 + setbuf( stdout, 0 ); 1.220 + if ( argc < 2 || argc > maxThreads + 1 ) { 1.221 + printf( "usage: testThreadedIO url1 <url2>...\n" 1.222 + "where <url#> is a location to be loaded on a separate thread\n" 1.223 + "limit is %d urls/threads", maxThreads ); 1.224 + return -1; 1.225 + } 1.226 + 1.227 + nsresult rv= (nsresult)-1; 1.228 + 1.229 + printf( "Test starting...\n" ); 1.230 + 1.231 + // Initialize XPCOM. 1.232 + printf( "Initializing XPCOM...\n" ); 1.233 + rv = NS_InitXPCOM2(nullptr, nullptr, nullptr); 1.234 + if ( NS_FAILED( rv ) ) { 1.235 + printf( "%s %d: NS_InitXPCOM failed, rv=0x%08X\n", 1.236 + (char*)__FILE__, (int)__LINE__, (int)rv ); 1.237 + return rv; 1.238 + } 1.239 + printf( "...XPCOM initialized OK\n" ); 1.240 + // Create the Event Queue for this thread... 1.241 + printf( "Creating event queue for main thread (0x%08X)...\n", 1.242 + (int)(void*)PR_GetCurrentThread() ); 1.243 + { 1.244 + nsCOMPtr<nsIEventQueue> mainThreadQ = createEventQueue(); 1.245 + 1.246 + if ( mainThreadQ ) { 1.247 + printf( "...main thread's event queue created OK\n" ); 1.248 + 1.249 + // Spawn threads to do I/O. 1.250 + int goodThreads = 0; 1.251 + PRThread *thread[ maxThreads ]; 1.252 + for ( int threadNo = 1; threadNo < argc; threadNo++ ) { 1.253 + printf( "Creating I/O thread %d to load %s...\n", threadNo, argv[threadNo] ); 1.254 + PRThread *ioThread = PR_CreateThread( PR_USER_THREAD, 1.255 + TestListener::IOThread, 1.256 + argv[threadNo], 1.257 + PR_PRIORITY_NORMAL, 1.258 + PR_GLOBAL_THREAD, 1.259 + PR_JOINABLE_THREAD, 1.260 + 0 ); 1.261 + if ( ioThread ) { 1.262 + thread[ goodThreads++ ] = ioThread; 1.263 + printf( "...I/O thread %d (0x%08X) created OK\n", 1.264 + threadNo, (int)(void*)ioThread ); 1.265 + } else { 1.266 + printf( "%s %d: PR_CreateThread for thread %d failed\n", 1.267 + (char*)__FILE__, (int)__LINE__, threadNo ); 1.268 + } 1.269 + } 1.270 + 1.271 + // Wait for all the threads to terminate. 1.272 + for ( int joinThread = 0; joinThread < goodThreads; joinThread++ ) { 1.273 + printf( "Waiting for thread %d to terminate...\n", joinThread+1 ); 1.274 + PR_JoinThread( thread[ joinThread ] ); 1.275 + } 1.276 + } 1.277 + } // this scopes the nsCOMPtrs 1.278 + // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM 1.279 + // Shut down XPCOM. 1.280 + printf( "Shutting down XPCOM...\n" ); 1.281 + NS_ShutdownXPCOM( 0 ); 1.282 + printf( "...XPCOM shutdown complete\n" ); 1.283 + 1.284 + // Exit. 1.285 + printf( "...test complete, rv=0x%08X\n", (int)rv ); 1.286 + return rv; 1.287 +}