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>
26 #include <cppu/Enterable.hxx>
27 #include "cppu/helper/purpenv/Environment.hxx"
28 #include "cppu/helper/purpenv/Mapping.hxx"
32 # define LOG_LIFECYCLE_AffineBridge
35 #ifdef LOG_LIFECYCLE_AffineBridge
37 # define LOG_LIFECYCLE_AffineBridge_emit(x) x
40 # define LOG_LIFECYCLE_AffineBridge_emit(x)
47 class AffineBridge
: public cppu::Enterable
57 uno_EnvCallee
* m_pCallee
;
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
;
83 void outerDispatch(int loop
);
86 class InnerThread
: public osl::Thread
88 virtual void SAL_CALL
run() SAL_OVERRIDE
;
90 AffineBridge
* m_pAffineBridge
;
93 InnerThread(AffineBridge
* threadEnvironment
)
94 : m_pAffineBridge(threadEnvironment
)
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
;
116 OuterThread(AffineBridge
* threadEnvironment
);
119 OuterThread::OuterThread(AffineBridge
* threadEnvironment
)
120 : m_pAffineBridge(threadEnvironment
)
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
),
145 m_pInnerThread (NULL
),
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
)
160 m_innerCondition
.set();
162 m_pInnerThread
->join();
165 delete m_pInnerThread
;
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
);
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();
204 m_innerCondition
.set();
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
);
223 m_innerCondition
.wait();
224 m_innerCondition
.reset();
238 m_outerCondition
.set();
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();
265 m_message
= CB_FPOINTER
;
268 m_innerCondition
.set();
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)
290 m_pOuterThread
->join();
291 delete m_pOuterThread
;
294 m_pOuterThread
= new OuterThread(this);
298 m_message
= CB_FPOINTER
;
301 m_outerCondition
.set();
306 void AffineBridge::v_enter()
308 m_innerMutex
.acquire();
311 m_innerThreadId
= osl::Thread::getCurrentIdentifier();
313 OSL_ASSERT(m_innerThreadId
== osl::Thread::getCurrentIdentifier());
318 void AffineBridge::v_leave()
320 OSL_ASSERT(m_innerThreadId
== osl::Thread::getCurrentIdentifier());
326 m_innerMutex
.release();
329 bool AffineBridge::v_isValid(rtl::OUString
* pReason
)
331 bool result
= m_enterCount
> 0;
333 *pReason
= "not entered";
337 result
= m_innerThreadId
== osl::Thread::getCurrentIdentifier();
340 *pReason
= "wrong thread";
349 #ifdef DISABLE_DYNLOADING
351 #define uno_initEnvironment affine_uno_uno_initEnvironment
352 #define uno_ext_getMapping affine_uno_uno_ext_getMapping
356 extern "C" void SAL_DLLPUBLIC_EXPORT SAL_CALL
uno_initEnvironment(uno_Environment
* pEnv
)
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: */