1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
23 * Bill Law <law@netscape.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
40 #include "nsIEventQueueService.h"
41 #include "nsIServiceManager.h"
42 #include "nsIStreamListener.h"
44 #include "nsNetUtil.h"
45 //#include "prthread.h"
47 // This test attempts to load a URL on a separate thread. It is currently
48 // designed simply to expose the problems inherent in such an ambitous task
49 // (i.e., it don't work).
51 // Utility functions...
53 // Create event queue for current thread.
54 static nsCOMPtr
<nsIEventQueue
>
56 nsCOMPtr
<nsIEventQueue
> result
;
57 // Get event queue service.
59 nsCOMPtr
<nsIEventQueueService
> eqs
=
60 do_GetService(NS_EVENTQUEUESERVICE_CONTRACTID
, &rv
);
61 if ( NS_SUCCEEDED( rv
) ) {
62 eqs
->GetThreadEventQueue(NS_CURRENT_THREAD
, getter_AddRefs(result
));
64 printf( "%s %d: NS_WITH_SERVICE(nsIEventQueueService) failed, rv=0x%08X\n",
65 (char*)__FILE__
, (int)__LINE__
, (int)rv
);
70 // Create channel for requested URL.
71 static nsCOMPtr
<nsIChannel
>
72 createChannel( const char *url
) {
73 nsCOMPtr
<nsIInputStream
> result
;
76 printf( "Calling NS_NewURI for %s...\n", url
);
77 nsresult rv
= NS_NewURI( getter_AddRefs( uri
), url
);
79 if ( NS_SUCCEEDED( rv
) ) {
80 printf( "...NS_NewURI completed OK\n" );
82 // Allocate a new input channel on this thread.
83 printf( "Calling NS_OpenURI...\n" );
84 nsresult rv
= NS_OpenURI( getter_AddRefs( result
), uri
, 0 );
86 if ( NS_SUCCEEDED( rv
) ) {
87 printf( "...NS_OpenURI completed OK\n" );
89 printf( "%s %d: NS_OpenURI failed, rv=0x%08X\n",
90 (char*)__FILE__
, (int)__LINE__
, (int)rv
);
93 printf( "%s %d: NS_NewURI failed, rv=0x%08X\n",
94 (char*)__FILE__
, (int)__LINE__
, (int)rv
);
99 // Test listener. It basically dumps incoming data to console.
100 class TestListener
: public nsIStreamListener
{
103 NS_DECL_NSISTREAMLISTENER
104 NS_DECL_NSISTREAMOBSERVER
108 static void IOThread( void *p
);
114 static int threadCount
;
115 }; // class TestListener
117 int TestListener::threadCount
= 0;
119 TestListener::TestListener()
120 : mDone( PR_FALSE
), mThreadNo( ++threadCount
) {
121 printf( "TestListener ctor called on thread %d\n", mThreadNo
);
124 TestListener::~TestListener() {
125 printf( "TestListener dtor called on thread %d\n", mThreadNo
);
128 NS_IMPL_ISUPPORTS2( TestListener
, nsIStreamListener
, nsIRequestObserver
)
131 TestListener::OnStartRequest( nsIChannel
*aChannel
, nsISupports
*aContext
) {
134 printf( "TestListener::OnStartRequest called on thread %d\n", mThreadNo
);
138 sprintf( fileName
, "%s%d", "thread", mThreadNo
);
139 mFile
= fopen( fileName
, "wb" );
146 TestListener::OnStopRequest( nsIChannel
*aChannel
,
147 nsISupports
*aContext
,
149 const PRUnichar
*aMsg
) {
152 printf( "TestListener::OnStopRequest called on thread %d\n", mThreadNo
);
161 TestListener::OnDataAvailable( nsIChannel
*aChannel
,
162 nsISupports
*aContext
,
163 nsIInputStream
*aStream
,
168 printf( "TestListener::OnDataAvailable called on thread %d\n", mThreadNo
);
170 // Write the data to the console.
171 // Read a buffer full till aLength bytes have been processed.
173 unsigned long bytesRemaining
= aLength
;
174 while ( bytesRemaining
) {
175 unsigned int bytesRead
;
176 // Read a buffer full or the number remaining (whichever is smaller).
177 rv
= aStream
->Read( buffer
,
178 PR_MIN( sizeof( buffer
), bytesRemaining
),
180 if ( NS_SUCCEEDED( rv
) ) {
181 // Write the bytes just read to the output file.
182 fwrite( buffer
, 1, bytesRead
, mFile
);
183 bytesRemaining
-= bytesRead
;
185 printf( "%s %d: Read error, rv=0x%08X\n",
186 (char*)__FILE__
, (int)__LINE__
, (int)rv
);
195 // IOThread: this function creates a new TestListener object (on the new
196 // thread), opens a channel, and does AsyncRead to it.
198 TestListener::IOThread( void *p
) {
199 printf( "I/O thread (0x%08X) started...\n", (int)(void*)PR_GetCurrentThread() );
201 // Argument is pointer to the nsIEventQueue for the main thread.
202 nsIEventQueue
*mainThreadQ
= static_cast<nsIEventQueue
*>(p
);
204 // Create channel for random web page.
205 nsCOMPtr
<nsIChannel
> channel
= createChannel( (const char*)p
);
208 // Create event queue.
209 nsCOMPtr
<nsIEventQueue
> ioEventQ
= createEventQueue();
212 // Create test listener.
213 TestListener
*testListener
= new TestListener();
214 testListener
->AddRef();
217 printf( "Doing AsyncRead...\n" );
218 nsresult rv
= channel
->AsyncRead( testListener
, 0 );
220 if ( NS_SUCCEEDED( rv
) ) {
221 printf( "...AsyncRead completed OK\n" );
223 // Process events till testListener says stop.
224 printf( "Start event loop on io thread %d...\n", testListener
->mThreadNo
);
225 while ( !testListener
->mDone
) {
227 ioEventQ
->GetEvent( &event
);
228 ioEventQ
->HandleEvent( event
);
230 printf( "...io thread %d event loop exiting\n", testListener
->mThreadNo
);
232 printf( "%s %d: AsyncRead failed on thread %d, rv=0x%08X\n",
233 (char*)__FILE__
, (int)__LINE__
, testListener
->mThreadNo
, (int)rv
);
236 // Release the test listener.
237 testListener
->Release();
241 printf( "...I/O thread terminating\n" );
244 static const int maxThreads
= 5;
247 main( int argc
, char* argv
[] ) {
249 if ( argc
< 2 || argc
> maxThreads
+ 1 ) {
250 printf( "usage: testThreadedIO url1 <url2>...\n"
251 "where <url#> is a location to be loaded on a separate thread\n"
252 "limit is %d urls/threads", maxThreads
);
256 nsresult rv
= (nsresult
)-1;
258 printf( "Test starting...\n" );
261 printf( "Initializing XPCOM...\n" );
262 rv
= NS_InitXPCOM2(nsnull
, nsnull
, nsnull
);
263 if ( NS_FAILED( rv
) ) {
264 printf( "%s %d: NS_InitXPCOM failed, rv=0x%08X\n",
265 (char*)__FILE__
, (int)__LINE__
, (int)rv
);
268 printf( "...XPCOM initialized OK\n" );
269 // Create the Event Queue for this thread...
270 printf( "Creating event queue for main thread (0x%08X)...\n",
271 (int)(void*)PR_GetCurrentThread() );
273 nsCOMPtr
<nsIEventQueue
> mainThreadQ
= createEventQueue();
276 printf( "...main thread's event queue created OK\n" );
278 // Spawn threads to do I/O.
280 PRThread
*thread
[ maxThreads
];
281 for ( int threadNo
= 1; threadNo
< argc
; threadNo
++ ) {
282 printf( "Creating I/O thread %d to load %s...\n", threadNo
, argv
[threadNo
] );
283 PRThread
*ioThread
= PR_CreateThread( PR_USER_THREAD
,
284 TestListener::IOThread
,
291 thread
[ goodThreads
++ ] = ioThread
;
292 printf( "...I/O thread %d (0x%08X) created OK\n",
293 threadNo
, (int)(void*)ioThread
);
295 printf( "%s %d: PR_CreateThread for thread %d failed\n",
296 (char*)__FILE__
, (int)__LINE__
, threadNo
);
300 // Wait for all the threads to terminate.
301 for ( int joinThread
= 0; joinThread
< goodThreads
; joinThread
++ ) {
302 printf( "Waiting for thread %d to terminate...\n", joinThread
+1 );
303 PR_JoinThread( thread
[ joinThread
] );
306 } // this scopes the nsCOMPtrs
307 // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
309 printf( "Shutting down XPCOM...\n" );
310 NS_ShutdownXPCOM( 0 );
311 printf( "...XPCOM shutdown complete\n" );
314 printf( "...test complete, rv=0x%08X\n", (int)rv
);