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_cppu.hxx"
26 
27 #include "osl/mutex.hxx"
28 #include "osl/thread.h"
29 
30 #include "cppu/helper/purpenv/Environment.hxx"
31 #include "cppu/helper/purpenv/Mapping.hxx"
32 
33 
34 #ifdef debug
35 # define LOG_LIFECYCLE_UnsafeBridge
36 #endif
37 
38 #ifdef LOG_LIFECYCLE_UnsafeBridge
39 #  include <iostream>
40 #  define LOG_LIFECYCLE_UnsafeBridge_emit(x) x
41 
42 #else
43 #  define LOG_LIFECYCLE_UnsafeBridge_emit(x)
44 
45 #endif
46 
47 
48 class SAL_DLLPRIVATE UnsafeBridge : public cppu::Enterable
49 {
50 	osl::Mutex          m_mutex;
51 	sal_Int32           m_count;
52 	oslThreadIdentifier m_threadId;
53 
54 	virtual  ~UnsafeBridge(void);
55 
56 public:
57 	explicit UnsafeBridge(void);
58 
59 	virtual void v_callInto_v(uno_EnvCallee * pCallee, va_list * pParam);
60 	virtual void v_callOut_v (uno_EnvCallee * pCallee, va_list * pParam);
61 
62 	virtual void v_enter(void);
63 	virtual void v_leave(void);
64 
65 	virtual int  v_isValid(rtl::OUString * pReason);
66 };
67 
UnsafeBridge(void)68 UnsafeBridge::UnsafeBridge(void)
69 	: m_count   (0),
70 	  m_threadId(0)
71 {
72 	LOG_LIFECYCLE_UnsafeBridge_emit(fprintf(stderr, "LIFE: %s -> %p\n", "UnsafeBridge::UnsafeBridge(uno_Environment * pEnv)", this));
73 }
74 
~UnsafeBridge(void)75 UnsafeBridge::~UnsafeBridge(void)
76 {
77 	LOG_LIFECYCLE_UnsafeBridge_emit(fprintf(stderr, "LIFE: %s -> %p\n", "UnsafeBridge::~UnsafeBridge(void)", this));
78 
79 	OSL_ASSERT(m_count >= 0);
80 }
81 
v_callInto_v(uno_EnvCallee * pCallee,va_list * pParam)82 void UnsafeBridge::v_callInto_v(uno_EnvCallee * pCallee, va_list * pParam)
83 {
84 	enter();
85 	pCallee(pParam);
86 	leave();
87 }
88 
v_callOut_v(uno_EnvCallee * pCallee,va_list * pParam)89 void UnsafeBridge::v_callOut_v(uno_EnvCallee * pCallee, va_list * pParam)
90 {
91 	OSL_ASSERT(m_count > 0);
92 
93 	-- m_count;
94 	pCallee(pParam);
95 	++ m_count;
96 
97 	if (!m_threadId)
98 		m_threadId = osl_getThreadIdentifier(NULL);
99 }
100 
v_enter(void)101 void UnsafeBridge::v_enter(void)
102 {
103 	m_mutex.acquire();
104 
105 	OSL_ASSERT(m_count >= 0);
106 
107 	if (m_count == 0)
108 		m_threadId = osl_getThreadIdentifier(NULL);
109 
110 	++ m_count;
111 }
112 
v_leave(void)113 void UnsafeBridge::v_leave(void)
114 {
115 	OSL_ASSERT(m_count > 0);
116 
117 	-- m_count;
118 	if (!m_count)
119 		m_threadId = 0;
120 
121 
122 	m_mutex.release();
123 }
124 
v_isValid(rtl::OUString * pReason)125 int UnsafeBridge::v_isValid(rtl::OUString * pReason)
126 {
127 	int result = 1;
128 
129 	result = m_count > 0;
130 	if (!result)
131 		*pReason = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("not entered"));
132 
133 	else
134 	{
135 		result = m_threadId == osl_getThreadIdentifier(NULL);
136 
137 		if (!result)
138 			*pReason = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("wrong thread"));
139 	}
140 
141 	if (result)
142 		*pReason = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("OK"));
143 
144 	return result;
145 }
146 
uno_initEnvironment(uno_Environment * pEnv)147 extern "C" void SAL_DLLPUBLIC_EXPORT SAL_CALL uno_initEnvironment(uno_Environment * pEnv)
148 	SAL_THROW_EXTERN_C()
149 {
150     cppu::helper::purpenv::Environment_initWithEnterable(pEnv, new UnsafeBridge());
151 }
152 
uno_ext_getMapping(uno_Mapping ** ppMapping,uno_Environment * pFrom,uno_Environment * pTo)153 extern "C" void SAL_DLLPUBLIC_EXPORT SAL_CALL uno_ext_getMapping(uno_Mapping     ** ppMapping,
154 								   uno_Environment  * pFrom,
155 								   uno_Environment  * pTo )
156 {
157 	cppu::helper::purpenv::createMapping(ppMapping, pFrom, pTo);
158 }
159 
160