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_dbaccess.hxx"
26 #ifndef DBAUI_ASYNCRONOUSLINK_HXX
27 #include "AsyncronousLink.hxx"
28 #endif
29 #ifndef _SV_SVAPP_HXX
30 #include <vcl/svapp.hxx>
31 #endif
32 #ifndef _TOOLS_DEBUG_HXX
33 #include <tools/debug.hxx>
34 #endif
35 
36 //==================================================================
37 //= OAsyncronousLink
38 //==================================================================
39 using namespace dbaui;
40 DBG_NAME(OAsyncronousLink)
41 //------------------------------------------------------------------
42 OAsyncronousLink::OAsyncronousLink( const Link& _rHandler )
43 	:m_aHandler(_rHandler)
44 	,m_aEventSafety()
45 	,m_aDestructionSafety()
46 	,m_nEventId(0)
47 {
48     DBG_CTOR(OAsyncronousLink,NULL);
49 }
50 
51 //------------------------------------------------------------------
52 OAsyncronousLink::~OAsyncronousLink()
53 {
54 	{
55 		::osl::MutexGuard aEventGuard( m_aEventSafety );
56 		if ( m_nEventId )
57 			Application::RemoveUserEvent(m_nEventId);
58 		m_nEventId = 0;
59 	}
60 
61 	{
62 		::osl::MutexGuard aDestructionGuard( m_aDestructionSafety );
63 		// this is just for the case we're deleted while another thread just handled the event :
64 		// if this other thread called our link while we were deleting the event here, the
65 		// link handler blocked. With leaving the above block it continued, but now we are prevented
66 		// to leave this destructor 'til the link handler recognizes that nEvent == 0 and leaves.
67 	}
68     DBG_DTOR(OAsyncronousLink,NULL);
69 }
70 
71 
72 //------------------------------------------------------------------
73 void OAsyncronousLink::Call( void* _pArgument )
74 {
75 	::osl::MutexGuard aEventGuard( m_aEventSafety );
76 	if (m_nEventId)
77 		Application::RemoveUserEvent(m_nEventId);
78 	m_nEventId = Application::PostUserEvent( LINK( this, OAsyncronousLink, OnAsyncCall ), _pArgument );
79 }
80 
81 //------------------------------------------------------------------
82 void OAsyncronousLink::CancelCall()
83 {
84 	::osl::MutexGuard aEventGuard( m_aEventSafety );
85 	if ( m_nEventId )
86 		Application::RemoveUserEvent( m_nEventId );
87 	m_nEventId = 0;
88 }
89 
90 //------------------------------------------------------------------
91 IMPL_LINK(OAsyncronousLink, OnAsyncCall, void*, _pArg)
92 {
93 	{
94 		::osl::MutexGuard aDestructionGuard( m_aDestructionSafety );
95 		{
96 			::osl::MutexGuard aEventGuard( m_aEventSafety );
97 			if (!m_nEventId)
98 				// our destructor deleted the event just while we we're waiting for m_aEventSafety
99 				// -> get outta here
100 				return 0;
101 			m_nEventId = 0;
102 		}
103 	}
104 	if (m_aHandler.IsSet())
105 		return m_aHandler.Call(_pArg);
106 
107 	return 0L;
108 }
109