Update ooo320-m1
[ooovba.git] / vcl / source / helper / threadex.cxx
blob636fe5bc891a12d684e313a6f10b11e6f06f9c9d
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: threadex.cxx,v $
10 * $Revision: 1.10 $
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_vcl.hxx"
34 #define THREADEX_IMPLEMENTATION
35 #include <vcl/threadex.hxx>
36 #include <vcl/svapp.hxx>
38 using namespace vcl;
40 ThreadExecutor::ThreadExecutor()
42 m_aFinish = osl_createCondition();
43 m_aThread = NULL;
46 ThreadExecutor::~ThreadExecutor()
48 osl_destroyCondition( m_aFinish );
49 if( m_aThread )
50 osl_destroyThread( m_aThread );
53 extern "C"
55 static void call_worker( void* pInstance )
57 ThreadExecutor::worker( pInstance );
61 void ThreadExecutor::worker( void* pInstance )
63 ThreadExecutor* pThis = ((ThreadExecutor*)pInstance);
64 pThis->m_nReturn = pThis->doIt();
65 osl_setCondition( pThis->m_aFinish );
68 long ThreadExecutor::execute()
70 osl_resetCondition( m_aFinish );
71 if( m_aThread )
72 osl_destroyThread( m_aThread ), m_aThread = NULL;
73 m_aThread = osl_createThread( call_worker, this );
74 while( ! osl_checkCondition( m_aFinish ) )
75 Application::Reschedule();
76 return m_nReturn;
80 SolarThreadExecutor::SolarThreadExecutor()
81 :m_nReturn( 0 )
82 ,m_bTimeout( false )
84 m_aStart = osl_createCondition();
85 m_aFinish = osl_createCondition();
88 SolarThreadExecutor::~SolarThreadExecutor()
90 osl_destroyCondition( m_aStart );
91 osl_destroyCondition( m_aFinish );
94 IMPL_LINK( SolarThreadExecutor, worker, void*, EMPTYARG )
96 if ( !m_bTimeout )
98 osl_setCondition( m_aStart );
99 m_nReturn = doIt();
100 osl_setCondition( m_aFinish );
102 return m_nReturn;
105 long SolarThreadExecutor::impl_execute( const TimeValue* _pTimeout )
107 if( ::vos::OThread::getCurrentIdentifier() == Application::GetMainThreadIdentifier() )
109 osl_setCondition( m_aStart );
110 m_nReturn = doIt();
111 osl_setCondition( m_aFinish );
113 else
115 osl_resetCondition( m_aStart );
116 osl_resetCondition( m_aFinish );
117 ULONG nSolarMutexCount = Application::ReleaseSolarMutex();
118 ULONG nEvent = Application::PostUserEvent( LINK( this, SolarThreadExecutor, worker ) );
119 if ( osl_cond_result_timeout == osl_waitCondition( m_aStart, _pTimeout ) )
121 m_bTimeout = true;
122 Application::RemoveUserEvent( nEvent );
124 else
125 osl_waitCondition( m_aFinish, NULL );
126 if( nSolarMutexCount )
127 Application::AcquireSolarMutex( nSolarMutexCount );
129 return m_nReturn;