update dev300-m58
[ooovba.git] / cppu / test / testthreadpool.cxx
blob38ec017705914b2af40ad9759cab61b176c4922c
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: testthreadpool.cxx,v $
10 * $Revision: 1.6 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_cppu.hxx"
33 #include <osl/diagnose.h>
34 #include <osl/time.h>
36 #include <rtl/uuid.h>
38 #include <uno/threadpool.h>
40 #include <vos/thread.hxx>
42 #define TEST_ENSURE OSL_ENSURE
44 using namespace ::vos;
47 class OThread1 : public OThread
49 public:
50 OThread1( sal_uInt8 *pCallerUuid );
51 virtual void run();
53 public:
54 sal_uInt8 *m_pCallerUuid;
55 sal_Int8 *m_pThreadIdentifier;
56 sal_Int32 m_nThreadIdentifierLength;
58 void *m_pThreadData;
61 OThread1::OThread1( sal_uInt8 *pCallerUuid ) :
62 m_pCallerUuid( pCallerUuid ),
63 m_pThreadData( (void*) 1 ),
64 m_pThreadIdentifier( 0 ),
65 m_nThreadIdentifierLength( 0 )
70 void OThread1::run()
73 uno_threadpool_Ticket *pTicket = uno_threadpool_createTicket( m_pCallerUuid );
75 uno_threadIdent_retrieve( &m_pThreadIdentifier , &m_nThreadIdentifierLength );
77 uno_threadpool_waitOnTicket( pTicket , &m_pThreadData );
79 uno_threadIdent_revoke();
83 void SAL_CALL doIt( void *pThreadData )
85 *( sal_Int32 *) pThreadData = 2;
88 void testthreadpool()
90 printf( "Testing threadpool ..." );
91 fflush( stdout );
93 sal_uInt8 pCallerUuid1[16];
94 sal_uInt8 pCallerUuid2[16];
95 rtl_createUuid( pCallerUuid1, 0 , sal_True );
96 rtl_createUuid( pCallerUuid2, 0 , sal_True );
98 //------------
99 // Test reply
100 //------------
102 OThread1 thread1( pCallerUuid1 );
104 thread1.create();
106 // do a busy wait
107 while( ! thread1.m_pThreadIdentifier && ! thread1.m_nThreadIdentifierLength );
109 void *pThreadData = (void*)0xdeadbabe;
110 uno_threadpool_reply( thread1.m_pThreadIdentifier ,
111 thread1.m_nThreadIdentifierLength,
112 pThreadData );
114 // do a busy wait
115 while( (void*)1 == thread1.m_pThreadData );
117 TEST_ENSURE( pThreadData == thread1.m_pThreadData, "uno_threadpool_reply error" );
120 //---------------
121 // Test request
122 //---------------
124 OThread1 thread1( pCallerUuid1 );
126 thread1.create();
128 // do a busy wait
129 while( ! thread1.m_pThreadIdentifier && ! thread1.m_nThreadIdentifierLength );
131 // do a request
132 sal_Int32 i = 1;
133 uno_threadpool_request( thread1.m_pThreadIdentifier ,
134 thread1.m_nThreadIdentifierLength,
136 doIt,
137 sal_False);
139 // do a busy wait
140 while( 1 == i );
141 TEST_ENSURE( 2 == i, "uno_threadpool_request error" );
143 // get it out of the pool
144 void *pThreadData = (void*)0xdeadbabe;
145 uno_threadpool_reply( thread1.m_pThreadIdentifier ,
146 thread1.m_nThreadIdentifierLength,
147 pThreadData );
149 // do a busy wait
150 while( pThreadData != thread1.m_pThreadData );
154 //---------------
155 // Test dispose threads
156 //---------------
158 OThread1 thread1( pCallerUuid1 );
159 OThread1 thread2( pCallerUuid2 );
161 thread1.create();
162 thread2.create();
164 // do a busy wait
165 while( ! thread1.m_pThreadIdentifier && ! thread1.m_nThreadIdentifierLength &&
166 ! thread2.m_pThreadIdentifier && ! thread2.m_nThreadIdentifierLength );
168 // dispose the first
169 uno_threadpool_disposeThreads( pCallerUuid1 );
171 while( (void*)1 == thread1.m_pThreadData );
172 TEST_ENSURE( (void*)0 == thread1.m_pThreadData, "disposing threads failed" );
174 TimeValue value = {1,0};
175 osl_waitThread( &value );
176 TEST_ENSURE( (void*)1 == thread2.m_pThreadData, "wrong thread disposed !" );
178 // test, if new threads are directly disposed
179 OThread1 thread3( pCallerUuid1 );
180 thread3.create();
182 while( (void*)1 == thread3.m_pThreadData );
183 TEST_ENSURE( (void*)0 == thread3.m_pThreadData ,
184 "new threads entering threadpool are not disposed" );
186 uno_threadpool_reply( thread2.m_pThreadIdentifier ,
187 thread2.m_nThreadIdentifierLength,
188 (void*)0x2 );
190 while( (void*)1 == thread2.m_pThreadData );
191 TEST_ENSURE( (void*)2 == thread2.m_pThreadData , "reply does not work correctly" );
193 uno_threadpool_stopDisposeThreads( pCallerUuid1 );
195 printf( "Done\n" );