1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: AffineBridge.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_cppu.hxx"
34 #include "osl/thread.hxx"
35 #include "osl/conditn.hxx"
36 #include "osl/mutex.hxx"
38 #include "cppu/helper/purpenv/Environment.hxx"
39 #include "cppu/helper/purpenv/Mapping.hxx"
43 # define LOG_LIFECYCLE_AffineBridge
46 #ifdef LOG_LIFECYCLE_AffineBridge
48 # define LOG_LIFECYCLE_AffineBridge_emit(x) x
51 # define LOG_LIFECYCLE_AffineBridge_emit(x)
58 class SAL_DLLPRIVATE AffineBridge
: public cppu::Enterable
68 uno_EnvCallee
* m_pCallee
;
71 osl::Mutex m_innerMutex
;
72 oslThreadIdentifier m_innerThreadId
;
73 InnerThread
* m_pInnerThread
;
74 osl::Condition m_innerCondition
;
75 sal_Int32 m_enterCount
;
77 osl::Mutex m_outerMutex
;
78 oslThreadIdentifier m_outerThreadId
;
79 osl::Condition m_outerCondition
;
80 OuterThread
* m_pOuterThread
;
82 explicit AffineBridge(void);
83 virtual ~AffineBridge(void);
85 virtual void v_callInto_v(uno_EnvCallee
* pCallee
, va_list * pParam
);
86 virtual void v_callOut_v (uno_EnvCallee
* pCallee
, va_list * pParam
);
88 virtual void v_enter(void);
89 virtual void v_leave(void);
91 virtual int v_isValid(rtl::OUString
* pReason
);
93 void innerDispatch(void);
94 void outerDispatch(int loop
);
97 class SAL_DLLPRIVATE InnerThread
: public osl::Thread
99 virtual void SAL_CALL
run(void);
101 AffineBridge
* m_pAffineBridge
;
104 InnerThread(AffineBridge
* threadEnvironment
)
105 : m_pAffineBridge(threadEnvironment
)
111 void InnerThread::run(void)
113 m_pAffineBridge
->enter();
114 m_pAffineBridge
->innerDispatch();
115 m_pAffineBridge
->leave();
118 class SAL_DLLPRIVATE OuterThread
: public osl::Thread
120 virtual void SAL_CALL
run(void);
122 AffineBridge
* m_pAffineBridge
;
125 OuterThread(AffineBridge
* threadEnvironment
);
128 OuterThread::OuterThread(AffineBridge
* threadEnvironment
)
129 : m_pAffineBridge(threadEnvironment
)
134 void OuterThread::run(void)
136 osl::MutexGuard
guard(m_pAffineBridge
->m_outerMutex
);
138 m_pAffineBridge
->m_outerThreadId
= getIdentifier();
139 m_pAffineBridge
->outerDispatch(0);
140 m_pAffineBridge
->m_outerThreadId
= 0;
142 m_pAffineBridge
->m_pOuterThread
= NULL
;
143 m_pAffineBridge
= NULL
;
147 AffineBridge::AffineBridge(void)
148 : m_innerThreadId(0),
149 m_pInnerThread (NULL
),
152 m_pOuterThread (NULL
)
154 LOG_LIFECYCLE_AffineBridge_emit(fprintf(stderr
, "LIFE: %s -> %p\n", "AffineBridge::AffineBridge(uno_Environment * pEnv)", this));
157 AffineBridge::~AffineBridge(void)
159 LOG_LIFECYCLE_AffineBridge_emit(fprintf(stderr
, "LIFE: %s -> %p\n", "AffineBridge::~AffineBridge(void)", this));
161 if (m_pInnerThread
&& osl_getThreadIdentifier(NULL
) != m_innerThreadId
)
164 m_innerCondition
.set();
166 m_pInnerThread
->join();
169 delete m_pInnerThread
;
173 m_pOuterThread
->join();
174 delete m_pOuterThread
;
179 void AffineBridge::outerDispatch(int loop
)
181 OSL_ASSERT(m_outerThreadId
== osl_getThreadIdentifier(NULL
));
182 OSL_ASSERT(m_innerThreadId
!= m_outerThreadId
);
188 // FIXME: created outer thread must not wait
189 // in case of no message
190 // note: no message can happen in case newly created
191 // outer thread acquire outerMutex after a real outer
192 // thread enters outerDispatch!
193 m_outerCondition
.wait();
194 m_outerCondition
.reset();
208 m_innerCondition
.set();
215 while(mm
!= CB_DONE
&& loop
);
218 void AffineBridge::innerDispatch(void)
220 OSL_ASSERT(m_innerThreadId
== osl_getThreadIdentifier(NULL
));
221 OSL_ASSERT(m_innerThreadId
!= m_outerThreadId
);
227 m_innerCondition
.wait();
228 m_innerCondition
.reset();
242 m_outerCondition
.set();
249 while(mm
!= CB_DONE
);
252 void AffineBridge::v_callInto_v(uno_EnvCallee
* pCallee
, va_list * pParam
)
254 osl::MutexGuard
guard(m_outerMutex
); // only one thread at a time can call into
256 if (m_innerThreadId
== 0) // no inner thread yet
258 m_pInnerThread
= new InnerThread(this);
259 m_pInnerThread
->resume();
262 bool resetId
= false;
263 if (!m_outerThreadId
)
265 m_outerThreadId
= osl_getThreadIdentifier(NULL
);
269 m_message
= CB_FPOINTER
;
272 m_innerCondition
.set();
280 void AffineBridge::v_callOut_v(uno_EnvCallee
* pCallee
, va_list * pParam
)
282 OSL_ASSERT(m_innerThreadId
);
284 osl::MutexGuard
guard(m_innerMutex
);
286 if (m_outerThreadId
== 0) // no outer thread yet
288 osl::MutexGuard
guard_m_outerMutex(m_outerMutex
);
290 if (m_outerThreadId
== 0)
294 m_pOuterThread
->join();
295 delete m_pOuterThread
;
298 m_pOuterThread
= new OuterThread(this);
302 m_message
= CB_FPOINTER
;
305 m_outerCondition
.set();
310 void AffineBridge::v_enter(void)
312 m_innerMutex
.acquire();
315 m_innerThreadId
= osl_getThreadIdentifier(NULL
);
317 OSL_ASSERT(m_innerThreadId
== osl_getThreadIdentifier(NULL
));
322 void AffineBridge::v_leave(void)
324 OSL_ASSERT(m_innerThreadId
== osl_getThreadIdentifier(NULL
));
330 m_innerMutex
.release();
333 int AffineBridge::v_isValid(rtl::OUString
* pReason
)
337 result
= m_enterCount
> 0;
339 *pReason
= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("not entered"));
343 result
= m_innerThreadId
== osl_getThreadIdentifier(NULL
);
346 *pReason
= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("wrong thread"));
350 *pReason
= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("OK"));
355 extern "C" void SAL_DLLPUBLIC_EXPORT SAL_CALL
uno_initEnvironment(uno_Environment
* pEnv
)
358 cppu::helper::purpenv::Environment_initWithEnterable(pEnv
, new AffineBridge());
361 extern "C" void SAL_DLLPUBLIC_EXPORT SAL_CALL
uno_ext_getMapping(uno_Mapping
** ppMapping
,
362 uno_Environment
* pFrom
,
363 uno_Environment
* pTo
)
365 cppu::helper::purpenv::createMapping(ppMapping
, pFrom
, pTo
);