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
10*129fa3d1SAndrew Rist  *
11*129fa3d1SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*129fa3d1SAndrew Rist  *
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.
19*129fa3d1SAndrew Rist  *
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/thread.hxx"
28cdf0e10cSrcweir #include "osl/conditn.hxx"
29cdf0e10cSrcweir #include "osl/mutex.hxx"
30cdf0e10cSrcweir 
31cdf0e10cSrcweir #include "cppu/helper/purpenv/Environment.hxx"
32cdf0e10cSrcweir #include "cppu/helper/purpenv/Mapping.hxx"
33cdf0e10cSrcweir 
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #ifdef debug
36cdf0e10cSrcweir # define LOG_LIFECYCLE_AffineBridge
37cdf0e10cSrcweir #endif
38cdf0e10cSrcweir 
39cdf0e10cSrcweir #ifdef LOG_LIFECYCLE_AffineBridge
40cdf0e10cSrcweir #  include <iostream>
41cdf0e10cSrcweir #  define LOG_LIFECYCLE_AffineBridge_emit(x) x
42cdf0e10cSrcweir 
43cdf0e10cSrcweir #else
44cdf0e10cSrcweir #  define LOG_LIFECYCLE_AffineBridge_emit(x)
45cdf0e10cSrcweir 
46cdf0e10cSrcweir #endif
47cdf0e10cSrcweir 
48cdf0e10cSrcweir class InnerThread;
49cdf0e10cSrcweir class OuterThread;
50cdf0e10cSrcweir 
51cdf0e10cSrcweir class SAL_DLLPRIVATE AffineBridge : public cppu::Enterable
52cdf0e10cSrcweir {
53cdf0e10cSrcweir public:
54cdf0e10cSrcweir 	enum Msg
55cdf0e10cSrcweir 	{
56cdf0e10cSrcweir 		CB_DONE,
57cdf0e10cSrcweir 		CB_FPOINTER
58cdf0e10cSrcweir 	};
59cdf0e10cSrcweir 
60cdf0e10cSrcweir 	Msg                   m_message;
61cdf0e10cSrcweir 	uno_EnvCallee       * m_pCallee;
62cdf0e10cSrcweir 	va_list             * m_pParam;
63cdf0e10cSrcweir 
64cdf0e10cSrcweir 	osl::Mutex            m_innerMutex;
65cdf0e10cSrcweir 	oslThreadIdentifier   m_innerThreadId;
66cdf0e10cSrcweir 	InnerThread         * m_pInnerThread;
67cdf0e10cSrcweir 	osl::Condition        m_innerCondition;
68cdf0e10cSrcweir 	sal_Int32             m_enterCount;
69cdf0e10cSrcweir 
70cdf0e10cSrcweir 	osl::Mutex            m_outerMutex;
71cdf0e10cSrcweir 	oslThreadIdentifier   m_outerThreadId;
72cdf0e10cSrcweir 	osl::Condition        m_outerCondition;
73cdf0e10cSrcweir 	OuterThread         * m_pOuterThread;
74cdf0e10cSrcweir 
75cdf0e10cSrcweir 	explicit  AffineBridge(void);
76cdf0e10cSrcweir 	virtual  ~AffineBridge(void);
77cdf0e10cSrcweir 
78cdf0e10cSrcweir 	virtual void  v_callInto_v(uno_EnvCallee * pCallee, va_list * pParam);
79cdf0e10cSrcweir 	virtual void  v_callOut_v (uno_EnvCallee * pCallee, va_list * pParam);
80cdf0e10cSrcweir 
81cdf0e10cSrcweir 	virtual void  v_enter(void);
82cdf0e10cSrcweir 	virtual void  v_leave(void);
83cdf0e10cSrcweir 
84cdf0e10cSrcweir 	virtual int  v_isValid(rtl::OUString * pReason);
85cdf0e10cSrcweir 
86cdf0e10cSrcweir 	void innerDispatch(void);
87cdf0e10cSrcweir 	void outerDispatch(int loop);
88cdf0e10cSrcweir };
89cdf0e10cSrcweir 
90cdf0e10cSrcweir class SAL_DLLPRIVATE InnerThread : public osl::Thread
91cdf0e10cSrcweir {
92cdf0e10cSrcweir     virtual void SAL_CALL run(void);
93cdf0e10cSrcweir 
94cdf0e10cSrcweir 	AffineBridge * m_pAffineBridge;
95cdf0e10cSrcweir 
96cdf0e10cSrcweir public:
InnerThread(AffineBridge * threadEnvironment)97cdf0e10cSrcweir 	InnerThread(AffineBridge * threadEnvironment)
98cdf0e10cSrcweir 		: m_pAffineBridge(threadEnvironment)
99cdf0e10cSrcweir 		{
100cdf0e10cSrcweir 			create();
101cdf0e10cSrcweir 		}
102cdf0e10cSrcweir };
103cdf0e10cSrcweir 
run(void)104cdf0e10cSrcweir void InnerThread::run(void)
105cdf0e10cSrcweir {
106cdf0e10cSrcweir 	m_pAffineBridge->enter();
107cdf0e10cSrcweir 	m_pAffineBridge->innerDispatch();
108cdf0e10cSrcweir 	m_pAffineBridge->leave();
109cdf0e10cSrcweir }
110cdf0e10cSrcweir 
111cdf0e10cSrcweir class SAL_DLLPRIVATE OuterThread : public osl::Thread
112cdf0e10cSrcweir {
113cdf0e10cSrcweir     virtual void SAL_CALL run(void);
114cdf0e10cSrcweir 
115cdf0e10cSrcweir 	AffineBridge * m_pAffineBridge;
116cdf0e10cSrcweir 
117cdf0e10cSrcweir public:
118cdf0e10cSrcweir 	OuterThread(AffineBridge * threadEnvironment);
119cdf0e10cSrcweir };
120cdf0e10cSrcweir 
OuterThread(AffineBridge * threadEnvironment)121cdf0e10cSrcweir OuterThread::OuterThread(AffineBridge * threadEnvironment)
122cdf0e10cSrcweir 	: m_pAffineBridge(threadEnvironment)
123cdf0e10cSrcweir {
124cdf0e10cSrcweir 	create();
125cdf0e10cSrcweir }
126cdf0e10cSrcweir 
run(void)127cdf0e10cSrcweir void OuterThread::run(void)
128cdf0e10cSrcweir {
129cdf0e10cSrcweir 	osl::MutexGuard guard(m_pAffineBridge->m_outerMutex);
130cdf0e10cSrcweir 
131cdf0e10cSrcweir 	m_pAffineBridge->m_outerThreadId = getIdentifier();
132cdf0e10cSrcweir 	m_pAffineBridge->outerDispatch(0);
133cdf0e10cSrcweir 	m_pAffineBridge->m_outerThreadId = 0;
134cdf0e10cSrcweir 
135cdf0e10cSrcweir 	m_pAffineBridge->m_pOuterThread = NULL;
136cdf0e10cSrcweir 	m_pAffineBridge = NULL;
137cdf0e10cSrcweir }
138cdf0e10cSrcweir 
139cdf0e10cSrcweir 
AffineBridge(void)140cdf0e10cSrcweir AffineBridge::AffineBridge(void)
141cdf0e10cSrcweir 	: m_innerThreadId(0),
142cdf0e10cSrcweir 	  m_pInnerThread (NULL),
143cdf0e10cSrcweir 	  m_enterCount   (0),
144cdf0e10cSrcweir 	  m_outerThreadId(0),
145cdf0e10cSrcweir 	  m_pOuterThread (NULL)
146cdf0e10cSrcweir {
147cdf0e10cSrcweir 	LOG_LIFECYCLE_AffineBridge_emit(fprintf(stderr, "LIFE: %s -> %p\n", "AffineBridge::AffineBridge(uno_Environment * pEnv)", this));
148cdf0e10cSrcweir }
149cdf0e10cSrcweir 
~AffineBridge(void)150cdf0e10cSrcweir AffineBridge::~AffineBridge(void)
151cdf0e10cSrcweir {
152cdf0e10cSrcweir 	LOG_LIFECYCLE_AffineBridge_emit(fprintf(stderr, "LIFE: %s -> %p\n", "AffineBridge::~AffineBridge(void)", this));
153cdf0e10cSrcweir 
154cdf0e10cSrcweir 	if (m_pInnerThread && osl_getThreadIdentifier(NULL) != m_innerThreadId)
155cdf0e10cSrcweir 	{
156cdf0e10cSrcweir 		m_message = CB_DONE;
157cdf0e10cSrcweir 		m_innerCondition.set();
158cdf0e10cSrcweir 
159cdf0e10cSrcweir 		m_pInnerThread->join();
160cdf0e10cSrcweir 	}
161cdf0e10cSrcweir 
162cdf0e10cSrcweir 	delete m_pInnerThread;
163cdf0e10cSrcweir 
164cdf0e10cSrcweir 	if (m_pOuterThread)
165cdf0e10cSrcweir 	{
166cdf0e10cSrcweir 		m_pOuterThread->join();
167cdf0e10cSrcweir 		delete m_pOuterThread;
168cdf0e10cSrcweir 	}
169cdf0e10cSrcweir }
170cdf0e10cSrcweir 
171cdf0e10cSrcweir 
outerDispatch(int loop)172cdf0e10cSrcweir void AffineBridge::outerDispatch(int loop)
173cdf0e10cSrcweir {
174cdf0e10cSrcweir 	OSL_ASSERT(m_outerThreadId == osl_getThreadIdentifier(NULL));
175cdf0e10cSrcweir 	OSL_ASSERT(m_innerThreadId != m_outerThreadId);
176cdf0e10cSrcweir 
177cdf0e10cSrcweir 	Msg mm;
178cdf0e10cSrcweir 
179cdf0e10cSrcweir 	do
180cdf0e10cSrcweir 	{
181cdf0e10cSrcweir 		// FIXME: created outer thread must not wait
182cdf0e10cSrcweir 		// in case of no message
183cdf0e10cSrcweir 		// note: no message can happen in case newly created
184cdf0e10cSrcweir 		// outer thread acquire outerMutex after a real outer
185cdf0e10cSrcweir 		// thread enters outerDispatch!
186cdf0e10cSrcweir 		m_outerCondition.wait();
187cdf0e10cSrcweir 		m_outerCondition.reset();
188cdf0e10cSrcweir 
189cdf0e10cSrcweir 		mm = m_message;
190cdf0e10cSrcweir 
191cdf0e10cSrcweir 		switch(mm)
192cdf0e10cSrcweir 		{
193cdf0e10cSrcweir 		case CB_DONE:
194cdf0e10cSrcweir 			break;
195cdf0e10cSrcweir 
196cdf0e10cSrcweir 		case CB_FPOINTER:
197cdf0e10cSrcweir 		{
198cdf0e10cSrcweir 			m_pCallee(m_pParam);
199cdf0e10cSrcweir 
200cdf0e10cSrcweir 			m_message = CB_DONE;
201cdf0e10cSrcweir 			m_innerCondition.set();
202cdf0e10cSrcweir 			break;
203cdf0e10cSrcweir 		}
204cdf0e10cSrcweir 		default:
205cdf0e10cSrcweir 			abort();
206cdf0e10cSrcweir 		}
207cdf0e10cSrcweir 	}
208cdf0e10cSrcweir 	while(mm != CB_DONE && loop);
209cdf0e10cSrcweir }
210cdf0e10cSrcweir 
innerDispatch(void)211cdf0e10cSrcweir void AffineBridge::innerDispatch(void)
212cdf0e10cSrcweir {
213cdf0e10cSrcweir 	OSL_ASSERT(m_innerThreadId == osl_getThreadIdentifier(NULL));
214cdf0e10cSrcweir 	OSL_ASSERT(m_innerThreadId != m_outerThreadId);
215cdf0e10cSrcweir 
216cdf0e10cSrcweir 	Msg mm;
217cdf0e10cSrcweir 
218cdf0e10cSrcweir 	do
219cdf0e10cSrcweir 	{
220cdf0e10cSrcweir 		m_innerCondition.wait();
221cdf0e10cSrcweir 		m_innerCondition.reset();
222cdf0e10cSrcweir 
223cdf0e10cSrcweir 		mm = m_message;
224cdf0e10cSrcweir 
225cdf0e10cSrcweir 		switch(mm)
226cdf0e10cSrcweir 		{
227cdf0e10cSrcweir 		case CB_DONE:
228cdf0e10cSrcweir 			break;
229cdf0e10cSrcweir 
230cdf0e10cSrcweir 		case CB_FPOINTER:
231cdf0e10cSrcweir 		{
232cdf0e10cSrcweir 			m_pCallee(m_pParam);
233cdf0e10cSrcweir 
234cdf0e10cSrcweir 			m_message = CB_DONE;
235cdf0e10cSrcweir 			m_outerCondition.set();
236cdf0e10cSrcweir 			break;
237cdf0e10cSrcweir 		}
238cdf0e10cSrcweir 		default:
239cdf0e10cSrcweir 			abort();
240cdf0e10cSrcweir 		}
241cdf0e10cSrcweir 	}
242cdf0e10cSrcweir 	while(mm != CB_DONE);
243cdf0e10cSrcweir }
244cdf0e10cSrcweir 
v_callInto_v(uno_EnvCallee * pCallee,va_list * pParam)245cdf0e10cSrcweir void AffineBridge::v_callInto_v(uno_EnvCallee * pCallee, va_list * pParam)
246cdf0e10cSrcweir {
247cdf0e10cSrcweir 	osl::MutexGuard guard(m_outerMutex); // only one thread at a time can call into
248cdf0e10cSrcweir 
249cdf0e10cSrcweir 	if (m_innerThreadId == 0) // no inner thread yet
250cdf0e10cSrcweir 	{
251cdf0e10cSrcweir 		m_pInnerThread  = new InnerThread(this);
252cdf0e10cSrcweir 		m_pInnerThread->resume();
253cdf0e10cSrcweir 	}
254cdf0e10cSrcweir 
255cdf0e10cSrcweir 	bool resetId = false;
256cdf0e10cSrcweir 	if (!m_outerThreadId)
257cdf0e10cSrcweir 	{
258cdf0e10cSrcweir 		m_outerThreadId = osl_getThreadIdentifier(NULL);
259cdf0e10cSrcweir 		resetId = true;
260cdf0e10cSrcweir 	}
261cdf0e10cSrcweir 
262cdf0e10cSrcweir 	m_message = CB_FPOINTER;
263cdf0e10cSrcweir 	m_pCallee = pCallee;
264cdf0e10cSrcweir 	m_pParam  = pParam;
265cdf0e10cSrcweir 	m_innerCondition.set();
266cdf0e10cSrcweir 
267cdf0e10cSrcweir 	outerDispatch(1);
268cdf0e10cSrcweir 
269cdf0e10cSrcweir 	if (resetId)
270cdf0e10cSrcweir 		m_outerThreadId = 0;
271cdf0e10cSrcweir }
272cdf0e10cSrcweir 
v_callOut_v(uno_EnvCallee * pCallee,va_list * pParam)273cdf0e10cSrcweir void AffineBridge::v_callOut_v(uno_EnvCallee * pCallee, va_list * pParam)
274cdf0e10cSrcweir {
275cdf0e10cSrcweir 	OSL_ASSERT(m_innerThreadId);
276cdf0e10cSrcweir 
277cdf0e10cSrcweir 	osl::MutexGuard guard(m_innerMutex);
278cdf0e10cSrcweir 
279cdf0e10cSrcweir 	if (m_outerThreadId == 0) // no outer thread yet
280cdf0e10cSrcweir 	{
281cdf0e10cSrcweir 		osl::MutexGuard guard_m_outerMutex(m_outerMutex);
282cdf0e10cSrcweir 
283cdf0e10cSrcweir 		if (m_outerThreadId == 0)
284cdf0e10cSrcweir 		{
285cdf0e10cSrcweir 			if (m_pOuterThread)
286cdf0e10cSrcweir 			{
287cdf0e10cSrcweir 				m_pOuterThread->join();
288cdf0e10cSrcweir 				delete m_pOuterThread;
289cdf0e10cSrcweir 			}
290cdf0e10cSrcweir 
291cdf0e10cSrcweir 			m_pOuterThread = new OuterThread(this);
292cdf0e10cSrcweir 		}
293cdf0e10cSrcweir 	}
294cdf0e10cSrcweir 
295cdf0e10cSrcweir 	m_message = CB_FPOINTER;
296cdf0e10cSrcweir 	m_pCallee = pCallee;
297cdf0e10cSrcweir 	m_pParam  = pParam;
298cdf0e10cSrcweir 	m_outerCondition.set();
299cdf0e10cSrcweir 
300cdf0e10cSrcweir 	innerDispatch();
301cdf0e10cSrcweir }
302cdf0e10cSrcweir 
v_enter(void)303cdf0e10cSrcweir void AffineBridge::v_enter(void)
304cdf0e10cSrcweir {
305cdf0e10cSrcweir 	m_innerMutex.acquire();
306cdf0e10cSrcweir 
307cdf0e10cSrcweir 	if (!m_enterCount)
308cdf0e10cSrcweir 		m_innerThreadId = osl_getThreadIdentifier(NULL);
309cdf0e10cSrcweir 
310cdf0e10cSrcweir 	OSL_ASSERT(m_innerThreadId == osl_getThreadIdentifier(NULL));
311cdf0e10cSrcweir 
312cdf0e10cSrcweir 	++ m_enterCount;
313cdf0e10cSrcweir }
314cdf0e10cSrcweir 
v_leave(void)315cdf0e10cSrcweir void AffineBridge::v_leave(void)
316cdf0e10cSrcweir {
317cdf0e10cSrcweir 	OSL_ASSERT(m_innerThreadId == osl_getThreadIdentifier(NULL));
318cdf0e10cSrcweir 
319cdf0e10cSrcweir 	-- m_enterCount;
320cdf0e10cSrcweir 	if (!m_enterCount)
321cdf0e10cSrcweir 		m_innerThreadId = 0;
322cdf0e10cSrcweir 
323cdf0e10cSrcweir 	m_innerMutex.release();
324cdf0e10cSrcweir }
325cdf0e10cSrcweir 
v_isValid(rtl::OUString * pReason)326cdf0e10cSrcweir int  AffineBridge::v_isValid(rtl::OUString * pReason)
327cdf0e10cSrcweir {
328cdf0e10cSrcweir 	int result = 1;
329cdf0e10cSrcweir 
330cdf0e10cSrcweir 	result = m_enterCount > 0;
331cdf0e10cSrcweir 	if (!result)
332cdf0e10cSrcweir 		*pReason = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("not entered"));
333cdf0e10cSrcweir 
334cdf0e10cSrcweir 	else
335cdf0e10cSrcweir 	{
336cdf0e10cSrcweir 		result = m_innerThreadId == osl_getThreadIdentifier(NULL);
337cdf0e10cSrcweir 
338cdf0e10cSrcweir 		if (!result)
339cdf0e10cSrcweir 			*pReason = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("wrong thread"));
340cdf0e10cSrcweir 	}
341cdf0e10cSrcweir 
342cdf0e10cSrcweir 	if (result)
343cdf0e10cSrcweir 		*pReason = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("OK"));
344cdf0e10cSrcweir 
345cdf0e10cSrcweir 	return result;
346cdf0e10cSrcweir }
347cdf0e10cSrcweir 
uno_initEnvironment(uno_Environment * pEnv)348cdf0e10cSrcweir extern "C" void SAL_DLLPUBLIC_EXPORT SAL_CALL uno_initEnvironment(uno_Environment * pEnv)
349cdf0e10cSrcweir 	SAL_THROW_EXTERN_C()
350cdf0e10cSrcweir {
351cdf0e10cSrcweir     cppu::helper::purpenv::Environment_initWithEnterable(pEnv, new AffineBridge());
352cdf0e10cSrcweir }
353cdf0e10cSrcweir 
uno_ext_getMapping(uno_Mapping ** ppMapping,uno_Environment * pFrom,uno_Environment * pTo)354cdf0e10cSrcweir extern "C" void SAL_DLLPUBLIC_EXPORT SAL_CALL uno_ext_getMapping(uno_Mapping     ** ppMapping,
355cdf0e10cSrcweir 														uno_Environment  * pFrom,
356cdf0e10cSrcweir 														uno_Environment  * pTo )
357cdf0e10cSrcweir {
358cdf0e10cSrcweir 	cppu::helper::purpenv::createMapping(ppMapping, pFrom, pTo);
359cdf0e10cSrcweir }
360cdf0e10cSrcweir 
361