tdf#154285 Check upper bound of arguments in SbRtl_Minute function
[LibreOffice.git] / cppu / source / AffineBridge / AffineBridge.cxx
blob147c9278bb40078b6de3cb1f87822f01fff23cc9
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));
250 bool bResetId = false;
251 if (!m_outerThreadId)
253 m_outerThreadId = osl::Thread::getCurrentIdentifier();
254 bResetId = true;
257 m_message = CB_FPOINTER;
258 m_pCallee = pCallee;
259 m_pParam = pParam;
260 m_innerCondition.set();
262 outerDispatch(true);
264 if (bResetId)
265 m_outerThreadId = 0;
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)
280 if (m_pOuterThread)
282 m_pOuterThread->join();
285 m_pOuterThread.reset(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()
299 m_innerMutex.acquire();
301 if (!m_enterCount)
302 m_innerThreadId = osl::Thread::getCurrentIdentifier();
304 OSL_ASSERT(m_innerThreadId == osl::Thread::getCurrentIdentifier());
306 ++ m_enterCount;
309 void AffineBridge::v_leave()
311 OSL_ASSERT(m_innerThreadId == osl::Thread::getCurrentIdentifier());
313 -- m_enterCount;
314 if (!m_enterCount)
315 m_innerThreadId = 0;
317 m_innerMutex.release();
320 bool AffineBridge::v_isValid(OUString * pReason)
322 bool result = m_enterCount > 0;
323 if (!result)
324 *pReason = "not entered";
326 else
328 result = m_innerThreadId == osl::Thread::getCurrentIdentifier();
330 if (!result)
331 *pReason = "wrong thread";
334 if (result)
335 *pReason = "OK";
337 return result;
340 #ifdef DISABLE_DYNLOADING
342 #define uno_initEnvironment affine_uno_uno_initEnvironment
343 #define uno_ext_getMapping affine_uno_uno_ext_getMapping
345 #endif
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: */