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: thread.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"
34 #include <osl/diagnose.h>
35 #include <uno/threadpool.h>
38 #include "jobqueue.hxx"
39 #include "threadpool.hxx"
45 void SAL_CALL
cppu_requestThreadWorker( void *pVoid
)
47 ::cppu_threadpool::ORequestThread
*pThread
= ( ::cppu_threadpool::ORequestThread
* ) pVoid
;
50 pThread
->onTerminated();
55 namespace cppu_threadpool
{
57 // ----------------------------------------------------------------------------------
58 ThreadAdmin::~ThreadAdmin()
60 #if OSL_DEBUG_LEVEL > 1
63 fprintf( stderr
, "%lu Threads left\n" , static_cast<unsigned long>(m_lst
.size()) );
68 void ThreadAdmin::add( ORequestThread
*p
)
70 MutexGuard
aGuard( m_mutex
);
74 void ThreadAdmin::remove( ORequestThread
* p
)
76 MutexGuard
aGuard( m_mutex
);
77 ::std::list
< ORequestThread
* >::iterator ii
= ::std::find( m_lst
.begin(), m_lst
.end(), p
);
78 OSL_ASSERT( ii
!= m_lst
.end() );
82 void ThreadAdmin::join()
84 ORequestThread
*pCurrent
;
89 MutexGuard
aGuard( m_mutex
);
92 pCurrent
= m_lst
.front();
93 pCurrent
->setDeleteSelf( sal_False
);
104 ThreadAdmin
* ThreadAdmin::getInstance()
106 static ThreadAdmin
*pThreadAdmin
= 0;
109 MutexGuard
guard( Mutex::getGlobalMutex() );
112 static ThreadAdmin admin
;
113 pThreadAdmin
= &admin
;
120 // ----------------------------------------------------------------------------------
121 ORequestThread::ORequestThread( JobQueue
*pQueue
,
122 const ByteSequence
&aThreadId
,
123 sal_Bool bAsynchron
)
126 , m_aThreadId( aThreadId
)
127 , m_bAsynchron( bAsynchron
)
128 , m_bDeleteSelf( sal_True
)
130 ThreadAdmin::getInstance()->add( this );
134 ORequestThread::~ORequestThread()
138 osl_destroyThread(m_thread
);
143 void ORequestThread::setTask( JobQueue
*pQueue
,
144 const ByteSequence
&aThreadId
,
145 sal_Bool bAsynchron
)
148 m_aThreadId
= aThreadId
;
149 m_bAsynchron
= bAsynchron
;
152 sal_Bool
ORequestThread::create()
154 OSL_ASSERT(m_thread
== 0); // only one running thread per instance
156 m_thread
= osl_createSuspendedThread( cppu_requestThreadWorker
, (void*)this);
159 osl_resumeThread( m_thread
);
162 return m_thread
!= 0;
165 void ORequestThread::join()
167 osl_joinWithThread( m_thread
);
170 void ORequestThread::onTerminated()
172 ThreadAdmin::getInstance()->remove( this );
179 void ORequestThread::run()
185 if ( !uno_bindIdToCurrentThread( m_aThreadId
.getHandle() ) )
191 while( ! m_pQueue
->isEmpty() )
193 // Note : Oneways should not get a disposable disposeid,
194 // It does not make sense to dispose a call in this state.
195 // That's way we put it an disposeid, that can't be used otherwise.
197 sal::static_int_cast
< sal_Int64
>(
198 reinterpret_cast< sal_IntPtr
>(this)),
201 if( m_pQueue
->isEmpty() )
203 ThreadPool::getInstance()->revokeQueue( m_aThreadId
, m_bAsynchron
);
204 // Note : revokeQueue might have failed because m_pQueue.isEmpty()
205 // may be false (race).
214 uno_releaseIdFromCurrentThread();
217 cppu_threadpool::ThreadPool::getInstance()->waitInPool( this );