Impress Remote 1.0.5, tag sdremote-1.0.5
[LibreOffice.git] / cppu / source / AffineBridge / AffineBridge.cxx
blob7d62b46cfa5833f911e4a6af02f8d38c25c636dc
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "osl/thread.hxx"
22 #include "osl/conditn.hxx"
23 #include "osl/mutex.hxx"
25 #include "cppu/helper/purpenv/Environment.hxx"
26 #include "cppu/helper/purpenv/Mapping.hxx"
29 #ifdef debug
30 # define LOG_LIFECYCLE_AffineBridge
31 #endif
33 #ifdef LOG_LIFECYCLE_AffineBridge
34 # include <iostream>
35 # define LOG_LIFECYCLE_AffineBridge_emit(x) x
37 #else
38 # define LOG_LIFECYCLE_AffineBridge_emit(x)
40 #endif
42 class InnerThread;
43 class OuterThread;
45 class SAL_DLLPRIVATE AffineBridge : public cppu::Enterable
47 public:
48 enum Msg
50 CB_DONE,
51 CB_FPOINTER
54 Msg m_message;
55 uno_EnvCallee * m_pCallee;
56 va_list * m_pParam;
58 osl::Mutex m_innerMutex;
59 oslThreadIdentifier m_innerThreadId;
60 InnerThread * m_pInnerThread;
61 osl::Condition m_innerCondition;
62 sal_Int32 m_enterCount;
64 osl::Mutex m_outerMutex;
65 oslThreadIdentifier m_outerThreadId;
66 osl::Condition m_outerCondition;
67 OuterThread * m_pOuterThread;
69 explicit AffineBridge(void);
70 virtual ~AffineBridge(void);
72 virtual void v_callInto_v(uno_EnvCallee * pCallee, va_list * pParam);
73 virtual void v_callOut_v (uno_EnvCallee * pCallee, va_list * pParam);
75 virtual void v_enter(void);
76 virtual void v_leave(void);
78 virtual int v_isValid(rtl::OUString * pReason);
80 void innerDispatch(void);
81 void outerDispatch(int loop);
84 class SAL_DLLPRIVATE InnerThread : public osl::Thread
86 virtual void SAL_CALL run(void);
88 AffineBridge * m_pAffineBridge;
90 public:
91 InnerThread(AffineBridge * threadEnvironment)
92 : m_pAffineBridge(threadEnvironment)
94 create();
98 void InnerThread::run(void)
100 m_pAffineBridge->enter();
101 m_pAffineBridge->innerDispatch();
102 m_pAffineBridge->leave();
105 class SAL_DLLPRIVATE OuterThread : public osl::Thread
107 virtual void SAL_CALL run(void);
109 AffineBridge * m_pAffineBridge;
111 public:
112 OuterThread(AffineBridge * threadEnvironment);
115 OuterThread::OuterThread(AffineBridge * threadEnvironment)
116 : m_pAffineBridge(threadEnvironment)
118 create();
121 void OuterThread::run(void)
123 osl::MutexGuard guard(m_pAffineBridge->m_outerMutex);
125 m_pAffineBridge->m_outerThreadId = getIdentifier();
126 m_pAffineBridge->outerDispatch(0);
127 m_pAffineBridge->m_outerThreadId = 0;
129 m_pAffineBridge->m_pOuterThread = NULL;
130 m_pAffineBridge = NULL;
134 AffineBridge::AffineBridge(void)
135 : m_innerThreadId(0),
136 m_pInnerThread (NULL),
137 m_enterCount (0),
138 m_outerThreadId(0),
139 m_pOuterThread (NULL)
141 LOG_LIFECYCLE_AffineBridge_emit(fprintf(stderr, "LIFE: %s -> %p\n", "AffineBridge::AffineBridge(uno_Environment * pEnv)", this));
144 AffineBridge::~AffineBridge(void)
146 LOG_LIFECYCLE_AffineBridge_emit(fprintf(stderr, "LIFE: %s -> %p\n", "AffineBridge::~AffineBridge(void)", this));
148 if (m_pInnerThread && osl_getThreadIdentifier(NULL) != m_innerThreadId)
150 m_message = CB_DONE;
151 m_innerCondition.set();
153 m_pInnerThread->join();
156 delete m_pInnerThread;
158 if (m_pOuterThread)
160 m_pOuterThread->join();
161 delete m_pOuterThread;
166 void AffineBridge::outerDispatch(int loop)
168 OSL_ASSERT(m_outerThreadId == osl_getThreadIdentifier(NULL));
169 OSL_ASSERT(m_innerThreadId != m_outerThreadId);
171 Msg mm;
175 // FIXME: created outer thread must not wait
176 // in case of no message
177 // note: no message can happen in case newly created
178 // outer thread acquire outerMutex after a real outer
179 // thread enters outerDispatch!
180 m_outerCondition.wait();
181 m_outerCondition.reset();
183 mm = m_message;
185 switch(mm)
187 case CB_DONE:
188 break;
190 case CB_FPOINTER:
192 m_pCallee(m_pParam);
194 m_message = CB_DONE;
195 m_innerCondition.set();
196 break;
198 default:
199 abort();
202 while(mm != CB_DONE && loop);
205 void AffineBridge::innerDispatch(void)
207 OSL_ASSERT(m_innerThreadId == osl_getThreadIdentifier(NULL));
208 OSL_ASSERT(m_innerThreadId != m_outerThreadId);
210 Msg mm;
214 m_innerCondition.wait();
215 m_innerCondition.reset();
217 mm = m_message;
219 switch(mm)
221 case CB_DONE:
222 break;
224 case CB_FPOINTER:
226 m_pCallee(m_pParam);
228 m_message = CB_DONE;
229 m_outerCondition.set();
230 break;
232 default:
233 abort();
236 while(mm != CB_DONE);
239 void AffineBridge::v_callInto_v(uno_EnvCallee * pCallee, va_list * pParam)
241 osl::MutexGuard guard(m_outerMutex); // only one thread at a time can call into
243 if (m_innerThreadId == 0) // no inner thread yet
245 m_pInnerThread = new InnerThread(this);
246 m_pInnerThread->resume();
249 bool resetId = false;
250 if (!m_outerThreadId)
252 m_outerThreadId = osl_getThreadIdentifier(NULL);
253 resetId = true;
256 m_message = CB_FPOINTER;
257 m_pCallee = pCallee;
258 m_pParam = pParam;
259 m_innerCondition.set();
261 outerDispatch(1);
263 if (resetId)
264 m_outerThreadId = 0;
267 void AffineBridge::v_callOut_v(uno_EnvCallee * pCallee, va_list * pParam)
269 OSL_ASSERT(m_innerThreadId);
271 osl::MutexGuard guard(m_innerMutex);
273 if (m_outerThreadId == 0) // no outer thread yet
275 osl::MutexGuard guard_m_outerMutex(m_outerMutex);
277 if (m_outerThreadId == 0)
279 if (m_pOuterThread)
281 m_pOuterThread->join();
282 delete m_pOuterThread;
285 m_pOuterThread = new OuterThread(this);
289 m_message = CB_FPOINTER;
290 m_pCallee = pCallee;
291 m_pParam = pParam;
292 m_outerCondition.set();
294 innerDispatch();
297 void AffineBridge::v_enter(void)
299 m_innerMutex.acquire();
301 if (!m_enterCount)
302 m_innerThreadId = osl_getThreadIdentifier(NULL);
304 OSL_ASSERT(m_innerThreadId == osl_getThreadIdentifier(NULL));
306 ++ m_enterCount;
309 void AffineBridge::v_leave(void)
311 OSL_ASSERT(m_innerThreadId == osl_getThreadIdentifier(NULL));
313 -- m_enterCount;
314 if (!m_enterCount)
315 m_innerThreadId = 0;
317 m_innerMutex.release();
320 int AffineBridge::v_isValid(rtl::OUString * pReason)
322 int result = 1;
324 result = m_enterCount > 0;
325 if (!result)
326 *pReason = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("not entered"));
328 else
330 result = m_innerThreadId == osl_getThreadIdentifier(NULL);
332 if (!result)
333 *pReason = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("wrong thread"));
336 if (result)
337 *pReason = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("OK"));
339 return result;
342 #ifdef DISABLE_DYNLOADING
344 #define uno_initEnvironment affine_uno_uno_initEnvironment
345 #define uno_ext_getMapping affine_uno_uno_ext_getMapping
347 #endif
349 extern "C" void SAL_DLLPUBLIC_EXPORT SAL_CALL uno_initEnvironment(uno_Environment * pEnv)
350 SAL_THROW_EXTERN_C()
352 cppu::helper::purpenv::Environment_initWithEnterable(pEnv, new AffineBridge());
355 extern "C" void SAL_DLLPUBLIC_EXPORT SAL_CALL uno_ext_getMapping(uno_Mapping ** ppMapping,
356 uno_Environment * pFrom,
357 uno_Environment * pTo )
359 cppu::helper::purpenv::createMapping(ppMapping, pFrom, pTo);
362 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */