update dev300-m58
[ooovba.git] / cppu / source / AffineBridge / AffineBridge.cxx
blobf8fb3588df0be41f5100fdad61860960d764355c
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: AffineBridge.cxx,v $
10 * $Revision: 1.5 $
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"
42 #ifdef debug
43 # define LOG_LIFECYCLE_AffineBridge
44 #endif
46 #ifdef LOG_LIFECYCLE_AffineBridge
47 # include <iostream>
48 # define LOG_LIFECYCLE_AffineBridge_emit(x) x
50 #else
51 # define LOG_LIFECYCLE_AffineBridge_emit(x)
53 #endif
55 class InnerThread;
56 class OuterThread;
58 class SAL_DLLPRIVATE AffineBridge : public cppu::Enterable
60 public:
61 enum Msg
63 CB_DONE,
64 CB_FPOINTER
67 Msg m_message;
68 uno_EnvCallee * m_pCallee;
69 va_list * m_pParam;
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;
103 public:
104 InnerThread(AffineBridge * threadEnvironment)
105 : m_pAffineBridge(threadEnvironment)
107 create();
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;
124 public:
125 OuterThread(AffineBridge * threadEnvironment);
128 OuterThread::OuterThread(AffineBridge * threadEnvironment)
129 : m_pAffineBridge(threadEnvironment)
131 create();
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),
150 m_enterCount (0),
151 m_outerThreadId(0),
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)
163 m_message = CB_DONE;
164 m_innerCondition.set();
166 m_pInnerThread->join();
169 delete m_pInnerThread;
171 if (m_pOuterThread)
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);
184 Msg mm;
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();
196 mm = m_message;
198 switch(mm)
200 case CB_DONE:
201 break;
203 case CB_FPOINTER:
205 m_pCallee(m_pParam);
207 m_message = CB_DONE;
208 m_innerCondition.set();
209 break;
211 default:
212 abort();
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);
223 Msg mm;
227 m_innerCondition.wait();
228 m_innerCondition.reset();
230 mm = m_message;
232 switch(mm)
234 case CB_DONE:
235 break;
237 case CB_FPOINTER:
239 m_pCallee(m_pParam);
241 m_message = CB_DONE;
242 m_outerCondition.set();
243 break;
245 default:
246 abort();
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);
266 resetId = true;
269 m_message = CB_FPOINTER;
270 m_pCallee = pCallee;
271 m_pParam = pParam;
272 m_innerCondition.set();
274 outerDispatch(1);
276 if (resetId)
277 m_outerThreadId = 0;
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)
292 if (m_pOuterThread)
294 m_pOuterThread->join();
295 delete m_pOuterThread;
298 m_pOuterThread = new OuterThread(this);
302 m_message = CB_FPOINTER;
303 m_pCallee = pCallee;
304 m_pParam = pParam;
305 m_outerCondition.set();
307 innerDispatch();
310 void AffineBridge::v_enter(void)
312 m_innerMutex.acquire();
314 if (!m_enterCount)
315 m_innerThreadId = osl_getThreadIdentifier(NULL);
317 OSL_ASSERT(m_innerThreadId == osl_getThreadIdentifier(NULL));
319 ++ m_enterCount;
322 void AffineBridge::v_leave(void)
324 OSL_ASSERT(m_innerThreadId == osl_getThreadIdentifier(NULL));
326 -- m_enterCount;
327 if (!m_enterCount)
328 m_innerThreadId = 0;
330 m_innerMutex.release();
333 int AffineBridge::v_isValid(rtl::OUString * pReason)
335 int result = 1;
337 result = m_enterCount > 0;
338 if (!result)
339 *pReason = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("not entered"));
341 else
343 result = m_innerThreadId == osl_getThreadIdentifier(NULL);
345 if (!result)
346 *pReason = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("wrong thread"));
349 if (result)
350 *pReason = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("OK"));
352 return result;
355 extern "C" void SAL_DLLPUBLIC_EXPORT SAL_CALL uno_initEnvironment(uno_Environment * pEnv)
356 SAL_THROW_EXTERN_C()
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);