LibreOffice Module cppu (master) 1
AffineBridge.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20
21#include <osl/thread.hxx>
22#include <osl/conditn.hxx>
23#include <osl/mutex.hxx>
24#include <osl/diagnose.h>
25#include <sal/log.hxx>
26
27#include <cppu/Enterable.hxx>
30#include <memory>
31
32namespace {
33
34class InnerThread;
35class OuterThread;
36
37class AffineBridge : public cppu::Enterable
38{
39public:
40 enum Msg
41 {
42 CB_DONE,
43 CB_FPOINTER
44 };
45
46 Msg m_message;
47 uno_EnvCallee * m_pCallee;
48 va_list * m_pParam;
49
50 osl::Mutex m_innerMutex;
51 oslThreadIdentifier m_innerThreadId;
52 std::unique_ptr<InnerThread> m_pInnerThread;
53 osl::Condition m_innerCondition;
54 sal_Int32 m_enterCount;
55
56 osl::Mutex m_outerMutex;
57 oslThreadIdentifier m_outerThreadId;
58 osl::Condition m_outerCondition;
59 std::unique_ptr<OuterThread> m_pOuterThread;
60
61 explicit AffineBridge();
62 virtual ~AffineBridge() override;
63
64 virtual void v_callInto_v(uno_EnvCallee * pCallee, va_list * pParam) override;
65 virtual void v_callOut_v (uno_EnvCallee * pCallee, va_list * pParam) override;
66
67 virtual void v_enter() override;
68 virtual void v_leave() override;
69
70 virtual bool v_isValid(OUString * pReason) override;
71
72 void innerDispatch();
73 void outerDispatch(bool loop);
74};
75
76class InnerThread : public osl::Thread
77{
78 virtual void SAL_CALL run() override;
79
80 AffineBridge * m_pAffineBridge;
81
82public:
83 explicit InnerThread(AffineBridge * threadEnvironment)
84 : m_pAffineBridge(threadEnvironment)
85 {
86 create();
87 }
88};
89
90}
91
92void InnerThread::run()
93{
94 osl_setThreadName("UNO AffineBridge InnerThread");
95
96 m_pAffineBridge->enter();
97 m_pAffineBridge->innerDispatch();
98 m_pAffineBridge->leave();
99}
100
101namespace {
102
103class OuterThread : public osl::Thread
104{
105 virtual void SAL_CALL run() override;
106
107 AffineBridge * m_pAffineBridge;
108
109public:
110 explicit OuterThread(AffineBridge * threadEnvironment);
111};
112
113}
114
115OuterThread::OuterThread(AffineBridge * threadEnvironment)
116 : m_pAffineBridge(threadEnvironment)
117{
118 create();
119}
120
121void OuterThread::run()
122{
123 osl_setThreadName("UNO AffineBridge OuterThread");
124
125 osl::MutexGuard guard(m_pAffineBridge->m_outerMutex);
126
127 m_pAffineBridge->m_outerThreadId = getIdentifier();
128 m_pAffineBridge->outerDispatch(false);
129 m_pAffineBridge->m_outerThreadId = 0;
130
131 m_pAffineBridge->m_pOuterThread = nullptr;
132 m_pAffineBridge = nullptr;
133}
134
135
136AffineBridge::AffineBridge()
137 : m_message (CB_DONE),
138 m_pCallee (nullptr),
139 m_pParam (nullptr),
140 m_innerThreadId(0),
141 m_enterCount (0),
142 m_outerThreadId(0)
143{
144 SAL_INFO("cppu.affinebridge", "LIFE: AffineBridge::AffineBridge(uno_Environment * pEnv) -> " << this);
145}
146
147AffineBridge::~AffineBridge()
148{
149 SAL_INFO("cppu.affinebridge", "LIFE: AffineBridge::~AffineBridge() -> " << this);
150
151 if (m_pInnerThread && osl::Thread::getCurrentIdentifier() != m_innerThreadId)
152 {
153 m_message = CB_DONE;
154 m_innerCondition.set();
155
156 m_pInnerThread->join();
157 }
158
159 m_pInnerThread.reset();
160
161 if (m_pOuterThread)
162 {
163 m_pOuterThread->join();
164 }
165}
166
167
168void AffineBridge::outerDispatch(bool loop)
169{
170 OSL_ASSERT(m_outerThreadId == osl::Thread::getCurrentIdentifier());
171 OSL_ASSERT(m_innerThreadId != m_outerThreadId);
172
173 Msg mm;
174
175 do
176 {
177 // FIXME: created outer thread must not wait
178 // in case of no message
179 // note: no message can happen in case newly created
180 // outer thread acquire outerMutex after a real outer
181 // thread enters outerDispatch!
182 m_outerCondition.wait();
183 m_outerCondition.reset();
184
185 mm = m_message;
186
187 switch(mm)
188 {
189 case CB_DONE:
190 break;
191
192 case CB_FPOINTER:
193 {
194 m_pCallee(m_pParam);
195
196 m_message = CB_DONE;
197 m_innerCondition.set();
198 break;
199 }
200 default:
201 abort();
202 }
203 }
204 while(mm != CB_DONE && loop);
205}
206
207void AffineBridge::innerDispatch()
208{
209 OSL_ASSERT(m_innerThreadId == osl::Thread::getCurrentIdentifier());
210 OSL_ASSERT(m_innerThreadId != m_outerThreadId);
211
212 Msg mm;
213
214 do
215 {
216 m_innerCondition.wait();
217 m_innerCondition.reset();
218
219 mm = m_message;
220
221 switch(mm)
222 {
223 case CB_DONE:
224 break;
225
226 case CB_FPOINTER:
227 {
228 m_pCallee(m_pParam);
229
230 m_message = CB_DONE;
231 m_outerCondition.set();
232 break;
233 }
234 default:
235 abort();
236 }
237 }
238 while(mm != CB_DONE);
239}
240
241void AffineBridge::v_callInto_v(uno_EnvCallee * pCallee, va_list * pParam)
242{
243 osl::MutexGuard guard(m_outerMutex); // only one thread at a time can call into
244
245 if (m_innerThreadId == 0) // no inner thread yet
246 {
247 m_pInnerThread.reset(new InnerThread(this));
248 }
249
250 bool bResetId = false;
251 if (!m_outerThreadId)
252 {
253 m_outerThreadId = osl::Thread::getCurrentIdentifier();
254 bResetId = true;
255 }
256
257 m_message = CB_FPOINTER;
258 m_pCallee = pCallee;
259 m_pParam = pParam;
260 m_innerCondition.set();
261
262 outerDispatch(true);
263
264 if (bResetId)
265 m_outerThreadId = 0;
266}
267
268void AffineBridge::v_callOut_v(uno_EnvCallee * pCallee, va_list * pParam)
269{
270 OSL_ASSERT(m_innerThreadId);
271
272 osl::MutexGuard guard(m_innerMutex);
273
274 if (m_outerThreadId == 0) // no outer thread yet
275 {
276 osl::MutexGuard guard_m_outerMutex(m_outerMutex);
277
278 if (m_outerThreadId == 0)
279 {
280 if (m_pOuterThread)
281 {
282 m_pOuterThread->join();
283 }
284
285 m_pOuterThread.reset(new OuterThread(this));
286 }
287 }
288
289 m_message = CB_FPOINTER;
290 m_pCallee = pCallee;
291 m_pParam = pParam;
292 m_outerCondition.set();
293
294 innerDispatch();
295}
296
297void AffineBridge::v_enter()
298{
299 m_innerMutex.acquire();
300
301 if (!m_enterCount)
302 m_innerThreadId = osl::Thread::getCurrentIdentifier();
303
304 OSL_ASSERT(m_innerThreadId == osl::Thread::getCurrentIdentifier());
305
306 ++ m_enterCount;
307}
308
309void AffineBridge::v_leave()
310{
311 OSL_ASSERT(m_innerThreadId == osl::Thread::getCurrentIdentifier());
312
313 -- m_enterCount;
314 if (!m_enterCount)
315 m_innerThreadId = 0;
316
317 m_innerMutex.release();
318}
319
320bool AffineBridge::v_isValid(OUString * pReason)
321{
322 bool result = m_enterCount > 0;
323 if (!result)
324 *pReason = "not entered";
325
326 else
327 {
328 result = m_innerThreadId == osl::Thread::getCurrentIdentifier();
329
330 if (!result)
331 *pReason = "wrong thread";
332 }
333
334 if (result)
335 *pReason = "OK";
336
337 return result;
338}
339
340#ifdef DISABLE_DYNLOADING
341
342#define uno_initEnvironment affine_uno_uno_initEnvironment
343#define uno_ext_getMapping affine_uno_uno_ext_getMapping
344
345#endif
346
347extern "C" void SAL_DLLPUBLIC_EXPORT uno_initEnvironment(uno_Environment * pEnv)
349{
351}
352
353extern "C" void SAL_DLLPUBLIC_EXPORT uno_ext_getMapping(uno_Mapping ** ppMapping,
354 uno_Environment * pFrom,
355 uno_Environment * pTo )
356{
357 cppu::helper::purpenv::createMapping(ppMapping, pFrom, pTo);
358}
359
360/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void SAL_DLLPUBLIC_EXPORT uno_ext_getMapping(uno_Mapping **ppMapping, uno_Environment *pFrom, uno_Environment *pTo)
void SAL_DLLPUBLIC_EXPORT uno_initEnvironment(uno_Environment *pEnv) SAL_THROW_EXTERN_C()
C++ wrapper for binary C Enterable (http://wiki.openoffice.org/wiki/Uno/Cpp/Spec/Environment_Stack)
Definition: Enterable.hxx:39
virtual void v_enter()=0
virtual bool v_isValid(rtl::OUString *pReason)=0
virtual void v_callOut_v(uno_EnvCallee *pCallee, va_list *pParam)=0
virtual void v_leave()=0
virtual void v_callInto_v(uno_EnvCallee *pCallee, va_list *pParam)=0
struct _uno_Environment uno_Environment
#define SAL_INFO(area, stream)
struct _uno_Mapping uno_Mapping
def run(arg=None, arg2=-1)
void Environment_initWithEnterable(uno_Environment *pEnvironment, cppu::Enterable *pEnterable)
C++ helper for implementing Purpose Environments.
void createMapping(uno_Mapping **ppMapping, uno_Environment *pFrom, uno_Environment *pTo, ProbeFun *probeFun, void *pContext)
DESKTOP_DEPLOYMENTMISC_DLLPUBLIC OUString getIdentifier(css::uno::Reference< css::deployment::XPackage > const &package)
css::uno::Reference< css::deployment::XPackageRegistry > create(css::uno::Reference< css::deployment::XPackageRegistry > const &xRootRegistry, OUString const &context, OUString const &cachePath, css::uno::Reference< css::uno::XComponentContext > const &xComponentContext)
#define SAL_THROW_EXTERN_C()
Any result