1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: testthreadpool.cxx,v $
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>
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
50 OThread1( sal_uInt8
*pCallerUuid
);
54 sal_uInt8
*m_pCallerUuid
;
55 sal_Int8
*m_pThreadIdentifier
;
56 sal_Int32 m_nThreadIdentifierLength
;
61 OThread1::OThread1( sal_uInt8
*pCallerUuid
) :
62 m_pCallerUuid( pCallerUuid
),
63 m_pThreadData( (void*) 1 ),
64 m_pThreadIdentifier( 0 ),
65 m_nThreadIdentifierLength( 0 )
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;
90 printf( "Testing threadpool ..." );
93 sal_uInt8 pCallerUuid1
[16];
94 sal_uInt8 pCallerUuid2
[16];
95 rtl_createUuid( pCallerUuid1
, 0 , sal_True
);
96 rtl_createUuid( pCallerUuid2
, 0 , sal_True
);
102 OThread1
thread1( pCallerUuid1
);
107 while( ! thread1
.m_pThreadIdentifier
&& ! thread1
.m_nThreadIdentifierLength
);
109 void *pThreadData
= (void*)0xdeadbabe;
110 uno_threadpool_reply( thread1
.m_pThreadIdentifier
,
111 thread1
.m_nThreadIdentifierLength
,
115 while( (void*)1 == thread1
.m_pThreadData
);
117 TEST_ENSURE( pThreadData
== thread1
.m_pThreadData
, "uno_threadpool_reply error" );
124 OThread1
thread1( pCallerUuid1
);
129 while( ! thread1
.m_pThreadIdentifier
&& ! thread1
.m_nThreadIdentifierLength
);
133 uno_threadpool_request( thread1
.m_pThreadIdentifier
,
134 thread1
.m_nThreadIdentifierLength
,
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
,
150 while( pThreadData
!= thread1
.m_pThreadData
);
155 // Test dispose threads
158 OThread1
thread1( pCallerUuid1
);
159 OThread1
thread2( pCallerUuid2
);
165 while( ! thread1
.m_pThreadIdentifier
&& ! thread1
.m_nThreadIdentifierLength
&&
166 ! thread2
.m_pThreadIdentifier
&& ! thread2
.m_nThreadIdentifierLength
);
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
);
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
,
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
);