1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_cppu.hxx" 30*cdf0e10cSrcweir #include <stdio.h> 31*cdf0e10cSrcweir #include <osl/diagnose.h> 32*cdf0e10cSrcweir #include <uno/threadpool.h> 33*cdf0e10cSrcweir 34*cdf0e10cSrcweir #include <rtl/instance.hxx> 35*cdf0e10cSrcweir 36*cdf0e10cSrcweir #include "thread.hxx" 37*cdf0e10cSrcweir #include "jobqueue.hxx" 38*cdf0e10cSrcweir #include "threadpool.hxx" 39*cdf0e10cSrcweir 40*cdf0e10cSrcweir 41*cdf0e10cSrcweir using namespace osl; 42*cdf0e10cSrcweir extern "C" { 43*cdf0e10cSrcweir 44*cdf0e10cSrcweir void SAL_CALL cppu_requestThreadWorker( void *pVoid ) 45*cdf0e10cSrcweir { 46*cdf0e10cSrcweir ::cppu_threadpool::ORequestThread *pThread = ( ::cppu_threadpool::ORequestThread * ) pVoid; 47*cdf0e10cSrcweir 48*cdf0e10cSrcweir pThread->run(); 49*cdf0e10cSrcweir pThread->onTerminated(); 50*cdf0e10cSrcweir } 51*cdf0e10cSrcweir 52*cdf0e10cSrcweir } 53*cdf0e10cSrcweir 54*cdf0e10cSrcweir namespace cppu_threadpool { 55*cdf0e10cSrcweir 56*cdf0e10cSrcweir // ---------------------------------------------------------------------------------- 57*cdf0e10cSrcweir ThreadAdmin::~ThreadAdmin() 58*cdf0e10cSrcweir { 59*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1 60*cdf0e10cSrcweir if( m_lst.size() ) 61*cdf0e10cSrcweir { 62*cdf0e10cSrcweir fprintf( stderr, "%lu Threads left\n" , static_cast<unsigned long>(m_lst.size()) ); 63*cdf0e10cSrcweir } 64*cdf0e10cSrcweir #endif 65*cdf0e10cSrcweir } 66*cdf0e10cSrcweir 67*cdf0e10cSrcweir void ThreadAdmin::add( ORequestThread *p ) 68*cdf0e10cSrcweir { 69*cdf0e10cSrcweir MutexGuard aGuard( m_mutex ); 70*cdf0e10cSrcweir m_lst.push_back( p ); 71*cdf0e10cSrcweir } 72*cdf0e10cSrcweir 73*cdf0e10cSrcweir void ThreadAdmin::remove( ORequestThread * p ) 74*cdf0e10cSrcweir { 75*cdf0e10cSrcweir MutexGuard aGuard( m_mutex ); 76*cdf0e10cSrcweir ::std::list< ORequestThread * >::iterator ii = ::std::find( m_lst.begin(), m_lst.end(), p ); 77*cdf0e10cSrcweir OSL_ASSERT( ii != m_lst.end() ); 78*cdf0e10cSrcweir m_lst.erase( ii ); 79*cdf0e10cSrcweir } 80*cdf0e10cSrcweir 81*cdf0e10cSrcweir void ThreadAdmin::join() 82*cdf0e10cSrcweir { 83*cdf0e10cSrcweir ORequestThread *pCurrent; 84*cdf0e10cSrcweir do 85*cdf0e10cSrcweir { 86*cdf0e10cSrcweir pCurrent = 0; 87*cdf0e10cSrcweir { 88*cdf0e10cSrcweir MutexGuard aGuard( m_mutex ); 89*cdf0e10cSrcweir if( ! m_lst.empty() ) 90*cdf0e10cSrcweir { 91*cdf0e10cSrcweir pCurrent = m_lst.front(); 92*cdf0e10cSrcweir pCurrent->setDeleteSelf( sal_False ); 93*cdf0e10cSrcweir } 94*cdf0e10cSrcweir } 95*cdf0e10cSrcweir if ( pCurrent ) 96*cdf0e10cSrcweir { 97*cdf0e10cSrcweir pCurrent->join(); 98*cdf0e10cSrcweir delete pCurrent; 99*cdf0e10cSrcweir } 100*cdf0e10cSrcweir } while( pCurrent ); 101*cdf0e10cSrcweir } 102*cdf0e10cSrcweir 103*cdf0e10cSrcweir struct theThreadAdmin : public rtl::StaticWithInit< ThreadAdminHolder, theThreadAdmin > 104*cdf0e10cSrcweir { 105*cdf0e10cSrcweir ThreadAdminHolder operator () () { 106*cdf0e10cSrcweir ThreadAdminHolder aRet(new ThreadAdmin()); 107*cdf0e10cSrcweir return aRet; 108*cdf0e10cSrcweir } 109*cdf0e10cSrcweir }; 110*cdf0e10cSrcweir 111*cdf0e10cSrcweir ThreadAdminHolder& ThreadAdmin::getInstance() 112*cdf0e10cSrcweir { 113*cdf0e10cSrcweir return theThreadAdmin::get(); 114*cdf0e10cSrcweir } 115*cdf0e10cSrcweir 116*cdf0e10cSrcweir // ---------------------------------------------------------------------------------- 117*cdf0e10cSrcweir ORequestThread::ORequestThread( JobQueue *pQueue, 118*cdf0e10cSrcweir const ByteSequence &aThreadId, 119*cdf0e10cSrcweir sal_Bool bAsynchron ) 120*cdf0e10cSrcweir : m_thread( 0 ) 121*cdf0e10cSrcweir , m_aThreadAdmin( ThreadAdmin::getInstance() ) 122*cdf0e10cSrcweir , m_pQueue( pQueue ) 123*cdf0e10cSrcweir , m_aThreadId( aThreadId ) 124*cdf0e10cSrcweir , m_bAsynchron( bAsynchron ) 125*cdf0e10cSrcweir , m_bDeleteSelf( sal_True ) 126*cdf0e10cSrcweir { 127*cdf0e10cSrcweir m_aThreadAdmin->add( this ); 128*cdf0e10cSrcweir } 129*cdf0e10cSrcweir 130*cdf0e10cSrcweir 131*cdf0e10cSrcweir ORequestThread::~ORequestThread() 132*cdf0e10cSrcweir { 133*cdf0e10cSrcweir if (m_thread != 0) 134*cdf0e10cSrcweir { 135*cdf0e10cSrcweir osl_destroyThread(m_thread); 136*cdf0e10cSrcweir } 137*cdf0e10cSrcweir } 138*cdf0e10cSrcweir 139*cdf0e10cSrcweir 140*cdf0e10cSrcweir void ORequestThread::setTask( JobQueue *pQueue, 141*cdf0e10cSrcweir const ByteSequence &aThreadId, 142*cdf0e10cSrcweir sal_Bool bAsynchron ) 143*cdf0e10cSrcweir { 144*cdf0e10cSrcweir m_pQueue = pQueue; 145*cdf0e10cSrcweir m_aThreadId = aThreadId; 146*cdf0e10cSrcweir m_bAsynchron = bAsynchron; 147*cdf0e10cSrcweir } 148*cdf0e10cSrcweir 149*cdf0e10cSrcweir sal_Bool ORequestThread::create() 150*cdf0e10cSrcweir { 151*cdf0e10cSrcweir OSL_ASSERT(m_thread == 0); // only one running thread per instance 152*cdf0e10cSrcweir 153*cdf0e10cSrcweir m_thread = osl_createSuspendedThread( cppu_requestThreadWorker, (void*)this); 154*cdf0e10cSrcweir if ( m_thread ) 155*cdf0e10cSrcweir { 156*cdf0e10cSrcweir osl_resumeThread( m_thread ); 157*cdf0e10cSrcweir } 158*cdf0e10cSrcweir 159*cdf0e10cSrcweir return m_thread != 0; 160*cdf0e10cSrcweir } 161*cdf0e10cSrcweir 162*cdf0e10cSrcweir void ORequestThread::join() 163*cdf0e10cSrcweir { 164*cdf0e10cSrcweir osl_joinWithThread( m_thread ); 165*cdf0e10cSrcweir } 166*cdf0e10cSrcweir 167*cdf0e10cSrcweir void ORequestThread::onTerminated() 168*cdf0e10cSrcweir { 169*cdf0e10cSrcweir m_aThreadAdmin->remove( this ); 170*cdf0e10cSrcweir if( m_bDeleteSelf ) 171*cdf0e10cSrcweir { 172*cdf0e10cSrcweir delete this; 173*cdf0e10cSrcweir } 174*cdf0e10cSrcweir } 175*cdf0e10cSrcweir 176*cdf0e10cSrcweir void ORequestThread::run() 177*cdf0e10cSrcweir { 178*cdf0e10cSrcweir ThreadPoolHolder theThreadPool = cppu_threadpool::ThreadPool::getInstance(); 179*cdf0e10cSrcweir 180*cdf0e10cSrcweir while ( m_pQueue ) 181*cdf0e10cSrcweir { 182*cdf0e10cSrcweir if( ! m_bAsynchron ) 183*cdf0e10cSrcweir { 184*cdf0e10cSrcweir if ( !uno_bindIdToCurrentThread( m_aThreadId.getHandle() ) ) 185*cdf0e10cSrcweir { 186*cdf0e10cSrcweir OSL_ASSERT( false ); 187*cdf0e10cSrcweir } 188*cdf0e10cSrcweir } 189*cdf0e10cSrcweir 190*cdf0e10cSrcweir while( ! m_pQueue->isEmpty() ) 191*cdf0e10cSrcweir { 192*cdf0e10cSrcweir // Note : Oneways should not get a disposable disposeid, 193*cdf0e10cSrcweir // It does not make sense to dispose a call in this state. 194*cdf0e10cSrcweir // That's way we put it an disposeid, that can't be used otherwise. 195*cdf0e10cSrcweir m_pQueue->enter( 196*cdf0e10cSrcweir sal::static_int_cast< sal_Int64 >( 197*cdf0e10cSrcweir reinterpret_cast< sal_IntPtr >(this)), 198*cdf0e10cSrcweir sal_True ); 199*cdf0e10cSrcweir 200*cdf0e10cSrcweir if( m_pQueue->isEmpty() ) 201*cdf0e10cSrcweir { 202*cdf0e10cSrcweir theThreadPool->revokeQueue( m_aThreadId , m_bAsynchron ); 203*cdf0e10cSrcweir // Note : revokeQueue might have failed because m_pQueue.isEmpty() 204*cdf0e10cSrcweir // may be false (race). 205*cdf0e10cSrcweir } 206*cdf0e10cSrcweir } 207*cdf0e10cSrcweir 208*cdf0e10cSrcweir delete m_pQueue; 209*cdf0e10cSrcweir m_pQueue = 0; 210*cdf0e10cSrcweir 211*cdf0e10cSrcweir if( ! m_bAsynchron ) 212*cdf0e10cSrcweir { 213*cdf0e10cSrcweir uno_releaseIdFromCurrentThread(); 214*cdf0e10cSrcweir } 215*cdf0e10cSrcweir 216*cdf0e10cSrcweir theThreadPool->waitInPool( this ); 217*cdf0e10cSrcweir } 218*cdf0e10cSrcweir } 219*cdf0e10cSrcweir } 220