1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
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.
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).
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.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_cppu.hxx"
31 #include "osl/thread.hxx"
32 #include "osl/conditn.hxx"
33 #include "osl/mutex.hxx"
35 #include "cppu/helper/purpenv/Environment.hxx"
36 #include "cppu/helper/purpenv/Mapping.hxx"
40 # define LOG_LIFECYCLE_AffineBridge
43 #ifdef LOG_LIFECYCLE_AffineBridge
45 # define LOG_LIFECYCLE_AffineBridge_emit(x) x
48 # define LOG_LIFECYCLE_AffineBridge_emit(x)
55 class SAL_DLLPRIVATE AffineBridge
: public cppu::Enterable
65 uno_EnvCallee
* m_pCallee
;
68 osl::Mutex m_innerMutex
;
69 oslThreadIdentifier m_innerThreadId
;
70 InnerThread
* m_pInnerThread
;
71 osl::Condition m_innerCondition
;
72 sal_Int32 m_enterCount
;
74 osl::Mutex m_outerMutex
;
75 oslThreadIdentifier m_outerThreadId
;
76 osl::Condition m_outerCondition
;
77 OuterThread
* m_pOuterThread
;
79 explicit AffineBridge(void);
80 virtual ~AffineBridge(void);
82 virtual void v_callInto_v(uno_EnvCallee
* pCallee
, va_list * pParam
);
83 virtual void v_callOut_v (uno_EnvCallee
* pCallee
, va_list * pParam
);
85 virtual void v_enter(void);
86 virtual void v_leave(void);
88 virtual int v_isValid(rtl::OUString
* pReason
);
90 void innerDispatch(void);
91 void outerDispatch(int loop
);
94 class SAL_DLLPRIVATE InnerThread
: public osl::Thread
96 virtual void SAL_CALL
run(void);
98 AffineBridge
* m_pAffineBridge
;
101 InnerThread(AffineBridge
* threadEnvironment
)
102 : m_pAffineBridge(threadEnvironment
)
108 void InnerThread::run(void)
110 m_pAffineBridge
->enter();
111 m_pAffineBridge
->innerDispatch();
112 m_pAffineBridge
->leave();
115 class SAL_DLLPRIVATE OuterThread
: public osl::Thread
117 virtual void SAL_CALL
run(void);
119 AffineBridge
* m_pAffineBridge
;
122 OuterThread(AffineBridge
* threadEnvironment
);
125 OuterThread::OuterThread(AffineBridge
* threadEnvironment
)
126 : m_pAffineBridge(threadEnvironment
)
131 void OuterThread::run(void)
133 osl::MutexGuard
guard(m_pAffineBridge
->m_outerMutex
);
135 m_pAffineBridge
->m_outerThreadId
= getIdentifier();
136 m_pAffineBridge
->outerDispatch(0);
137 m_pAffineBridge
->m_outerThreadId
= 0;
139 m_pAffineBridge
->m_pOuterThread
= NULL
;
140 m_pAffineBridge
= NULL
;
144 AffineBridge::AffineBridge(void)
145 : m_innerThreadId(0),
146 m_pInnerThread (NULL
),
149 m_pOuterThread (NULL
)
151 LOG_LIFECYCLE_AffineBridge_emit(fprintf(stderr
, "LIFE: %s -> %p\n", "AffineBridge::AffineBridge(uno_Environment * pEnv)", this));
154 AffineBridge::~AffineBridge(void)
156 LOG_LIFECYCLE_AffineBridge_emit(fprintf(stderr
, "LIFE: %s -> %p\n", "AffineBridge::~AffineBridge(void)", this));
158 if (m_pInnerThread
&& osl_getThreadIdentifier(NULL
) != m_innerThreadId
)
161 m_innerCondition
.set();
163 m_pInnerThread
->join();
166 delete m_pInnerThread
;
170 m_pOuterThread
->join();
171 delete m_pOuterThread
;
176 void AffineBridge::outerDispatch(int loop
)
178 OSL_ASSERT(m_outerThreadId
== osl_getThreadIdentifier(NULL
));
179 OSL_ASSERT(m_innerThreadId
!= m_outerThreadId
);
185 // FIXME: created outer thread must not wait
186 // in case of no message
187 // note: no message can happen in case newly created
188 // outer thread acquire outerMutex after a real outer
189 // thread enters outerDispatch!
190 m_outerCondition
.wait();
191 m_outerCondition
.reset();
205 m_innerCondition
.set();
212 while(mm
!= CB_DONE
&& loop
);
215 void AffineBridge::innerDispatch(void)
217 OSL_ASSERT(m_innerThreadId
== osl_getThreadIdentifier(NULL
));
218 OSL_ASSERT(m_innerThreadId
!= m_outerThreadId
);
224 m_innerCondition
.wait();
225 m_innerCondition
.reset();
239 m_outerCondition
.set();
246 while(mm
!= CB_DONE
);
249 void AffineBridge::v_callInto_v(uno_EnvCallee
* pCallee
, va_list * pParam
)
251 osl::MutexGuard
guard(m_outerMutex
); // only one thread at a time can call into
253 if (m_innerThreadId
== 0) // no inner thread yet
255 m_pInnerThread
= new InnerThread(this);
256 m_pInnerThread
->resume();
259 bool resetId
= false;
260 if (!m_outerThreadId
)
262 m_outerThreadId
= osl_getThreadIdentifier(NULL
);
266 m_message
= CB_FPOINTER
;
269 m_innerCondition
.set();
277 void AffineBridge::v_callOut_v(uno_EnvCallee
* pCallee
, va_list * pParam
)
279 OSL_ASSERT(m_innerThreadId
);
281 osl::MutexGuard
guard(m_innerMutex
);
283 if (m_outerThreadId
== 0) // no outer thread yet
285 osl::MutexGuard
guard_m_outerMutex(m_outerMutex
);
287 if (m_outerThreadId
== 0)
291 m_pOuterThread
->join();
292 delete m_pOuterThread
;
295 m_pOuterThread
= new OuterThread(this);
299 m_message
= CB_FPOINTER
;
302 m_outerCondition
.set();
307 void AffineBridge::v_enter(void)
309 m_innerMutex
.acquire();
312 m_innerThreadId
= osl_getThreadIdentifier(NULL
);
314 OSL_ASSERT(m_innerThreadId
== osl_getThreadIdentifier(NULL
));
319 void AffineBridge::v_leave(void)
321 OSL_ASSERT(m_innerThreadId
== osl_getThreadIdentifier(NULL
));
327 m_innerMutex
.release();
330 int AffineBridge::v_isValid(rtl::OUString
* pReason
)
334 result
= m_enterCount
> 0;
336 *pReason
= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("not entered"));
340 result
= m_innerThreadId
== osl_getThreadIdentifier(NULL
);
343 *pReason
= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("wrong thread"));
347 *pReason
= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("OK"));
352 extern "C" void SAL_DLLPUBLIC_EXPORT SAL_CALL
uno_initEnvironment(uno_Environment
* pEnv
)
355 cppu::helper::purpenv::Environment_initWithEnterable(pEnv
, new AffineBridge());
358 extern "C" void SAL_DLLPUBLIC_EXPORT SAL_CALL
uno_ext_getMapping(uno_Mapping
** ppMapping
,
359 uno_Environment
* pFrom
,
360 uno_Environment
* pTo
)
362 cppu::helper::purpenv::createMapping(ppMapping
, pFrom
, pTo
);