fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / cppu / source / AffineBridge / AffineBridge.cxx
blob2184a59461a1e4406224142e0b752a03ef1f74c2
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>
26 #include <cppu/Enterable.hxx>
27 #include "cppu/helper/purpenv/Environment.hxx"
28 #include "cppu/helper/purpenv/Mapping.hxx"
31 #ifdef debug
32 # define LOG_LIFECYCLE_AffineBridge
33 #endif
35 #ifdef LOG_LIFECYCLE_AffineBridge
36 # include <iostream>
37 # define LOG_LIFECYCLE_AffineBridge_emit(x) x
39 #else
40 # define LOG_LIFECYCLE_AffineBridge_emit(x)
42 #endif
44 class InnerThread;
45 class OuterThread;
47 class AffineBridge : public cppu::Enterable
49 public:
50 enum Msg
52 CB_DONE,
53 CB_FPOINTER
56 Msg m_message;
57 uno_EnvCallee * m_pCallee;
58 va_list * m_pParam;
60 osl::Mutex m_innerMutex;
61 oslThreadIdentifier m_innerThreadId;
62 InnerThread * m_pInnerThread;
63 osl::Condition m_innerCondition;
64 sal_Int32 m_enterCount;
66 osl::Mutex m_outerMutex;
67 oslThreadIdentifier m_outerThreadId;
68 osl::Condition m_outerCondition;
69 OuterThread * m_pOuterThread;
71 explicit AffineBridge();
72 virtual ~AffineBridge();
74 virtual void v_callInto_v(uno_EnvCallee * pCallee, va_list * pParam) SAL_OVERRIDE;
75 virtual void v_callOut_v (uno_EnvCallee * pCallee, va_list * pParam) SAL_OVERRIDE;
77 virtual void v_enter() SAL_OVERRIDE;
78 virtual void v_leave() SAL_OVERRIDE;
80 virtual bool v_isValid(rtl::OUString * pReason) SAL_OVERRIDE;
82 void innerDispatch();
83 void outerDispatch(int loop);
86 class InnerThread : public osl::Thread
88 virtual void SAL_CALL run() SAL_OVERRIDE;
90 AffineBridge * m_pAffineBridge;
92 public:
93 InnerThread(AffineBridge * threadEnvironment)
94 : m_pAffineBridge(threadEnvironment)
96 create();
100 void InnerThread::run()
102 osl_setThreadName("UNO AffineBridge InnerThread");
104 m_pAffineBridge->enter();
105 m_pAffineBridge->innerDispatch();
106 m_pAffineBridge->leave();
109 class OuterThread : public osl::Thread
111 virtual void SAL_CALL run() SAL_OVERRIDE;
113 AffineBridge * m_pAffineBridge;
115 public:
116 OuterThread(AffineBridge * threadEnvironment);
119 OuterThread::OuterThread(AffineBridge * threadEnvironment)
120 : m_pAffineBridge(threadEnvironment)
122 create();
125 void OuterThread::run()
127 osl_setThreadName("UNO AffineBridge OuterThread");
129 osl::MutexGuard guard(m_pAffineBridge->m_outerMutex);
131 m_pAffineBridge->m_outerThreadId = getIdentifier();
132 m_pAffineBridge->outerDispatch(0);
133 m_pAffineBridge->m_outerThreadId = 0;
135 m_pAffineBridge->m_pOuterThread = NULL;
136 m_pAffineBridge = NULL;
140 AffineBridge::AffineBridge()
141 : m_message (CB_DONE),
142 m_pCallee (0),
143 m_pParam (0),
144 m_innerThreadId(0),
145 m_pInnerThread (NULL),
146 m_enterCount (0),
147 m_outerThreadId(0),
148 m_pOuterThread (NULL)
150 LOG_LIFECYCLE_AffineBridge_emit(fprintf(stderr, "LIFE: %s -> %p\n", "AffineBridge::AffineBridge(uno_Environment * pEnv)", this));
153 AffineBridge::~AffineBridge()
155 LOG_LIFECYCLE_AffineBridge_emit(fprintf(stderr, "LIFE: %s -> %p\n", "AffineBridge::~AffineBridge()", this));
157 if (m_pInnerThread && osl::Thread::getCurrentIdentifier() != m_innerThreadId)
159 m_message = CB_DONE;
160 m_innerCondition.set();
162 m_pInnerThread->join();
165 delete m_pInnerThread;
167 if (m_pOuterThread)
169 m_pOuterThread->join();
170 delete m_pOuterThread;
175 void AffineBridge::outerDispatch(int loop)
177 OSL_ASSERT(m_outerThreadId == osl::Thread::getCurrentIdentifier());
178 OSL_ASSERT(m_innerThreadId != m_outerThreadId);
180 Msg mm;
184 // FIXME: created outer thread must not wait
185 // in case of no message
186 // note: no message can happen in case newly created
187 // outer thread acquire outerMutex after a real outer
188 // thread enters outerDispatch!
189 m_outerCondition.wait();
190 m_outerCondition.reset();
192 mm = m_message;
194 switch(mm)
196 case CB_DONE:
197 break;
199 case CB_FPOINTER:
201 m_pCallee(m_pParam);
203 m_message = CB_DONE;
204 m_innerCondition.set();
205 break;
207 default:
208 abort();
211 while(mm != CB_DONE && loop);
214 void AffineBridge::innerDispatch()
216 OSL_ASSERT(m_innerThreadId == osl::Thread::getCurrentIdentifier());
217 OSL_ASSERT(m_innerThreadId != m_outerThreadId);
219 Msg mm;
223 m_innerCondition.wait();
224 m_innerCondition.reset();
226 mm = m_message;
228 switch(mm)
230 case CB_DONE:
231 break;
233 case CB_FPOINTER:
235 m_pCallee(m_pParam);
237 m_message = CB_DONE;
238 m_outerCondition.set();
239 break;
241 default:
242 abort();
245 while(mm != CB_DONE);
248 void AffineBridge::v_callInto_v(uno_EnvCallee * pCallee, va_list * pParam)
250 osl::MutexGuard guard(m_outerMutex); // only one thread at a time can call into
252 if (m_innerThreadId == 0) // no inner thread yet
254 m_pInnerThread = new InnerThread(this);
255 m_pInnerThread->resume();
258 bool resetId = false;
259 if (!m_outerThreadId)
261 m_outerThreadId = osl::Thread::getCurrentIdentifier();
262 resetId = true;
265 m_message = CB_FPOINTER;
266 m_pCallee = pCallee;
267 m_pParam = pParam;
268 m_innerCondition.set();
270 outerDispatch(1);
272 if (resetId)
273 m_outerThreadId = 0;
276 void AffineBridge::v_callOut_v(uno_EnvCallee * pCallee, va_list * pParam)
278 OSL_ASSERT(m_innerThreadId);
280 osl::MutexGuard guard(m_innerMutex);
282 if (m_outerThreadId == 0) // no outer thread yet
284 osl::MutexGuard guard_m_outerMutex(m_outerMutex);
286 if (m_outerThreadId == 0)
288 if (m_pOuterThread)
290 m_pOuterThread->join();
291 delete m_pOuterThread;
294 m_pOuterThread = new OuterThread(this);
298 m_message = CB_FPOINTER;
299 m_pCallee = pCallee;
300 m_pParam = pParam;
301 m_outerCondition.set();
303 innerDispatch();
306 void AffineBridge::v_enter()
308 m_innerMutex.acquire();
310 if (!m_enterCount)
311 m_innerThreadId = osl::Thread::getCurrentIdentifier();
313 OSL_ASSERT(m_innerThreadId == osl::Thread::getCurrentIdentifier());
315 ++ m_enterCount;
318 void AffineBridge::v_leave()
320 OSL_ASSERT(m_innerThreadId == osl::Thread::getCurrentIdentifier());
322 -- m_enterCount;
323 if (!m_enterCount)
324 m_innerThreadId = 0;
326 m_innerMutex.release();
329 bool AffineBridge::v_isValid(rtl::OUString * pReason)
331 bool result = m_enterCount > 0;
332 if (!result)
333 *pReason = "not entered";
335 else
337 result = m_innerThreadId == osl::Thread::getCurrentIdentifier();
339 if (!result)
340 *pReason = "wrong thread";
343 if (result)
344 *pReason = "OK";
346 return result;
349 #ifdef DISABLE_DYNLOADING
351 #define uno_initEnvironment affine_uno_uno_initEnvironment
352 #define uno_ext_getMapping affine_uno_uno_ext_getMapping
354 #endif
356 extern "C" void SAL_DLLPUBLIC_EXPORT SAL_CALL uno_initEnvironment(uno_Environment * pEnv)
357 SAL_THROW_EXTERN_C()
359 cppu::helper::purpenv::Environment_initWithEnterable(pEnv, new AffineBridge());
362 extern "C" void SAL_DLLPUBLIC_EXPORT SAL_CALL uno_ext_getMapping(uno_Mapping ** ppMapping,
363 uno_Environment * pFrom,
364 uno_Environment * pTo )
366 cppu::helper::purpenv::createMapping(ppMapping, pFrom, pTo);
369 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */