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