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 <hash_set>
31*cdf0e10cSrcweir #include <stdio.h>
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include <osl/diagnose.h>
34*cdf0e10cSrcweir #include <osl/mutex.hxx>
35*cdf0e10cSrcweir #include <osl/thread.h>
36*cdf0e10cSrcweir #include <rtl/instance.hxx>
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir #include <uno/threadpool.h>
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir #include "threadpool.hxx"
41*cdf0e10cSrcweir #include "thread.hxx"
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir using namespace ::std;
44*cdf0e10cSrcweir using namespace ::osl;
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir namespace cppu_threadpool
47*cdf0e10cSrcweir {
48*cdf0e10cSrcweir 	struct theDisposedCallerAdmin :
49*cdf0e10cSrcweir 		public rtl::StaticWithInit< DisposedCallerAdminHolder, theDisposedCallerAdmin >
50*cdf0e10cSrcweir 	{
51*cdf0e10cSrcweir 		DisposedCallerAdminHolder operator () () {
52*cdf0e10cSrcweir 			return DisposedCallerAdminHolder(new DisposedCallerAdmin());
53*cdf0e10cSrcweir 		}
54*cdf0e10cSrcweir 	};
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir 	DisposedCallerAdminHolder DisposedCallerAdmin::getInstance()
57*cdf0e10cSrcweir 	{
58*cdf0e10cSrcweir 		return theDisposedCallerAdmin::get();
59*cdf0e10cSrcweir 	}
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir 	DisposedCallerAdmin::~DisposedCallerAdmin()
62*cdf0e10cSrcweir 	{
63*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
64*cdf0e10cSrcweir 		if( !m_lst.empty() )
65*cdf0e10cSrcweir 		{
66*cdf0e10cSrcweir 			printf( "DisposedCallerList : %lu left\n" , static_cast<unsigned long>(m_lst.size( )));
67*cdf0e10cSrcweir 		}
68*cdf0e10cSrcweir #endif
69*cdf0e10cSrcweir 	}
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir 	void DisposedCallerAdmin::dispose( sal_Int64 nDisposeId )
72*cdf0e10cSrcweir 	{
73*cdf0e10cSrcweir 		MutexGuard guard( m_mutex );
74*cdf0e10cSrcweir 		m_lst.push_back( nDisposeId );
75*cdf0e10cSrcweir 	}
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir 	void DisposedCallerAdmin::stopDisposing( sal_Int64 nDisposeId )
78*cdf0e10cSrcweir 	{
79*cdf0e10cSrcweir 		MutexGuard guard( m_mutex );
80*cdf0e10cSrcweir 		for( DisposedCallerList::iterator ii = m_lst.begin() ;
81*cdf0e10cSrcweir 			 ii != m_lst.end() ;
82*cdf0e10cSrcweir 			 ++ ii )
83*cdf0e10cSrcweir 		{
84*cdf0e10cSrcweir 			if( (*ii) == nDisposeId )
85*cdf0e10cSrcweir 			{
86*cdf0e10cSrcweir 				m_lst.erase( ii );
87*cdf0e10cSrcweir 				break;
88*cdf0e10cSrcweir 			}
89*cdf0e10cSrcweir 		}
90*cdf0e10cSrcweir 	}
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir 	sal_Bool DisposedCallerAdmin::isDisposed( sal_Int64 nDisposeId )
93*cdf0e10cSrcweir 	{
94*cdf0e10cSrcweir 		MutexGuard guard( m_mutex );
95*cdf0e10cSrcweir 		for( DisposedCallerList::iterator ii = m_lst.begin() ;
96*cdf0e10cSrcweir 			 ii != m_lst.end() ;
97*cdf0e10cSrcweir 			 ++ ii )
98*cdf0e10cSrcweir 		{
99*cdf0e10cSrcweir 			if( (*ii) == nDisposeId )
100*cdf0e10cSrcweir 			{
101*cdf0e10cSrcweir 				return sal_True;
102*cdf0e10cSrcweir 			}
103*cdf0e10cSrcweir 		}
104*cdf0e10cSrcweir 		return sal_False;
105*cdf0e10cSrcweir 	}
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir 	//-------------------------------------------------------------------------------
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir 	struct theThreadPool :
111*cdf0e10cSrcweir 		public rtl::StaticWithInit< ThreadPoolHolder, theThreadPool >
112*cdf0e10cSrcweir 	{
113*cdf0e10cSrcweir 		ThreadPoolHolder operator () () {
114*cdf0e10cSrcweir 			ThreadPoolHolder aRet(new ThreadPool());
115*cdf0e10cSrcweir 			return aRet;
116*cdf0e10cSrcweir 		}
117*cdf0e10cSrcweir 	};
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir 	ThreadPool::ThreadPool()
120*cdf0e10cSrcweir 	{
121*cdf0e10cSrcweir         	m_DisposedCallerAdmin = DisposedCallerAdmin::getInstance();
122*cdf0e10cSrcweir 	}
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir 	ThreadPool::~ThreadPool()
125*cdf0e10cSrcweir 	{
126*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
127*cdf0e10cSrcweir 		if( m_mapQueue.size() )
128*cdf0e10cSrcweir 		{
129*cdf0e10cSrcweir 			printf( "ThreadIdHashMap : %lu left\n" , static_cast<unsigned long>(m_mapQueue.size()) );
130*cdf0e10cSrcweir 		}
131*cdf0e10cSrcweir #endif
132*cdf0e10cSrcweir 	}
133*cdf0e10cSrcweir 	ThreadPoolHolder ThreadPool::getInstance()
134*cdf0e10cSrcweir 	{
135*cdf0e10cSrcweir 		return theThreadPool::get();
136*cdf0e10cSrcweir 	}
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir 	void ThreadPool::dispose( sal_Int64 nDisposeId )
140*cdf0e10cSrcweir 	{
141*cdf0e10cSrcweir 		if( nDisposeId )
142*cdf0e10cSrcweir 		{
143*cdf0e10cSrcweir 			m_DisposedCallerAdmin->dispose( nDisposeId );
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir 			MutexGuard guard( m_mutex );
146*cdf0e10cSrcweir 			for( ThreadIdHashMap::iterator ii = m_mapQueue.begin() ;
147*cdf0e10cSrcweir 				 ii != m_mapQueue.end();
148*cdf0e10cSrcweir 				 ++ii)
149*cdf0e10cSrcweir 			{
150*cdf0e10cSrcweir 				if( (*ii).second.first )
151*cdf0e10cSrcweir 				{
152*cdf0e10cSrcweir 					(*ii).second.first->dispose( nDisposeId );
153*cdf0e10cSrcweir 				}
154*cdf0e10cSrcweir 				if( (*ii).second.second )
155*cdf0e10cSrcweir 				{
156*cdf0e10cSrcweir 					(*ii).second.second->dispose( nDisposeId );
157*cdf0e10cSrcweir 				}
158*cdf0e10cSrcweir 			}
159*cdf0e10cSrcweir 		}
160*cdf0e10cSrcweir 		else
161*cdf0e10cSrcweir 		{
162*cdf0e10cSrcweir 			{
163*cdf0e10cSrcweir 				MutexGuard guard( m_mutexWaitingThreadList );
164*cdf0e10cSrcweir 				for( WaitingThreadList::iterator ii = m_lstThreads.begin() ;
165*cdf0e10cSrcweir 					 ii != m_lstThreads.end() ;
166*cdf0e10cSrcweir 					 ++ ii )
167*cdf0e10cSrcweir 				{
168*cdf0e10cSrcweir 					// wake the threads up
169*cdf0e10cSrcweir 					osl_setCondition( (*ii)->condition );
170*cdf0e10cSrcweir 				}
171*cdf0e10cSrcweir 			}
172*cdf0e10cSrcweir 			ThreadAdmin::getInstance()->join();
173*cdf0e10cSrcweir 		}
174*cdf0e10cSrcweir 	}
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir 	void ThreadPool::stopDisposing( sal_Int64 nDisposeId )
177*cdf0e10cSrcweir 	{
178*cdf0e10cSrcweir 		m_DisposedCallerAdmin->stopDisposing( nDisposeId );
179*cdf0e10cSrcweir 	}
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir 	/******************
182*cdf0e10cSrcweir 	 * This methods lets the thread wait a certain amount of time. If within this timespan
183*cdf0e10cSrcweir 	 * a new request comes in, this thread is reused. This is done only to improve performance,
184*cdf0e10cSrcweir 	 * it is not required for threadpool functionality.
185*cdf0e10cSrcweir 	 ******************/
186*cdf0e10cSrcweir 	void ThreadPool::waitInPool( ORequestThread * pThread )
187*cdf0e10cSrcweir 	{
188*cdf0e10cSrcweir 		struct WaitingThread waitingThread;
189*cdf0e10cSrcweir 		waitingThread.condition = osl_createCondition();
190*cdf0e10cSrcweir 		waitingThread.thread = pThread;
191*cdf0e10cSrcweir 		{
192*cdf0e10cSrcweir 			MutexGuard guard( m_mutexWaitingThreadList );
193*cdf0e10cSrcweir 			m_lstThreads.push_front( &waitingThread );
194*cdf0e10cSrcweir 		}
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir 		// let the thread wait 2 seconds
197*cdf0e10cSrcweir 		TimeValue time = { 2 , 0 };
198*cdf0e10cSrcweir 		osl_waitCondition( waitingThread.condition , &time );
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir 		{
201*cdf0e10cSrcweir 			MutexGuard guard ( m_mutexWaitingThreadList );
202*cdf0e10cSrcweir 			if( waitingThread.thread )
203*cdf0e10cSrcweir 			{
204*cdf0e10cSrcweir 				// thread wasn't reused, remove it from the list
205*cdf0e10cSrcweir 				WaitingThreadList::iterator ii = find(
206*cdf0e10cSrcweir 					m_lstThreads.begin(), m_lstThreads.end(), &waitingThread );
207*cdf0e10cSrcweir 				OSL_ASSERT( ii != m_lstThreads.end() );
208*cdf0e10cSrcweir 				m_lstThreads.erase( ii );
209*cdf0e10cSrcweir 			}
210*cdf0e10cSrcweir 		}
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir 		osl_destroyCondition( waitingThread.condition );
213*cdf0e10cSrcweir 	}
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir 	void ThreadPool::createThread( JobQueue *pQueue ,
216*cdf0e10cSrcweir 								   const ByteSequence &aThreadId,
217*cdf0e10cSrcweir 								   sal_Bool bAsynchron )
218*cdf0e10cSrcweir 	{
219*cdf0e10cSrcweir 		sal_Bool bCreate = sal_True;
220*cdf0e10cSrcweir 		{
221*cdf0e10cSrcweir 			// Can a thread be reused ?
222*cdf0e10cSrcweir 			MutexGuard guard( m_mutexWaitingThreadList );
223*cdf0e10cSrcweir 			if( ! m_lstThreads.empty() )
224*cdf0e10cSrcweir 			{
225*cdf0e10cSrcweir 				// inform the thread and let it go
226*cdf0e10cSrcweir 				struct WaitingThread *pWaitingThread = m_lstThreads.back();
227*cdf0e10cSrcweir 				pWaitingThread->thread->setTask( pQueue , aThreadId , bAsynchron );
228*cdf0e10cSrcweir 				pWaitingThread->thread = 0;
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir 				// remove from list
231*cdf0e10cSrcweir 				m_lstThreads.pop_back();
232*cdf0e10cSrcweir 
233*cdf0e10cSrcweir 				// let the thread go
234*cdf0e10cSrcweir 				osl_setCondition( pWaitingThread->condition );
235*cdf0e10cSrcweir 				bCreate = sal_False;
236*cdf0e10cSrcweir 			}
237*cdf0e10cSrcweir 		}
238*cdf0e10cSrcweir 
239*cdf0e10cSrcweir 		if( bCreate )
240*cdf0e10cSrcweir 		{
241*cdf0e10cSrcweir 			ORequestThread *pThread =
242*cdf0e10cSrcweir 				new ORequestThread( pQueue , aThreadId, bAsynchron);
243*cdf0e10cSrcweir 			// deletes itself !
244*cdf0e10cSrcweir 			pThread->create();
245*cdf0e10cSrcweir 		}
246*cdf0e10cSrcweir 	}
247*cdf0e10cSrcweir 
248*cdf0e10cSrcweir 	sal_Bool ThreadPool::revokeQueue( const ByteSequence &aThreadId, sal_Bool bAsynchron )
249*cdf0e10cSrcweir 	{
250*cdf0e10cSrcweir 		MutexGuard guard( m_mutex );
251*cdf0e10cSrcweir 
252*cdf0e10cSrcweir 		ThreadIdHashMap::iterator ii = m_mapQueue.find( aThreadId );
253*cdf0e10cSrcweir 		OSL_ASSERT( ii != m_mapQueue.end() );
254*cdf0e10cSrcweir 
255*cdf0e10cSrcweir 		if( bAsynchron )
256*cdf0e10cSrcweir 		{
257*cdf0e10cSrcweir 			if( ! (*ii).second.second->isEmpty() )
258*cdf0e10cSrcweir 			{
259*cdf0e10cSrcweir 				// another thread has put something into the queue
260*cdf0e10cSrcweir 				return sal_False;
261*cdf0e10cSrcweir 			}
262*cdf0e10cSrcweir 
263*cdf0e10cSrcweir 			(*ii).second.second = 0;
264*cdf0e10cSrcweir 			if( (*ii).second.first )
265*cdf0e10cSrcweir 			{
266*cdf0e10cSrcweir 				// all oneway request have been processed, now
267*cdf0e10cSrcweir 				// synchronus requests may go on
268*cdf0e10cSrcweir 				(*ii).second.first->resume();
269*cdf0e10cSrcweir 			}
270*cdf0e10cSrcweir 		}
271*cdf0e10cSrcweir 		else
272*cdf0e10cSrcweir 		{
273*cdf0e10cSrcweir 			if( ! (*ii).second.first->isEmpty() )
274*cdf0e10cSrcweir 			{
275*cdf0e10cSrcweir 				// another thread has put something into the queue
276*cdf0e10cSrcweir 				return sal_False;
277*cdf0e10cSrcweir 			}
278*cdf0e10cSrcweir 			(*ii).second.first = 0;
279*cdf0e10cSrcweir 		}
280*cdf0e10cSrcweir 
281*cdf0e10cSrcweir 		if( 0 == (*ii).second.first && 0 == (*ii).second.second )
282*cdf0e10cSrcweir 		{
283*cdf0e10cSrcweir 			m_mapQueue.erase( ii );
284*cdf0e10cSrcweir 		}
285*cdf0e10cSrcweir 
286*cdf0e10cSrcweir 		return sal_True;
287*cdf0e10cSrcweir 	}
288*cdf0e10cSrcweir 
289*cdf0e10cSrcweir 
290*cdf0e10cSrcweir 	void ThreadPool::addJob(
291*cdf0e10cSrcweir 		const ByteSequence &aThreadId ,
292*cdf0e10cSrcweir 		sal_Bool bAsynchron,
293*cdf0e10cSrcweir 		void *pThreadSpecificData,
294*cdf0e10cSrcweir 		RequestFun * doRequest )
295*cdf0e10cSrcweir 	{
296*cdf0e10cSrcweir 		sal_Bool bCreateThread = sal_False;
297*cdf0e10cSrcweir 		JobQueue *pQueue = 0;
298*cdf0e10cSrcweir 		{
299*cdf0e10cSrcweir 			MutexGuard guard( m_mutex );
300*cdf0e10cSrcweir 
301*cdf0e10cSrcweir 			ThreadIdHashMap::iterator ii = m_mapQueue.find( aThreadId );
302*cdf0e10cSrcweir 
303*cdf0e10cSrcweir 			if( ii == m_mapQueue.end() )
304*cdf0e10cSrcweir 			{
305*cdf0e10cSrcweir 				m_mapQueue[ aThreadId ] = pair < JobQueue * , JobQueue * > ( 0 , 0 );
306*cdf0e10cSrcweir 				ii = m_mapQueue.find( aThreadId );
307*cdf0e10cSrcweir 				OSL_ASSERT( ii != m_mapQueue.end() );
308*cdf0e10cSrcweir 			}
309*cdf0e10cSrcweir 
310*cdf0e10cSrcweir 			if( bAsynchron )
311*cdf0e10cSrcweir 			{
312*cdf0e10cSrcweir 				if( ! (*ii).second.second )
313*cdf0e10cSrcweir 				{
314*cdf0e10cSrcweir 					(*ii).second.second = new JobQueue();
315*cdf0e10cSrcweir 					bCreateThread = sal_True;
316*cdf0e10cSrcweir 				}
317*cdf0e10cSrcweir 				pQueue = (*ii).second.second;
318*cdf0e10cSrcweir 			}
319*cdf0e10cSrcweir 			else
320*cdf0e10cSrcweir 			{
321*cdf0e10cSrcweir 				if( ! (*ii).second.first )
322*cdf0e10cSrcweir 				{
323*cdf0e10cSrcweir 					(*ii).second.first = new JobQueue();
324*cdf0e10cSrcweir 					bCreateThread = sal_True;
325*cdf0e10cSrcweir 				}
326*cdf0e10cSrcweir 				pQueue = (*ii).second.first;
327*cdf0e10cSrcweir 
328*cdf0e10cSrcweir 				if( (*ii).second.second && ( (*ii).second.second->isBusy() ) )
329*cdf0e10cSrcweir 				{
330*cdf0e10cSrcweir 					pQueue->suspend();
331*cdf0e10cSrcweir 				}
332*cdf0e10cSrcweir 			}
333*cdf0e10cSrcweir 			pQueue->add( pThreadSpecificData , doRequest );
334*cdf0e10cSrcweir 		}
335*cdf0e10cSrcweir 
336*cdf0e10cSrcweir 		if( bCreateThread )
337*cdf0e10cSrcweir 		{
338*cdf0e10cSrcweir 			createThread( pQueue , aThreadId , bAsynchron);
339*cdf0e10cSrcweir 		}
340*cdf0e10cSrcweir 	}
341*cdf0e10cSrcweir 
342*cdf0e10cSrcweir 	void ThreadPool::prepare( const ByteSequence &aThreadId )
343*cdf0e10cSrcweir 	{
344*cdf0e10cSrcweir 		MutexGuard guard( m_mutex );
345*cdf0e10cSrcweir 
346*cdf0e10cSrcweir 		ThreadIdHashMap::iterator ii = m_mapQueue.find( aThreadId );
347*cdf0e10cSrcweir 
348*cdf0e10cSrcweir 		if( ii == m_mapQueue.end() )
349*cdf0e10cSrcweir 		{
350*cdf0e10cSrcweir 			JobQueue *p = new JobQueue();
351*cdf0e10cSrcweir 			m_mapQueue[ aThreadId ] = pair< JobQueue * , JobQueue * > ( p , 0 );
352*cdf0e10cSrcweir 		}
353*cdf0e10cSrcweir 		else if( 0 == (*ii).second.first )
354*cdf0e10cSrcweir 		{
355*cdf0e10cSrcweir 			(*ii).second.first = new JobQueue();
356*cdf0e10cSrcweir 		}
357*cdf0e10cSrcweir 	}
358*cdf0e10cSrcweir 
359*cdf0e10cSrcweir 	void * ThreadPool::enter( const ByteSequence & aThreadId , sal_Int64 nDisposeId )
360*cdf0e10cSrcweir 	{
361*cdf0e10cSrcweir 		JobQueue *pQueue = 0;
362*cdf0e10cSrcweir 		{
363*cdf0e10cSrcweir 			MutexGuard guard( m_mutex );
364*cdf0e10cSrcweir 
365*cdf0e10cSrcweir 			ThreadIdHashMap::iterator ii = m_mapQueue.find( aThreadId );
366*cdf0e10cSrcweir 
367*cdf0e10cSrcweir 			OSL_ASSERT( ii != m_mapQueue.end() );
368*cdf0e10cSrcweir 			pQueue = (*ii).second.first;
369*cdf0e10cSrcweir 		}
370*cdf0e10cSrcweir 
371*cdf0e10cSrcweir 		OSL_ASSERT( pQueue );
372*cdf0e10cSrcweir 		void *pReturn = pQueue->enter( nDisposeId );
373*cdf0e10cSrcweir 
374*cdf0e10cSrcweir 		if( pQueue->isCallstackEmpty() )
375*cdf0e10cSrcweir 		{
376*cdf0e10cSrcweir 			if( revokeQueue( aThreadId , sal_False) )
377*cdf0e10cSrcweir 			{
378*cdf0e10cSrcweir 				// remove queue
379*cdf0e10cSrcweir 				delete pQueue;
380*cdf0e10cSrcweir 			}
381*cdf0e10cSrcweir 		}
382*cdf0e10cSrcweir 		return pReturn;
383*cdf0e10cSrcweir 	}
384*cdf0e10cSrcweir }
385*cdf0e10cSrcweir 
386*cdf0e10cSrcweir 
387*cdf0e10cSrcweir using namespace cppu_threadpool;
388*cdf0e10cSrcweir 
389*cdf0e10cSrcweir struct uno_ThreadPool_Equal
390*cdf0e10cSrcweir {
391*cdf0e10cSrcweir 	sal_Bool operator () ( const uno_ThreadPool &a , const uno_ThreadPool &b ) const
392*cdf0e10cSrcweir 		{
393*cdf0e10cSrcweir 			return a == b;
394*cdf0e10cSrcweir 		}
395*cdf0e10cSrcweir };
396*cdf0e10cSrcweir 
397*cdf0e10cSrcweir struct uno_ThreadPool_Hash
398*cdf0e10cSrcweir {
399*cdf0e10cSrcweir 	sal_Size operator () ( const uno_ThreadPool &a  )  const
400*cdf0e10cSrcweir 		{
401*cdf0e10cSrcweir 			return (sal_Size) a;
402*cdf0e10cSrcweir 		}
403*cdf0e10cSrcweir };
404*cdf0e10cSrcweir 
405*cdf0e10cSrcweir 
406*cdf0e10cSrcweir 
407*cdf0e10cSrcweir typedef ::std::hash_map< uno_ThreadPool, ThreadPoolHolder, uno_ThreadPool_Hash, uno_ThreadPool_Equal > ThreadpoolHashSet;
408*cdf0e10cSrcweir 
409*cdf0e10cSrcweir static ThreadpoolHashSet *g_pThreadpoolHashSet;
410*cdf0e10cSrcweir 
411*cdf0e10cSrcweir struct _uno_ThreadPool
412*cdf0e10cSrcweir {
413*cdf0e10cSrcweir 	sal_Int32 dummy;
414*cdf0e10cSrcweir };
415*cdf0e10cSrcweir 
416*cdf0e10cSrcweir extern "C" uno_ThreadPool SAL_CALL
417*cdf0e10cSrcweir uno_threadpool_create() SAL_THROW_EXTERN_C()
418*cdf0e10cSrcweir {
419*cdf0e10cSrcweir 	MutexGuard guard( Mutex::getGlobalMutex() );
420*cdf0e10cSrcweir 	if( ! g_pThreadpoolHashSet )
421*cdf0e10cSrcweir 	{
422*cdf0e10cSrcweir 		g_pThreadpoolHashSet = new ThreadpoolHashSet();
423*cdf0e10cSrcweir 	}
424*cdf0e10cSrcweir 
425*cdf0e10cSrcweir 	// Just ensure that the handle is unique in the process (via heap)
426*cdf0e10cSrcweir 	uno_ThreadPool h = new struct _uno_ThreadPool;
427*cdf0e10cSrcweir 	g_pThreadpoolHashSet->insert( ThreadpoolHashSet::value_type(h, ThreadPool::getInstance()) );
428*cdf0e10cSrcweir 	return h;
429*cdf0e10cSrcweir }
430*cdf0e10cSrcweir 
431*cdf0e10cSrcweir extern "C" void SAL_CALL
432*cdf0e10cSrcweir uno_threadpool_attach( uno_ThreadPool ) SAL_THROW_EXTERN_C()
433*cdf0e10cSrcweir {
434*cdf0e10cSrcweir 	sal_Sequence *pThreadId = 0;
435*cdf0e10cSrcweir 	uno_getIdOfCurrentThread( &pThreadId );
436*cdf0e10cSrcweir 	ThreadPool::getInstance()->prepare( pThreadId );
437*cdf0e10cSrcweir 	rtl_byte_sequence_release( pThreadId );
438*cdf0e10cSrcweir 	uno_releaseIdFromCurrentThread();
439*cdf0e10cSrcweir }
440*cdf0e10cSrcweir 
441*cdf0e10cSrcweir extern "C" void SAL_CALL
442*cdf0e10cSrcweir uno_threadpool_enter( uno_ThreadPool hPool , void **ppJob )
443*cdf0e10cSrcweir 	SAL_THROW_EXTERN_C()
444*cdf0e10cSrcweir {
445*cdf0e10cSrcweir 	sal_Sequence *pThreadId = 0;
446*cdf0e10cSrcweir 	uno_getIdOfCurrentThread( &pThreadId );
447*cdf0e10cSrcweir 	*ppJob =
448*cdf0e10cSrcweir 		ThreadPool::getInstance()->enter(
449*cdf0e10cSrcweir             pThreadId,
450*cdf0e10cSrcweir             sal::static_int_cast< sal_Int64 >(
451*cdf0e10cSrcweir                 reinterpret_cast< sal_IntPtr >(hPool)) );
452*cdf0e10cSrcweir 	rtl_byte_sequence_release( pThreadId );
453*cdf0e10cSrcweir 	uno_releaseIdFromCurrentThread();
454*cdf0e10cSrcweir }
455*cdf0e10cSrcweir 
456*cdf0e10cSrcweir extern "C" void SAL_CALL
457*cdf0e10cSrcweir uno_threadpool_detach( uno_ThreadPool ) SAL_THROW_EXTERN_C()
458*cdf0e10cSrcweir {
459*cdf0e10cSrcweir 	// we might do here some tiding up in case a thread called attach but never detach
460*cdf0e10cSrcweir }
461*cdf0e10cSrcweir 
462*cdf0e10cSrcweir extern "C" void SAL_CALL
463*cdf0e10cSrcweir uno_threadpool_putJob(
464*cdf0e10cSrcweir 	uno_ThreadPool,
465*cdf0e10cSrcweir 	sal_Sequence *pThreadId,
466*cdf0e10cSrcweir 	void *pJob,
467*cdf0e10cSrcweir 	void ( SAL_CALL * doRequest ) ( void *pThreadSpecificData ),
468*cdf0e10cSrcweir 	sal_Bool bIsOneway ) SAL_THROW_EXTERN_C()
469*cdf0e10cSrcweir {
470*cdf0e10cSrcweir 	ThreadPool::getInstance()->addJob( pThreadId, bIsOneway, pJob ,doRequest );
471*cdf0e10cSrcweir }
472*cdf0e10cSrcweir 
473*cdf0e10cSrcweir extern "C" void SAL_CALL
474*cdf0e10cSrcweir uno_threadpool_dispose( uno_ThreadPool hPool ) SAL_THROW_EXTERN_C()
475*cdf0e10cSrcweir {
476*cdf0e10cSrcweir 	ThreadPool::getInstance()->dispose(
477*cdf0e10cSrcweir         sal::static_int_cast< sal_Int64 >(
478*cdf0e10cSrcweir             reinterpret_cast< sal_IntPtr >(hPool)) );
479*cdf0e10cSrcweir }
480*cdf0e10cSrcweir 
481*cdf0e10cSrcweir extern "C" void SAL_CALL
482*cdf0e10cSrcweir uno_threadpool_destroy( uno_ThreadPool hPool ) SAL_THROW_EXTERN_C()
483*cdf0e10cSrcweir {
484*cdf0e10cSrcweir 	ThreadPool::getInstance()->stopDisposing(
485*cdf0e10cSrcweir         sal::static_int_cast< sal_Int64 >(
486*cdf0e10cSrcweir             reinterpret_cast< sal_IntPtr >(hPool)) );
487*cdf0e10cSrcweir 
488*cdf0e10cSrcweir 	if( hPool )
489*cdf0e10cSrcweir 	{
490*cdf0e10cSrcweir 		// special treatment for 0 !
491*cdf0e10cSrcweir 		OSL_ASSERT( g_pThreadpoolHashSet );
492*cdf0e10cSrcweir 
493*cdf0e10cSrcweir 		MutexGuard guard( Mutex::getGlobalMutex() );
494*cdf0e10cSrcweir 
495*cdf0e10cSrcweir 		ThreadpoolHashSet::iterator ii = g_pThreadpoolHashSet->find( hPool );
496*cdf0e10cSrcweir 		OSL_ASSERT( ii != g_pThreadpoolHashSet->end() );
497*cdf0e10cSrcweir 		g_pThreadpoolHashSet->erase( ii );
498*cdf0e10cSrcweir 		delete hPool;
499*cdf0e10cSrcweir 
500*cdf0e10cSrcweir 		if( g_pThreadpoolHashSet->empty() )
501*cdf0e10cSrcweir 		{
502*cdf0e10cSrcweir 			delete g_pThreadpoolHashSet;
503*cdf0e10cSrcweir 			g_pThreadpoolHashSet = 0;
504*cdf0e10cSrcweir 		}
505*cdf0e10cSrcweir 	}
506*cdf0e10cSrcweir }
507