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: threadex.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_vcl.hxx"
34 #define THREADEX_IMPLEMENTATION
35 #include <vcl/threadex.hxx>
36 #include <vcl/svapp.hxx>
40 ThreadExecutor::ThreadExecutor()
42 m_aFinish
= osl_createCondition();
46 ThreadExecutor::~ThreadExecutor()
48 osl_destroyCondition( m_aFinish
);
50 osl_destroyThread( m_aThread
);
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
);
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();
80 SolarThreadExecutor::SolarThreadExecutor()
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
)
98 osl_setCondition( m_aStart
);
100 osl_setCondition( m_aFinish
);
105 long SolarThreadExecutor::impl_execute( const TimeValue
* _pTimeout
)
107 if( ::vos::OThread::getCurrentIdentifier() == Application::GetMainThreadIdentifier() )
109 osl_setCondition( m_aStart
);
111 osl_setCondition( m_aFinish
);
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
) )
122 Application::RemoveUserEvent( nEvent
);
125 osl_waitCondition( m_aFinish
, NULL
);
126 if( nSolarMutexCount
)
127 Application::AcquireSolarMutex( nSolarMutexCount
);