nss: upgrade to release 3.73
[LibreOffice.git] / cppu / source / AffineBridge / AffineBridge.cxx
blob156d6eb9c7a0316ccffcc2b87153d9f8c5ce75a7
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>
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>
30 #include <memory>
32 namespace {
34 class InnerThread;
35 class OuterThread;
37 class AffineBridge : public cppu::Enterable
39 public:
40 enum Msg
42 CB_DONE,
43 CB_FPOINTER
46 Msg m_message;
47 uno_EnvCallee * m_pCallee;
48 va_list * m_pParam;
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;
72 void innerDispatch();
73 void outerDispatch(bool loop);
76 class InnerThread : public osl::Thread
78 virtual void SAL_CALL run() override;
80 AffineBridge * m_pAffineBridge;
82 public:
83 explicit InnerThread(AffineBridge * threadEnvironment)
84 : m_pAffineBridge(threadEnvironment)
86 create();
92 void InnerThread::run()
94 osl_setThreadName("UNO AffineBridge InnerThread");
96 m_pAffineBridge->enter();
97 m_pAffineBridge->innerDispatch();
98 m_pAffineBridge->leave();
101 namespace {
103 class OuterThread : public osl::Thread
105 virtual void SAL_CALL run() override;
107 AffineBridge * m_pAffineBridge;
109 public:
110 explicit OuterThread(AffineBridge * threadEnvironment);
115 OuterThread::OuterThread(AffineBridge * threadEnvironment)
116 : m_pAffineBridge(threadEnvironment)
118 create();
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),
138 m_pCallee (nullptr),
139 m_pParam (nullptr),
140 m_innerThreadId(0),
141 m_enterCount (0),
142 m_outerThreadId(0)
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)
153 m_message = CB_DONE;
154 m_innerCondition.set();
156 m_pInnerThread->join();
159 m_pInnerThread.reset();
161 if (m_pOuterThread)
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);
173 Msg mm;
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();
185 mm = m_message;
187 switch(mm)
189 case CB_DONE:
190 break;
192 case CB_FPOINTER:
194 m_pCallee(m_pParam);
196 m_message = CB_DONE;
197 m_innerCondition.set();
198 break;
200 default:
201 abort();
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);
212 Msg mm;
216 m_innerCondition.wait();
217 m_innerCondition.reset();
219 mm = m_message;
221 switch(mm)
223 case CB_DONE:
224 break;
226 case CB_FPOINTER:
228 m_pCallee(m_pParam);
230 m_message = CB_DONE;
231 m_outerCondition.set();
232 break;
234 default:
235 abort();
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));
248 m_pInnerThread->resume();
251 bool bResetId = false;
252 if (!m_outerThreadId)
254 m_outerThreadId = osl::Thread::getCurrentIdentifier();
255 bResetId = true;
258 m_message = CB_FPOINTER;
259 m_pCallee = pCallee;
260 m_pParam = pParam;
261 m_innerCondition.set();
263 outerDispatch(true);
265 if (bResetId)
266 m_outerThreadId = 0;
269 void AffineBridge::v_callOut_v(uno_EnvCallee * pCallee, va_list * pParam)
271 OSL_ASSERT(m_innerThreadId);
273 osl::MutexGuard guard(m_innerMutex);
275 if (m_outerThreadId == 0) // no outer thread yet
277 osl::MutexGuard guard_m_outerMutex(m_outerMutex);
279 if (m_outerThreadId == 0)
281 if (m_pOuterThread)
283 m_pOuterThread->join();
286 m_pOuterThread.reset(new OuterThread(this));
290 m_message = CB_FPOINTER;
291 m_pCallee = pCallee;
292 m_pParam = pParam;
293 m_outerCondition.set();
295 innerDispatch();
298 void AffineBridge::v_enter()
300 m_innerMutex.acquire();
302 if (!m_enterCount)
303 m_innerThreadId = osl::Thread::getCurrentIdentifier();
305 OSL_ASSERT(m_innerThreadId == osl::Thread::getCurrentIdentifier());
307 ++ m_enterCount;
310 void AffineBridge::v_leave()
312 OSL_ASSERT(m_innerThreadId == osl::Thread::getCurrentIdentifier());
314 -- m_enterCount;
315 if (!m_enterCount)
316 m_innerThreadId = 0;
318 m_innerMutex.release();
321 bool AffineBridge::v_isValid(OUString * pReason)
323 bool result = m_enterCount > 0;
324 if (!result)
325 *pReason = "not entered";
327 else
329 result = m_innerThreadId == osl::Thread::getCurrentIdentifier();
331 if (!result)
332 *pReason = "wrong thread";
335 if (result)
336 *pReason = "OK";
338 return result;
341 #ifdef DISABLE_DYNLOADING
343 #define uno_initEnvironment affine_uno_uno_initEnvironment
344 #define uno_ext_getMapping affine_uno_uno_ext_getMapping
346 #endif
348 extern "C" void SAL_DLLPUBLIC_EXPORT uno_initEnvironment(uno_Environment * pEnv)
349 SAL_THROW_EXTERN_C()
351 cppu::helper::purpenv::Environment_initWithEnterable(pEnv, new AffineBridge());
354 extern "C" void SAL_DLLPUBLIC_EXPORT uno_ext_getMapping(uno_Mapping ** ppMapping,
355 uno_Environment * pFrom,
356 uno_Environment * pTo )
358 cppu::helper::purpenv::createMapping(ppMapping, pFrom, pTo);
361 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */