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