1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_cppu.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #include "osl/mutex.hxx"
32*cdf0e10cSrcweir #include "osl/thread.h"
33*cdf0e10cSrcweir 
34*cdf0e10cSrcweir #include "cppu/helper/purpenv/Environment.hxx"
35*cdf0e10cSrcweir #include "cppu/helper/purpenv/Mapping.hxx"
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir #ifdef debug
39*cdf0e10cSrcweir # define LOG_LIFECYCLE_UnsafeBridge
40*cdf0e10cSrcweir #endif
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir #ifdef LOG_LIFECYCLE_UnsafeBridge
43*cdf0e10cSrcweir #  include <iostream>
44*cdf0e10cSrcweir #  define LOG_LIFECYCLE_UnsafeBridge_emit(x) x
45*cdf0e10cSrcweir 
46*cdf0e10cSrcweir #else
47*cdf0e10cSrcweir #  define LOG_LIFECYCLE_UnsafeBridge_emit(x)
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir #endif
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir class SAL_DLLPRIVATE UnsafeBridge : public cppu::Enterable
53*cdf0e10cSrcweir {
54*cdf0e10cSrcweir 	osl::Mutex          m_mutex;
55*cdf0e10cSrcweir 	sal_Int32           m_count;
56*cdf0e10cSrcweir 	oslThreadIdentifier m_threadId;
57*cdf0e10cSrcweir 
58*cdf0e10cSrcweir 	virtual  ~UnsafeBridge(void);
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir public:
61*cdf0e10cSrcweir 	explicit UnsafeBridge(void);
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir 	virtual void v_callInto_v(uno_EnvCallee * pCallee, va_list * pParam);
64*cdf0e10cSrcweir 	virtual void v_callOut_v (uno_EnvCallee * pCallee, va_list * pParam);
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir 	virtual void v_enter(void);
67*cdf0e10cSrcweir 	virtual void v_leave(void);
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir 	virtual int  v_isValid(rtl::OUString * pReason);
70*cdf0e10cSrcweir };
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir UnsafeBridge::UnsafeBridge(void)
73*cdf0e10cSrcweir 	: m_count   (0),
74*cdf0e10cSrcweir 	  m_threadId(0)
75*cdf0e10cSrcweir {
76*cdf0e10cSrcweir 	LOG_LIFECYCLE_UnsafeBridge_emit(fprintf(stderr, "LIFE: %s -> %p\n", "UnsafeBridge::UnsafeBridge(uno_Environment * pEnv)", this));
77*cdf0e10cSrcweir }
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir UnsafeBridge::~UnsafeBridge(void)
80*cdf0e10cSrcweir {
81*cdf0e10cSrcweir 	LOG_LIFECYCLE_UnsafeBridge_emit(fprintf(stderr, "LIFE: %s -> %p\n", "UnsafeBridge::~UnsafeBridge(void)", this));
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir 	OSL_ASSERT(m_count >= 0);
84*cdf0e10cSrcweir }
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir void UnsafeBridge::v_callInto_v(uno_EnvCallee * pCallee, va_list * pParam)
87*cdf0e10cSrcweir {
88*cdf0e10cSrcweir 	enter();
89*cdf0e10cSrcweir 	pCallee(pParam);
90*cdf0e10cSrcweir 	leave();
91*cdf0e10cSrcweir }
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir void UnsafeBridge::v_callOut_v(uno_EnvCallee * pCallee, va_list * pParam)
94*cdf0e10cSrcweir {
95*cdf0e10cSrcweir 	OSL_ASSERT(m_count > 0);
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir 	-- m_count;
98*cdf0e10cSrcweir 	pCallee(pParam);
99*cdf0e10cSrcweir 	++ m_count;
100*cdf0e10cSrcweir 
101*cdf0e10cSrcweir 	if (!m_threadId)
102*cdf0e10cSrcweir 		m_threadId = osl_getThreadIdentifier(NULL);
103*cdf0e10cSrcweir }
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir void UnsafeBridge::v_enter(void)
106*cdf0e10cSrcweir {
107*cdf0e10cSrcweir 	m_mutex.acquire();
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir 	OSL_ASSERT(m_count >= 0);
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir 	if (m_count == 0)
112*cdf0e10cSrcweir 		m_threadId = osl_getThreadIdentifier(NULL);
113*cdf0e10cSrcweir 
114*cdf0e10cSrcweir 	++ m_count;
115*cdf0e10cSrcweir }
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir void UnsafeBridge::v_leave(void)
118*cdf0e10cSrcweir {
119*cdf0e10cSrcweir 	OSL_ASSERT(m_count > 0);
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir 	-- m_count;
122*cdf0e10cSrcweir 	if (!m_count)
123*cdf0e10cSrcweir 		m_threadId = 0;
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir 	m_mutex.release();
127*cdf0e10cSrcweir }
128*cdf0e10cSrcweir 
129*cdf0e10cSrcweir int UnsafeBridge::v_isValid(rtl::OUString * pReason)
130*cdf0e10cSrcweir {
131*cdf0e10cSrcweir 	int result = 1;
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir 	result = m_count > 0;
134*cdf0e10cSrcweir 	if (!result)
135*cdf0e10cSrcweir 		*pReason = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("not entered"));
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir 	else
138*cdf0e10cSrcweir 	{
139*cdf0e10cSrcweir 		result = m_threadId == osl_getThreadIdentifier(NULL);
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir 		if (!result)
142*cdf0e10cSrcweir 			*pReason = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("wrong thread"));
143*cdf0e10cSrcweir 	}
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir 	if (result)
146*cdf0e10cSrcweir 		*pReason = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("OK"));
147*cdf0e10cSrcweir 
148*cdf0e10cSrcweir 	return result;
149*cdf0e10cSrcweir }
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir extern "C" void SAL_DLLPUBLIC_EXPORT SAL_CALL uno_initEnvironment(uno_Environment * pEnv)
152*cdf0e10cSrcweir 	SAL_THROW_EXTERN_C()
153*cdf0e10cSrcweir {
154*cdf0e10cSrcweir     cppu::helper::purpenv::Environment_initWithEnterable(pEnv, new UnsafeBridge());
155*cdf0e10cSrcweir }
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir extern "C" void SAL_DLLPUBLIC_EXPORT SAL_CALL uno_ext_getMapping(uno_Mapping     ** ppMapping,
158*cdf0e10cSrcweir 								   uno_Environment  * pFrom,
159*cdf0e10cSrcweir 								   uno_Environment  * pTo )
160*cdf0e10cSrcweir {
161*cdf0e10cSrcweir 	cppu::helper::purpenv::createMapping(ppMapping, pFrom, pTo);
162*cdf0e10cSrcweir }
163*cdf0e10cSrcweir 
164