1*129fa3d1SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*129fa3d1SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*129fa3d1SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*129fa3d1SAndrew Rist * distributed with this work for additional information
6*129fa3d1SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*129fa3d1SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*129fa3d1SAndrew Rist * "License"); you may not use this file except in compliance
9*129fa3d1SAndrew Rist * with the License. You may obtain a copy of the License at
10cdf0e10cSrcweir *
11*129fa3d1SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir *
13*129fa3d1SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*129fa3d1SAndrew Rist * software distributed under the License is distributed on an
15*129fa3d1SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*129fa3d1SAndrew Rist * KIND, either express or implied. See the License for the
17*129fa3d1SAndrew Rist * specific language governing permissions and limitations
18*129fa3d1SAndrew Rist * under the License.
19cdf0e10cSrcweir *
20*129fa3d1SAndrew Rist *************************************************************/
21*129fa3d1SAndrew Rist
22*129fa3d1SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_cppu.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include "osl/mutex.hxx"
28cdf0e10cSrcweir #include "osl/thread.h"
29cdf0e10cSrcweir
30cdf0e10cSrcweir #include "cppu/helper/purpenv/Environment.hxx"
31cdf0e10cSrcweir #include "cppu/helper/purpenv/Mapping.hxx"
32cdf0e10cSrcweir
33cdf0e10cSrcweir
34cdf0e10cSrcweir #ifdef debug
35cdf0e10cSrcweir # define LOG_LIFECYCLE_UnsafeBridge
36cdf0e10cSrcweir #endif
37cdf0e10cSrcweir
38cdf0e10cSrcweir #ifdef LOG_LIFECYCLE_UnsafeBridge
39cdf0e10cSrcweir # include <iostream>
40cdf0e10cSrcweir # define LOG_LIFECYCLE_UnsafeBridge_emit(x) x
41cdf0e10cSrcweir
42cdf0e10cSrcweir #else
43cdf0e10cSrcweir # define LOG_LIFECYCLE_UnsafeBridge_emit(x)
44cdf0e10cSrcweir
45cdf0e10cSrcweir #endif
46cdf0e10cSrcweir
47cdf0e10cSrcweir
48cdf0e10cSrcweir class SAL_DLLPRIVATE UnsafeBridge : public cppu::Enterable
49cdf0e10cSrcweir {
50cdf0e10cSrcweir osl::Mutex m_mutex;
51cdf0e10cSrcweir sal_Int32 m_count;
52cdf0e10cSrcweir oslThreadIdentifier m_threadId;
53cdf0e10cSrcweir
54cdf0e10cSrcweir virtual ~UnsafeBridge(void);
55cdf0e10cSrcweir
56cdf0e10cSrcweir public:
57cdf0e10cSrcweir explicit UnsafeBridge(void);
58cdf0e10cSrcweir
59cdf0e10cSrcweir virtual void v_callInto_v(uno_EnvCallee * pCallee, va_list * pParam);
60cdf0e10cSrcweir virtual void v_callOut_v (uno_EnvCallee * pCallee, va_list * pParam);
61cdf0e10cSrcweir
62cdf0e10cSrcweir virtual void v_enter(void);
63cdf0e10cSrcweir virtual void v_leave(void);
64cdf0e10cSrcweir
65cdf0e10cSrcweir virtual int v_isValid(rtl::OUString * pReason);
66cdf0e10cSrcweir };
67cdf0e10cSrcweir
UnsafeBridge(void)68cdf0e10cSrcweir UnsafeBridge::UnsafeBridge(void)
69cdf0e10cSrcweir : m_count (0),
70cdf0e10cSrcweir m_threadId(0)
71cdf0e10cSrcweir {
72cdf0e10cSrcweir LOG_LIFECYCLE_UnsafeBridge_emit(fprintf(stderr, "LIFE: %s -> %p\n", "UnsafeBridge::UnsafeBridge(uno_Environment * pEnv)", this));
73cdf0e10cSrcweir }
74cdf0e10cSrcweir
~UnsafeBridge(void)75cdf0e10cSrcweir UnsafeBridge::~UnsafeBridge(void)
76cdf0e10cSrcweir {
77cdf0e10cSrcweir LOG_LIFECYCLE_UnsafeBridge_emit(fprintf(stderr, "LIFE: %s -> %p\n", "UnsafeBridge::~UnsafeBridge(void)", this));
78cdf0e10cSrcweir
79cdf0e10cSrcweir OSL_ASSERT(m_count >= 0);
80cdf0e10cSrcweir }
81cdf0e10cSrcweir
v_callInto_v(uno_EnvCallee * pCallee,va_list * pParam)82cdf0e10cSrcweir void UnsafeBridge::v_callInto_v(uno_EnvCallee * pCallee, va_list * pParam)
83cdf0e10cSrcweir {
84cdf0e10cSrcweir enter();
85cdf0e10cSrcweir pCallee(pParam);
86cdf0e10cSrcweir leave();
87cdf0e10cSrcweir }
88cdf0e10cSrcweir
v_callOut_v(uno_EnvCallee * pCallee,va_list * pParam)89cdf0e10cSrcweir void UnsafeBridge::v_callOut_v(uno_EnvCallee * pCallee, va_list * pParam)
90cdf0e10cSrcweir {
91cdf0e10cSrcweir OSL_ASSERT(m_count > 0);
92cdf0e10cSrcweir
93cdf0e10cSrcweir -- m_count;
94cdf0e10cSrcweir pCallee(pParam);
95cdf0e10cSrcweir ++ m_count;
96cdf0e10cSrcweir
97cdf0e10cSrcweir if (!m_threadId)
98cdf0e10cSrcweir m_threadId = osl_getThreadIdentifier(NULL);
99cdf0e10cSrcweir }
100cdf0e10cSrcweir
v_enter(void)101cdf0e10cSrcweir void UnsafeBridge::v_enter(void)
102cdf0e10cSrcweir {
103cdf0e10cSrcweir m_mutex.acquire();
104cdf0e10cSrcweir
105cdf0e10cSrcweir OSL_ASSERT(m_count >= 0);
106cdf0e10cSrcweir
107cdf0e10cSrcweir if (m_count == 0)
108cdf0e10cSrcweir m_threadId = osl_getThreadIdentifier(NULL);
109cdf0e10cSrcweir
110cdf0e10cSrcweir ++ m_count;
111cdf0e10cSrcweir }
112cdf0e10cSrcweir
v_leave(void)113cdf0e10cSrcweir void UnsafeBridge::v_leave(void)
114cdf0e10cSrcweir {
115cdf0e10cSrcweir OSL_ASSERT(m_count > 0);
116cdf0e10cSrcweir
117cdf0e10cSrcweir -- m_count;
118cdf0e10cSrcweir if (!m_count)
119cdf0e10cSrcweir m_threadId = 0;
120cdf0e10cSrcweir
121cdf0e10cSrcweir
122cdf0e10cSrcweir m_mutex.release();
123cdf0e10cSrcweir }
124cdf0e10cSrcweir
v_isValid(rtl::OUString * pReason)125cdf0e10cSrcweir int UnsafeBridge::v_isValid(rtl::OUString * pReason)
126cdf0e10cSrcweir {
127cdf0e10cSrcweir int result = 1;
128cdf0e10cSrcweir
129cdf0e10cSrcweir result = m_count > 0;
130cdf0e10cSrcweir if (!result)
131cdf0e10cSrcweir *pReason = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("not entered"));
132cdf0e10cSrcweir
133cdf0e10cSrcweir else
134cdf0e10cSrcweir {
135cdf0e10cSrcweir result = m_threadId == osl_getThreadIdentifier(NULL);
136cdf0e10cSrcweir
137cdf0e10cSrcweir if (!result)
138cdf0e10cSrcweir *pReason = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("wrong thread"));
139cdf0e10cSrcweir }
140cdf0e10cSrcweir
141cdf0e10cSrcweir if (result)
142cdf0e10cSrcweir *pReason = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("OK"));
143cdf0e10cSrcweir
144cdf0e10cSrcweir return result;
145cdf0e10cSrcweir }
146cdf0e10cSrcweir
uno_initEnvironment(uno_Environment * pEnv)147cdf0e10cSrcweir extern "C" void SAL_DLLPUBLIC_EXPORT SAL_CALL uno_initEnvironment(uno_Environment * pEnv)
148cdf0e10cSrcweir SAL_THROW_EXTERN_C()
149cdf0e10cSrcweir {
150cdf0e10cSrcweir cppu::helper::purpenv::Environment_initWithEnterable(pEnv, new UnsafeBridge());
151cdf0e10cSrcweir }
152cdf0e10cSrcweir
uno_ext_getMapping(uno_Mapping ** ppMapping,uno_Environment * pFrom,uno_Environment * pTo)153cdf0e10cSrcweir extern "C" void SAL_DLLPUBLIC_EXPORT SAL_CALL uno_ext_getMapping(uno_Mapping ** ppMapping,
154cdf0e10cSrcweir uno_Environment * pFrom,
155cdf0e10cSrcweir uno_Environment * pTo )
156cdf0e10cSrcweir {
157cdf0e10cSrcweir cppu::helper::purpenv::createMapping(ppMapping, pFrom, pTo);
158cdf0e10cSrcweir }
159cdf0e10cSrcweir
160