1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsIEventQueueService.h"
8 #include "nsIServiceManager.h"
9 #include "nsIStreamListener.h"
11 #include "nsNetUtil.h"
13 //#include "prthread.h"
15 // This test attempts to load a URL on a separate thread. It is currently
16 // designed simply to expose the problems inherent in such an ambitous task
17 // (i.e., it don't work).
19 // Utility functions...
21 // Create event queue for current thread.
22 static nsCOMPtr
<nsIEventQueue
>
24 nsCOMPtr
<nsIEventQueue
> result
;
25 // Get event queue service.
27 nsCOMPtr
<nsIEventQueueService
> eqs
=
28 do_GetService(NS_EVENTQUEUESERVICE_CONTRACTID
, &rv
);
29 if ( NS_SUCCEEDED( rv
) ) {
30 eqs
->GetThreadEventQueue(NS_CURRENT_THREAD
, getter_AddRefs(result
));
32 printf( "%s %d: NS_WITH_SERVICE(nsIEventQueueService) failed, rv=0x%08X\n",
33 (char*)__FILE__
, (int)__LINE__
, (int)rv
);
38 // Create channel for requested URL.
39 static nsCOMPtr
<nsIChannel
>
40 createChannel( const char *url
) {
41 nsCOMPtr
<nsIInputStream
> result
;
44 printf( "Calling NS_NewURI for %s...\n", url
);
45 nsresult rv
= NS_NewURI( getter_AddRefs( uri
), url
);
47 if ( NS_SUCCEEDED( rv
) ) {
48 printf( "...NS_NewURI completed OK\n" );
50 // Allocate a new input channel on this thread.
51 printf( "Calling NS_OpenURI...\n" );
52 nsresult rv
= NS_OpenURI( getter_AddRefs( result
), uri
, 0 );
54 if ( NS_SUCCEEDED( rv
) ) {
55 printf( "...NS_OpenURI completed OK\n" );
57 printf( "%s %d: NS_OpenURI failed, rv=0x%08X\n",
58 (char*)__FILE__
, (int)__LINE__
, (int)rv
);
61 printf( "%s %d: NS_NewURI failed, rv=0x%08X\n",
62 (char*)__FILE__
, (int)__LINE__
, (int)rv
);
67 // Test listener. It basically dumps incoming data to console.
68 class TestListener
: public nsIStreamListener
{
71 NS_DECL_NSISTREAMLISTENER
72 NS_DECL_NSISTREAMOBSERVER
76 static void IOThread( void *p
);
82 static int threadCount
;
83 }; // class TestListener
85 int TestListener::threadCount
= 0;
87 TestListener::TestListener()
88 : mDone( false ), mThreadNo( ++threadCount
) {
89 printf( "TestListener ctor called on thread %d\n", mThreadNo
);
92 TestListener::~TestListener() {
93 printf( "TestListener dtor called on thread %d\n", mThreadNo
);
96 NS_IMPL_ISUPPORTS( TestListener
, nsIStreamListener
, nsIRequestObserver
)
99 TestListener::OnStartRequest( nsIChannel
*aChannel
, nsISupports
*aContext
) {
102 printf( "TestListener::OnStartRequest called on thread %d\n", mThreadNo
);
106 sprintf( fileName
, "%s%d", "thread", mThreadNo
);
107 mFile
= fopen( fileName
, "wb" );
114 TestListener::OnStopRequest( nsIChannel
*aChannel
,
115 nsISupports
*aContext
,
117 const char16_t
*aMsg
) {
120 printf( "TestListener::OnStopRequest called on thread %d\n", mThreadNo
);
129 TestListener::OnDataAvailable( nsIChannel
*aChannel
,
130 nsISupports
*aContext
,
131 nsIInputStream
*aStream
,
136 printf( "TestListener::OnDataAvailable called on thread %d\n", mThreadNo
);
138 // Write the data to the console.
139 // Read a buffer full till aLength bytes have been processed.
141 unsigned long bytesRemaining
= aLength
;
142 while ( bytesRemaining
) {
143 unsigned int bytesRead
;
144 // Read a buffer full or the number remaining (whichever is smaller).
145 rv
= aStream
->Read( buffer
,
146 std::min( sizeof( buffer
), bytesRemaining
),
148 if ( NS_SUCCEEDED( rv
) ) {
149 // Write the bytes just read to the output file.
150 fwrite( buffer
, 1, bytesRead
, mFile
);
151 bytesRemaining
-= bytesRead
;
153 printf( "%s %d: Read error, rv=0x%08X\n",
154 (char*)__FILE__
, (int)__LINE__
, (int)rv
);
163 // IOThread: this function creates a new TestListener object (on the new
164 // thread), opens a channel, and does AsyncRead to it.
166 TestListener::IOThread( void *p
) {
167 printf( "I/O thread (0x%08X) started...\n", (int)(void*)PR_GetCurrentThread() );
169 // Argument is pointer to the nsIEventQueue for the main thread.
170 nsIEventQueue
*mainThreadQ
= static_cast<nsIEventQueue
*>(p
);
172 // Create channel for random web page.
173 nsCOMPtr
<nsIChannel
> channel
= createChannel( (const char*)p
);
176 // Create event queue.
177 nsCOMPtr
<nsIEventQueue
> ioEventQ
= createEventQueue();
180 // Create test listener.
181 TestListener
*testListener
= new TestListener();
182 testListener
->AddRef();
185 printf( "Doing AsyncRead...\n" );
186 nsresult rv
= channel
->AsyncRead( testListener
, 0 );
188 if ( NS_SUCCEEDED( rv
) ) {
189 printf( "...AsyncRead completed OK\n" );
191 // Process events till testListener says stop.
192 printf( "Start event loop on io thread %d...\n", testListener
->mThreadNo
);
193 while ( !testListener
->mDone
) {
195 ioEventQ
->GetEvent( &event
);
196 ioEventQ
->HandleEvent( event
);
198 printf( "...io thread %d event loop exiting\n", testListener
->mThreadNo
);
200 printf( "%s %d: AsyncRead failed on thread %d, rv=0x%08X\n",
201 (char*)__FILE__
, (int)__LINE__
, testListener
->mThreadNo
, (int)rv
);
204 // Release the test listener.
205 testListener
->Release();
209 printf( "...I/O thread terminating\n" );
212 static const int maxThreads
= 5;
215 main( int argc
, char* argv
[] ) {
217 if ( argc
< 2 || argc
> maxThreads
+ 1 ) {
218 printf( "usage: testThreadedIO url1 <url2>...\n"
219 "where <url#> is a location to be loaded on a separate thread\n"
220 "limit is %d urls/threads", maxThreads
);
224 nsresult rv
= (nsresult
)-1;
226 printf( "Test starting...\n" );
229 printf( "Initializing XPCOM...\n" );
230 rv
= NS_InitXPCOM2(nullptr, nullptr, nullptr);
231 if ( NS_FAILED( rv
) ) {
232 printf( "%s %d: NS_InitXPCOM failed, rv=0x%08X\n",
233 (char*)__FILE__
, (int)__LINE__
, (int)rv
);
236 printf( "...XPCOM initialized OK\n" );
237 // Create the Event Queue for this thread...
238 printf( "Creating event queue for main thread (0x%08X)...\n",
239 (int)(void*)PR_GetCurrentThread() );
241 nsCOMPtr
<nsIEventQueue
> mainThreadQ
= createEventQueue();
244 printf( "...main thread's event queue created OK\n" );
246 // Spawn threads to do I/O.
248 PRThread
*thread
[ maxThreads
];
249 for ( int threadNo
= 1; threadNo
< argc
; threadNo
++ ) {
250 printf( "Creating I/O thread %d to load %s...\n", threadNo
, argv
[threadNo
] );
251 PRThread
*ioThread
= PR_CreateThread( PR_USER_THREAD
,
252 TestListener::IOThread
,
259 thread
[ goodThreads
++ ] = ioThread
;
260 printf( "...I/O thread %d (0x%08X) created OK\n",
261 threadNo
, (int)(void*)ioThread
);
263 printf( "%s %d: PR_CreateThread for thread %d failed\n",
264 (char*)__FILE__
, (int)__LINE__
, threadNo
);
268 // Wait for all the threads to terminate.
269 for ( int joinThread
= 0; joinThread
< goodThreads
; joinThread
++ ) {
270 printf( "Waiting for thread %d to terminate...\n", joinThread
+1 );
271 PR_JoinThread( thread
[ joinThread
] );
274 } // this scopes the nsCOMPtrs
275 // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
277 printf( "Shutting down XPCOM...\n" );
278 NS_ShutdownXPCOM( 0 );
279 printf( "...XPCOM shutdown complete\n" );
282 printf( "...test complete, rv=0x%08X\n", (int)rv
);