xref: /aoo42x/main/vcl/source/helper/threadex.cxx (revision 9f62ea84)
1*9f62ea84SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*9f62ea84SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*9f62ea84SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*9f62ea84SAndrew Rist  * distributed with this work for additional information
6*9f62ea84SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*9f62ea84SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*9f62ea84SAndrew Rist  * "License"); you may not use this file except in compliance
9*9f62ea84SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*9f62ea84SAndrew Rist  *
11*9f62ea84SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*9f62ea84SAndrew Rist  *
13*9f62ea84SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*9f62ea84SAndrew Rist  * software distributed under the License is distributed on an
15*9f62ea84SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*9f62ea84SAndrew Rist  * KIND, either express or implied.  See the License for the
17*9f62ea84SAndrew Rist  * specific language governing permissions and limitations
18*9f62ea84SAndrew Rist  * under the License.
19*9f62ea84SAndrew Rist  *
20*9f62ea84SAndrew Rist  *************************************************************/
21*9f62ea84SAndrew Rist 
22*9f62ea84SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_vcl.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #define THREADEX_IMPLEMENTATION
28cdf0e10cSrcweir #include <vcl/threadex.hxx>
29cdf0e10cSrcweir #include <vcl/svapp.hxx>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir using namespace vcl;
32cdf0e10cSrcweir 
ThreadExecutor()33cdf0e10cSrcweir ThreadExecutor::ThreadExecutor()
34cdf0e10cSrcweir {
35cdf0e10cSrcweir 	m_aFinish = osl_createCondition();
36cdf0e10cSrcweir 	m_aThread = NULL;
37cdf0e10cSrcweir }
38cdf0e10cSrcweir 
~ThreadExecutor()39cdf0e10cSrcweir ThreadExecutor::~ThreadExecutor()
40cdf0e10cSrcweir {
41cdf0e10cSrcweir 	osl_destroyCondition( m_aFinish );
42cdf0e10cSrcweir 	if( m_aThread )
43cdf0e10cSrcweir 		osl_destroyThread( m_aThread );
44cdf0e10cSrcweir }
45cdf0e10cSrcweir 
46cdf0e10cSrcweir extern "C"
47cdf0e10cSrcweir {
call_worker(void * pInstance)48cdf0e10cSrcweir     static void call_worker( void* pInstance )
49cdf0e10cSrcweir     {
50cdf0e10cSrcweir         ThreadExecutor::worker( pInstance );
51cdf0e10cSrcweir     }
52cdf0e10cSrcweir }
53cdf0e10cSrcweir 
worker(void * pInstance)54cdf0e10cSrcweir void ThreadExecutor::worker( void* pInstance )
55cdf0e10cSrcweir {
56cdf0e10cSrcweir 	ThreadExecutor* pThis = ((ThreadExecutor*)pInstance);
57cdf0e10cSrcweir 	pThis->m_nReturn = pThis->doIt();
58cdf0e10cSrcweir 	osl_setCondition( pThis->m_aFinish );
59cdf0e10cSrcweir }
60cdf0e10cSrcweir 
execute()61cdf0e10cSrcweir long ThreadExecutor::execute()
62cdf0e10cSrcweir {
63cdf0e10cSrcweir 	osl_resetCondition( m_aFinish );
64cdf0e10cSrcweir 	if( m_aThread )
65cdf0e10cSrcweir 		osl_destroyThread( m_aThread ), m_aThread = NULL;
66cdf0e10cSrcweir 	m_aThread = osl_createThread( call_worker, this );
67cdf0e10cSrcweir 	while( ! osl_checkCondition( m_aFinish ) )
68cdf0e10cSrcweir 		Application::Reschedule();
69cdf0e10cSrcweir 	return m_nReturn;
70cdf0e10cSrcweir }
71cdf0e10cSrcweir 
72cdf0e10cSrcweir 
SolarThreadExecutor()73cdf0e10cSrcweir SolarThreadExecutor::SolarThreadExecutor()
74cdf0e10cSrcweir     :m_nReturn( 0 )
75cdf0e10cSrcweir     ,m_bTimeout( false )
76cdf0e10cSrcweir {
77cdf0e10cSrcweir     m_aStart = osl_createCondition();
78cdf0e10cSrcweir 	m_aFinish = osl_createCondition();
79cdf0e10cSrcweir }
80cdf0e10cSrcweir 
~SolarThreadExecutor()81cdf0e10cSrcweir SolarThreadExecutor::~SolarThreadExecutor()
82cdf0e10cSrcweir {
83cdf0e10cSrcweir     osl_destroyCondition( m_aStart );
84cdf0e10cSrcweir 	osl_destroyCondition( m_aFinish );
85cdf0e10cSrcweir }
86cdf0e10cSrcweir 
IMPL_LINK(SolarThreadExecutor,worker,void *,EMPTYARG)87cdf0e10cSrcweir IMPL_LINK( SolarThreadExecutor, worker, void*, EMPTYARG )
88cdf0e10cSrcweir {
89cdf0e10cSrcweir     if ( !m_bTimeout )
90cdf0e10cSrcweir     {
91cdf0e10cSrcweir         osl_setCondition( m_aStart );
92cdf0e10cSrcweir 	    m_nReturn = doIt();
93cdf0e10cSrcweir 	    osl_setCondition( m_aFinish );
94cdf0e10cSrcweir     }
95cdf0e10cSrcweir 	return m_nReturn;
96cdf0e10cSrcweir }
97cdf0e10cSrcweir 
impl_execute(const TimeValue * _pTimeout)98cdf0e10cSrcweir long SolarThreadExecutor::impl_execute( const TimeValue* _pTimeout )
99cdf0e10cSrcweir {
100cdf0e10cSrcweir 	if( ::vos::OThread::getCurrentIdentifier() == Application::GetMainThreadIdentifier() )
101cdf0e10cSrcweir 	{
102cdf0e10cSrcweir         osl_setCondition( m_aStart );
103cdf0e10cSrcweir 		m_nReturn = doIt();
104cdf0e10cSrcweir 		osl_setCondition( m_aFinish );
105cdf0e10cSrcweir 	}
106cdf0e10cSrcweir 	else
107cdf0e10cSrcweir 	{
108cdf0e10cSrcweir         osl_resetCondition( m_aStart );
109cdf0e10cSrcweir 		osl_resetCondition( m_aFinish );
110cdf0e10cSrcweir 		sal_uLong nSolarMutexCount = Application::ReleaseSolarMutex();
111cdf0e10cSrcweir 		sal_uLong nEvent = Application::PostUserEvent( LINK( this, SolarThreadExecutor, worker ) );
112cdf0e10cSrcweir 		if ( osl_cond_result_timeout == osl_waitCondition( m_aStart, _pTimeout ) )
113cdf0e10cSrcweir 		{
114cdf0e10cSrcweir             m_bTimeout = true;
115cdf0e10cSrcweir 			Application::RemoveUserEvent( nEvent );
116cdf0e10cSrcweir 		}
117cdf0e10cSrcweir         else
118cdf0e10cSrcweir             osl_waitCondition( m_aFinish, NULL );
119cdf0e10cSrcweir 		if( nSolarMutexCount )
120cdf0e10cSrcweir 			Application::AcquireSolarMutex( nSolarMutexCount );
121cdf0e10cSrcweir 	}
122cdf0e10cSrcweir 	return m_nReturn;
123cdf0e10cSrcweir }
124