1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
24 #include <osl/diagnose.h>
25 #include <sal/log.hxx>
27 #include <cppu/Enterable.hxx>
28 #include <cppu/helper/purpenv/Environment.hxx>
29 #include <cppu/helper/purpenv/Mapping.hxx>
37 class AffineBridge
: public cppu::Enterable
47 uno_EnvCallee
* m_pCallee
;
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
;
56 osl::Mutex m_outerMutex
;
57 oslThreadIdentifier m_outerThreadId
;
58 osl::Condition m_outerCondition
;
59 std::unique_ptr
<OuterThread
> m_pOuterThread
;
61 explicit AffineBridge();
62 virtual ~AffineBridge() override
;
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
;
67 virtual void v_enter() override
;
68 virtual void v_leave() override
;
70 virtual bool v_isValid(OUString
* pReason
) override
;
73 void outerDispatch(bool loop
);
76 class InnerThread
: public osl::Thread
78 virtual void SAL_CALL
run() override
;
80 AffineBridge
* m_pAffineBridge
;
83 explicit InnerThread(AffineBridge
* threadEnvironment
)
84 : m_pAffineBridge(threadEnvironment
)
92 void InnerThread::run()
94 osl_setThreadName("UNO AffineBridge InnerThread");
96 m_pAffineBridge
->enter();
97 m_pAffineBridge
->innerDispatch();
98 m_pAffineBridge
->leave();
103 class OuterThread
: public osl::Thread
105 virtual void SAL_CALL
run() override
;
107 AffineBridge
* m_pAffineBridge
;
110 explicit OuterThread(AffineBridge
* threadEnvironment
);
115 OuterThread::OuterThread(AffineBridge
* threadEnvironment
)
116 : m_pAffineBridge(threadEnvironment
)
121 void OuterThread::run()
123 osl_setThreadName("UNO AffineBridge OuterThread");
125 osl::MutexGuard
guard(m_pAffineBridge
->m_outerMutex
);
127 m_pAffineBridge
->m_outerThreadId
= getIdentifier();
128 m_pAffineBridge
->outerDispatch(false);
129 m_pAffineBridge
->m_outerThreadId
= 0;
131 m_pAffineBridge
->m_pOuterThread
= nullptr;
132 m_pAffineBridge
= nullptr;
136 AffineBridge::AffineBridge()
137 : m_message (CB_DONE
),
144 SAL_INFO("cppu.affinebridge", "LIFE: AffineBridge::AffineBridge(uno_Environment * pEnv) -> " << this);
147 AffineBridge::~AffineBridge()
149 SAL_INFO("cppu.affinebridge", "LIFE: AffineBridge::~AffineBridge() -> " << this);
151 if (m_pInnerThread
&& osl::Thread::getCurrentIdentifier() != m_innerThreadId
)
154 m_innerCondition
.set();
156 m_pInnerThread
->join();
159 m_pInnerThread
.reset();
163 m_pOuterThread
->join();
168 void AffineBridge::outerDispatch(bool loop
)
170 OSL_ASSERT(m_outerThreadId
== osl::Thread::getCurrentIdentifier());
171 OSL_ASSERT(m_innerThreadId
!= m_outerThreadId
);
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();
197 m_innerCondition
.set();
204 while(mm
!= CB_DONE
&& loop
);
207 void AffineBridge::innerDispatch()
209 OSL_ASSERT(m_innerThreadId
== osl::Thread::getCurrentIdentifier());
210 OSL_ASSERT(m_innerThreadId
!= m_outerThreadId
);
216 m_innerCondition
.wait();
217 m_innerCondition
.reset();
231 m_outerCondition
.set();
238 while(mm
!= CB_DONE
);
241 void AffineBridge::v_callInto_v(uno_EnvCallee
* pCallee
, va_list * pParam
)
243 osl::MutexGuard
guard(m_outerMutex
); // only one thread at a time can call into
245 if (m_innerThreadId
== 0) // no inner thread yet
247 m_pInnerThread
.reset(new InnerThread(this));
250 bool bResetId
= false;
251 if (!m_outerThreadId
)
253 m_outerThreadId
= osl::Thread::getCurrentIdentifier();
257 m_message
= CB_FPOINTER
;
260 m_innerCondition
.set();
268 void AffineBridge::v_callOut_v(uno_EnvCallee
* pCallee
, va_list * pParam
)
270 OSL_ASSERT(m_innerThreadId
);
272 osl::MutexGuard
guard(m_innerMutex
);
274 if (m_outerThreadId
== 0) // no outer thread yet
276 osl::MutexGuard
guard_m_outerMutex(m_outerMutex
);
278 if (m_outerThreadId
== 0)
282 m_pOuterThread
->join();
285 m_pOuterThread
.reset(new OuterThread(this));
289 m_message
= CB_FPOINTER
;
292 m_outerCondition
.set();
297 void AffineBridge::v_enter()
299 m_innerMutex
.acquire();
302 m_innerThreadId
= osl::Thread::getCurrentIdentifier();
304 OSL_ASSERT(m_innerThreadId
== osl::Thread::getCurrentIdentifier());
309 void AffineBridge::v_leave()
311 OSL_ASSERT(m_innerThreadId
== osl::Thread::getCurrentIdentifier());
317 m_innerMutex
.release();
320 bool AffineBridge::v_isValid(OUString
* pReason
)
322 bool result
= m_enterCount
> 0;
324 *pReason
= "not entered";
328 result
= m_innerThreadId
== osl::Thread::getCurrentIdentifier();
331 *pReason
= "wrong thread";
340 #ifdef DISABLE_DYNLOADING
342 #define uno_initEnvironment affine_uno_uno_initEnvironment
343 #define uno_ext_getMapping affine_uno_uno_ext_getMapping
347 extern "C" void SAL_DLLPUBLIC_EXPORT
uno_initEnvironment(uno_Environment
* pEnv
) noexcept
349 cppu::helper::purpenv::Environment_initWithEnterable(pEnv
, new AffineBridge());
352 extern "C" void SAL_DLLPUBLIC_EXPORT
uno_ext_getMapping(uno_Mapping
** ppMapping
,
353 uno_Environment
* pFrom
,
354 uno_Environment
* pTo
)
356 cppu::helper::purpenv::createMapping(ppMapping
, pFrom
, pTo
);
359 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */