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"
25 #include "cppu/helper/purpenv/Environment.hxx"
26 #include "cppu/helper/purpenv/Mapping.hxx"
30 # define LOG_LIFECYCLE_AffineBridge
33 #ifdef LOG_LIFECYCLE_AffineBridge
35 # define LOG_LIFECYCLE_AffineBridge_emit(x) x
38 # define LOG_LIFECYCLE_AffineBridge_emit(x)
45 class SAL_DLLPRIVATE AffineBridge
: public cppu::Enterable
55 uno_EnvCallee
* m_pCallee
;
58 osl::Mutex m_innerMutex
;
59 oslThreadIdentifier m_innerThreadId
;
60 InnerThread
* m_pInnerThread
;
61 osl::Condition m_innerCondition
;
62 sal_Int32 m_enterCount
;
64 osl::Mutex m_outerMutex
;
65 oslThreadIdentifier m_outerThreadId
;
66 osl::Condition m_outerCondition
;
67 OuterThread
* m_pOuterThread
;
69 explicit AffineBridge(void);
70 virtual ~AffineBridge(void);
72 virtual void v_callInto_v(uno_EnvCallee
* pCallee
, va_list * pParam
);
73 virtual void v_callOut_v (uno_EnvCallee
* pCallee
, va_list * pParam
);
75 virtual void v_enter(void);
76 virtual void v_leave(void);
78 virtual int v_isValid(rtl::OUString
* pReason
);
80 void innerDispatch(void);
81 void outerDispatch(int loop
);
84 class SAL_DLLPRIVATE InnerThread
: public osl::Thread
86 virtual void SAL_CALL
run(void);
88 AffineBridge
* m_pAffineBridge
;
91 InnerThread(AffineBridge
* threadEnvironment
)
92 : m_pAffineBridge(threadEnvironment
)
98 void InnerThread::run(void)
100 m_pAffineBridge
->enter();
101 m_pAffineBridge
->innerDispatch();
102 m_pAffineBridge
->leave();
105 class SAL_DLLPRIVATE OuterThread
: public osl::Thread
107 virtual void SAL_CALL
run(void);
109 AffineBridge
* m_pAffineBridge
;
112 OuterThread(AffineBridge
* threadEnvironment
);
115 OuterThread::OuterThread(AffineBridge
* threadEnvironment
)
116 : m_pAffineBridge(threadEnvironment
)
121 void OuterThread::run(void)
123 osl::MutexGuard
guard(m_pAffineBridge
->m_outerMutex
);
125 m_pAffineBridge
->m_outerThreadId
= getIdentifier();
126 m_pAffineBridge
->outerDispatch(0);
127 m_pAffineBridge
->m_outerThreadId
= 0;
129 m_pAffineBridge
->m_pOuterThread
= NULL
;
130 m_pAffineBridge
= NULL
;
134 AffineBridge::AffineBridge(void)
135 : m_innerThreadId(0),
136 m_pInnerThread (NULL
),
139 m_pOuterThread (NULL
)
141 LOG_LIFECYCLE_AffineBridge_emit(fprintf(stderr
, "LIFE: %s -> %p\n", "AffineBridge::AffineBridge(uno_Environment * pEnv)", this));
144 AffineBridge::~AffineBridge(void)
146 LOG_LIFECYCLE_AffineBridge_emit(fprintf(stderr
, "LIFE: %s -> %p\n", "AffineBridge::~AffineBridge(void)", this));
148 if (m_pInnerThread
&& osl_getThreadIdentifier(NULL
) != m_innerThreadId
)
151 m_innerCondition
.set();
153 m_pInnerThread
->join();
156 delete m_pInnerThread
;
160 m_pOuterThread
->join();
161 delete m_pOuterThread
;
166 void AffineBridge::outerDispatch(int loop
)
168 OSL_ASSERT(m_outerThreadId
== osl_getThreadIdentifier(NULL
));
169 OSL_ASSERT(m_innerThreadId
!= m_outerThreadId
);
175 // FIXME: created outer thread must not wait
176 // in case of no message
177 // note: no message can happen in case newly created
178 // outer thread acquire outerMutex after a real outer
179 // thread enters outerDispatch!
180 m_outerCondition
.wait();
181 m_outerCondition
.reset();
195 m_innerCondition
.set();
202 while(mm
!= CB_DONE
&& loop
);
205 void AffineBridge::innerDispatch(void)
207 OSL_ASSERT(m_innerThreadId
== osl_getThreadIdentifier(NULL
));
208 OSL_ASSERT(m_innerThreadId
!= m_outerThreadId
);
214 m_innerCondition
.wait();
215 m_innerCondition
.reset();
229 m_outerCondition
.set();
236 while(mm
!= CB_DONE
);
239 void AffineBridge::v_callInto_v(uno_EnvCallee
* pCallee
, va_list * pParam
)
241 osl::MutexGuard
guard(m_outerMutex
); // only one thread at a time can call into
243 if (m_innerThreadId
== 0) // no inner thread yet
245 m_pInnerThread
= new InnerThread(this);
246 m_pInnerThread
->resume();
249 bool resetId
= false;
250 if (!m_outerThreadId
)
252 m_outerThreadId
= osl_getThreadIdentifier(NULL
);
256 m_message
= CB_FPOINTER
;
259 m_innerCondition
.set();
267 void AffineBridge::v_callOut_v(uno_EnvCallee
* pCallee
, va_list * pParam
)
269 OSL_ASSERT(m_innerThreadId
);
271 osl::MutexGuard
guard(m_innerMutex
);
273 if (m_outerThreadId
== 0) // no outer thread yet
275 osl::MutexGuard
guard_m_outerMutex(m_outerMutex
);
277 if (m_outerThreadId
== 0)
281 m_pOuterThread
->join();
282 delete m_pOuterThread
;
285 m_pOuterThread
= new OuterThread(this);
289 m_message
= CB_FPOINTER
;
292 m_outerCondition
.set();
297 void AffineBridge::v_enter(void)
299 m_innerMutex
.acquire();
302 m_innerThreadId
= osl_getThreadIdentifier(NULL
);
304 OSL_ASSERT(m_innerThreadId
== osl_getThreadIdentifier(NULL
));
309 void AffineBridge::v_leave(void)
311 OSL_ASSERT(m_innerThreadId
== osl_getThreadIdentifier(NULL
));
317 m_innerMutex
.release();
320 int AffineBridge::v_isValid(rtl::OUString
* pReason
)
324 result
= m_enterCount
> 0;
326 *pReason
= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("not entered"));
330 result
= m_innerThreadId
== osl_getThreadIdentifier(NULL
);
333 *pReason
= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("wrong thread"));
337 *pReason
= rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("OK"));
342 #ifdef DISABLE_DYNLOADING
344 #define uno_initEnvironment affine_uno_uno_initEnvironment
345 #define uno_ext_getMapping affine_uno_uno_ext_getMapping
349 extern "C" void SAL_DLLPUBLIC_EXPORT SAL_CALL
uno_initEnvironment(uno_Environment
* pEnv
)
352 cppu::helper::purpenv::Environment_initWithEnterable(pEnv
, new AffineBridge());
355 extern "C" void SAL_DLLPUBLIC_EXPORT SAL_CALL
uno_ext_getMapping(uno_Mapping
** ppMapping
,
356 uno_Environment
* pFrom
,
357 uno_Environment
* pTo
)
359 cppu::helper::purpenv::createMapping(ppMapping
, pFrom
, pTo
);
362 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */