xref: /trunk/main/vcl/source/helper/threadex.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb) !
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_vcl.hxx"
30 
31 #define THREADEX_IMPLEMENTATION
32 #include <vcl/threadex.hxx>
33 #include <vcl/svapp.hxx>
34 
35 using namespace vcl;
36 
37 ThreadExecutor::ThreadExecutor()
38 {
39     m_aFinish = osl_createCondition();
40     m_aThread = NULL;
41 }
42 
43 ThreadExecutor::~ThreadExecutor()
44 {
45     osl_destroyCondition( m_aFinish );
46     if( m_aThread )
47         osl_destroyThread( m_aThread );
48 }
49 
50 extern "C"
51 {
52     static void call_worker( void* pInstance )
53     {
54         ThreadExecutor::worker( pInstance );
55     }
56 }
57 
58 void ThreadExecutor::worker( void* pInstance )
59 {
60     ThreadExecutor* pThis = ((ThreadExecutor*)pInstance);
61     pThis->m_nReturn = pThis->doIt();
62     osl_setCondition( pThis->m_aFinish );
63 }
64 
65 long ThreadExecutor::execute()
66 {
67     osl_resetCondition( m_aFinish );
68     if( m_aThread )
69         osl_destroyThread( m_aThread ), m_aThread = NULL;
70     m_aThread = osl_createThread( call_worker, this );
71     while( ! osl_checkCondition( m_aFinish ) )
72         Application::Reschedule();
73     return m_nReturn;
74 }
75 
76 
77 SolarThreadExecutor::SolarThreadExecutor()
78     :m_nReturn( 0 )
79     ,m_bTimeout( false )
80 {
81     m_aStart = osl_createCondition();
82     m_aFinish = osl_createCondition();
83 }
84 
85 SolarThreadExecutor::~SolarThreadExecutor()
86 {
87     osl_destroyCondition( m_aStart );
88     osl_destroyCondition( m_aFinish );
89 }
90 
91 IMPL_LINK( SolarThreadExecutor, worker, void*, EMPTYARG )
92 {
93     if ( !m_bTimeout )
94     {
95         osl_setCondition( m_aStart );
96         m_nReturn = doIt();
97         osl_setCondition( m_aFinish );
98     }
99     return m_nReturn;
100 }
101 
102 long SolarThreadExecutor::impl_execute( const TimeValue* _pTimeout )
103 {
104     if( ::vos::OThread::getCurrentIdentifier() == Application::GetMainThreadIdentifier() )
105     {
106         osl_setCondition( m_aStart );
107         m_nReturn = doIt();
108         osl_setCondition( m_aFinish );
109     }
110     else
111     {
112         osl_resetCondition( m_aStart );
113         osl_resetCondition( m_aFinish );
114         sal_uLong nSolarMutexCount = Application::ReleaseSolarMutex();
115         sal_uLong nEvent = Application::PostUserEvent( LINK( this, SolarThreadExecutor, worker ) );
116         if ( osl_cond_result_timeout == osl_waitCondition( m_aStart, _pTimeout ) )
117         {
118             m_bTimeout = true;
119             Application::RemoveUserEvent( nEvent );
120         }
121         else
122             osl_waitCondition( m_aFinish, NULL );
123         if( nSolarMutexCount )
124             Application::AcquireSolarMutex( nSolarMutexCount );
125     }
126     return m_nReturn;
127 }
128