1*5900e8ecSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*5900e8ecSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*5900e8ecSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*5900e8ecSAndrew Rist  * distributed with this work for additional information
6*5900e8ecSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*5900e8ecSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*5900e8ecSAndrew Rist  * "License"); you may not use this file except in compliance
9*5900e8ecSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*5900e8ecSAndrew Rist  *
11*5900e8ecSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*5900e8ecSAndrew Rist  *
13*5900e8ecSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*5900e8ecSAndrew Rist  * software distributed under the License is distributed on an
15*5900e8ecSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*5900e8ecSAndrew Rist  * KIND, either express or implied.  See the License for the
17*5900e8ecSAndrew Rist  * specific language governing permissions and limitations
18*5900e8ecSAndrew Rist  * under the License.
19*5900e8ecSAndrew Rist  *
20*5900e8ecSAndrew Rist  *************************************************************/
21*5900e8ecSAndrew Rist 
22*5900e8ecSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_svtools.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <svtools/asynclink.hxx>
28cdf0e10cSrcweir #include <vos/mutex.hxx>
29cdf0e10cSrcweir #include <tools/debug.hxx>
30cdf0e10cSrcweir #include <vcl/timer.hxx>
31cdf0e10cSrcweir #include <vcl/svapp.hxx>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir //--------------------------------------------------------------------
34cdf0e10cSrcweir namespace svtools {
35cdf0e10cSrcweir 
CreateMutex()36cdf0e10cSrcweir void AsynchronLink::CreateMutex()
37cdf0e10cSrcweir {
38cdf0e10cSrcweir     if( !_pMutex ) _pMutex = new vos::OMutex;
39cdf0e10cSrcweir }
40cdf0e10cSrcweir 
Call(void * pObj,sal_Bool bAllowDoubles,sal_Bool bUseTimer)41cdf0e10cSrcweir void AsynchronLink::Call( void* pObj, sal_Bool
42cdf0e10cSrcweir #ifdef DBG_UTIL
43cdf0e10cSrcweir bAllowDoubles
44cdf0e10cSrcweir #endif
45cdf0e10cSrcweir , sal_Bool bUseTimer )
46cdf0e10cSrcweir {
47cdf0e10cSrcweir #ifdef DBG_UTIL
48cdf0e10cSrcweir 	if ( bUseTimer || !_bInCall )
49cdf0e10cSrcweir 		DBG_WARNING( "Recursives Call. Eher ueber Timer. TLX Fragen" );
50cdf0e10cSrcweir #endif
51cdf0e10cSrcweir 	if( _aLink.IsSet() )
52cdf0e10cSrcweir 	{
53cdf0e10cSrcweir 		_pArg = pObj;
54cdf0e10cSrcweir 		DBG_ASSERT( bAllowDoubles ||
55cdf0e10cSrcweir 					( !_nEventId && ( !_pTimer || !_pTimer->IsActive() ) ),
56cdf0e10cSrcweir 					"Schon ein Call unterwegs" );
57cdf0e10cSrcweir 		if( _nEventId )
58cdf0e10cSrcweir 		{
59cdf0e10cSrcweir 			if( _pMutex ) _pMutex->acquire();
60cdf0e10cSrcweir 			Application::RemoveUserEvent( _nEventId );
61cdf0e10cSrcweir 			if( _pMutex ) _pMutex->release();
62cdf0e10cSrcweir 		}
63cdf0e10cSrcweir 		if( _pTimer )_pTimer->Stop();
64cdf0e10cSrcweir 		if( bUseTimer )
65cdf0e10cSrcweir 		{
66cdf0e10cSrcweir 			if( !_pTimer )
67cdf0e10cSrcweir 			{
68cdf0e10cSrcweir 				_pTimer = new Timer;
69cdf0e10cSrcweir 				_pTimer->SetTimeout( 0 );
70cdf0e10cSrcweir 				_pTimer->SetTimeoutHdl( STATIC_LINK(
71cdf0e10cSrcweir 					this, AsynchronLink, HandleCall) );
72cdf0e10cSrcweir 			}
73cdf0e10cSrcweir 			_pTimer->Start();
74cdf0e10cSrcweir 		}
75cdf0e10cSrcweir 		else
76cdf0e10cSrcweir 		{
77cdf0e10cSrcweir 			if( _pMutex ) _pMutex->acquire();
78cdf0e10cSrcweir             Application::PostUserEvent( _nEventId, STATIC_LINK( this, AsynchronLink, HandleCall), 0 );
79cdf0e10cSrcweir 			if( _pMutex ) _pMutex->release();
80cdf0e10cSrcweir 		}
81cdf0e10cSrcweir 	}
82cdf0e10cSrcweir }
83cdf0e10cSrcweir 
~AsynchronLink()84cdf0e10cSrcweir AsynchronLink::~AsynchronLink()
85cdf0e10cSrcweir {
86cdf0e10cSrcweir 	if( _nEventId )
87cdf0e10cSrcweir 	{
88cdf0e10cSrcweir 		Application::RemoveUserEvent( _nEventId );
89cdf0e10cSrcweir 	}
90cdf0e10cSrcweir 	delete _pTimer;
91cdf0e10cSrcweir 	if( _pDeleted ) *_pDeleted = sal_True;
92cdf0e10cSrcweir 	delete _pMutex;
93cdf0e10cSrcweir }
94cdf0e10cSrcweir 
IMPL_STATIC_LINK(AsynchronLink,HandleCall,void *,EMPTYARG)95cdf0e10cSrcweir IMPL_STATIC_LINK( AsynchronLink, HandleCall, void*, EMPTYARG )
96cdf0e10cSrcweir {
97cdf0e10cSrcweir 	if( pThis->_pMutex ) pThis->_pMutex->acquire();
98cdf0e10cSrcweir 	pThis->_nEventId = 0;
99cdf0e10cSrcweir 	if( pThis->_pMutex ) pThis->_pMutex->release();
100cdf0e10cSrcweir 	pThis->Call_Impl( pThis->_pArg );
101cdf0e10cSrcweir 	return 0;
102cdf0e10cSrcweir }
103cdf0e10cSrcweir 
ForcePendingCall()104cdf0e10cSrcweir void AsynchronLink::ForcePendingCall()
105cdf0e10cSrcweir {
106cdf0e10cSrcweir 	ClearPendingCall();
107cdf0e10cSrcweir 	Call_Impl( _pArg );
108cdf0e10cSrcweir }
109cdf0e10cSrcweir 
ClearPendingCall()110cdf0e10cSrcweir void AsynchronLink::ClearPendingCall()
111cdf0e10cSrcweir {
112cdf0e10cSrcweir     if( _pMutex ) _pMutex->acquire();
113cdf0e10cSrcweir 	if( _nEventId )
114cdf0e10cSrcweir 	{
115cdf0e10cSrcweir 		Application::RemoveUserEvent( _nEventId );
116cdf0e10cSrcweir 		_nEventId = 0;
117cdf0e10cSrcweir 	}
118cdf0e10cSrcweir     if( _pMutex ) _pMutex->release();
119cdf0e10cSrcweir 	if( _pTimer ) _pTimer->Stop();
120cdf0e10cSrcweir }
121cdf0e10cSrcweir 
Call_Impl(void * pArg)122cdf0e10cSrcweir void AsynchronLink::Call_Impl( void* pArg )
123cdf0e10cSrcweir {
124cdf0e10cSrcweir 	_bInCall = sal_True;
125cdf0e10cSrcweir 	sal_Bool bDeleted = sal_False;
126cdf0e10cSrcweir 	_pDeleted = &bDeleted;
127cdf0e10cSrcweir 	_aLink.Call( pArg );
128cdf0e10cSrcweir 	if( !bDeleted )
129cdf0e10cSrcweir 	{
130cdf0e10cSrcweir 		_bInCall = sal_False;
131cdf0e10cSrcweir 		_pDeleted = 0;
132cdf0e10cSrcweir 	}
133cdf0e10cSrcweir }
134cdf0e10cSrcweir 
135cdf0e10cSrcweir }
136