1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_cppu.hxx" 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir #include "osl/thread.hxx" 32*cdf0e10cSrcweir #include "osl/conditn.hxx" 33*cdf0e10cSrcweir #include "osl/mutex.hxx" 34*cdf0e10cSrcweir 35*cdf0e10cSrcweir #include "cppu/helper/purpenv/Environment.hxx" 36*cdf0e10cSrcweir #include "cppu/helper/purpenv/Mapping.hxx" 37*cdf0e10cSrcweir 38*cdf0e10cSrcweir 39*cdf0e10cSrcweir #ifdef debug 40*cdf0e10cSrcweir # define LOG_LIFECYCLE_AffineBridge 41*cdf0e10cSrcweir #endif 42*cdf0e10cSrcweir 43*cdf0e10cSrcweir #ifdef LOG_LIFECYCLE_AffineBridge 44*cdf0e10cSrcweir # include <iostream> 45*cdf0e10cSrcweir # define LOG_LIFECYCLE_AffineBridge_emit(x) x 46*cdf0e10cSrcweir 47*cdf0e10cSrcweir #else 48*cdf0e10cSrcweir # define LOG_LIFECYCLE_AffineBridge_emit(x) 49*cdf0e10cSrcweir 50*cdf0e10cSrcweir #endif 51*cdf0e10cSrcweir 52*cdf0e10cSrcweir class InnerThread; 53*cdf0e10cSrcweir class OuterThread; 54*cdf0e10cSrcweir 55*cdf0e10cSrcweir class SAL_DLLPRIVATE AffineBridge : public cppu::Enterable 56*cdf0e10cSrcweir { 57*cdf0e10cSrcweir public: 58*cdf0e10cSrcweir enum Msg 59*cdf0e10cSrcweir { 60*cdf0e10cSrcweir CB_DONE, 61*cdf0e10cSrcweir CB_FPOINTER 62*cdf0e10cSrcweir }; 63*cdf0e10cSrcweir 64*cdf0e10cSrcweir Msg m_message; 65*cdf0e10cSrcweir uno_EnvCallee * m_pCallee; 66*cdf0e10cSrcweir va_list * m_pParam; 67*cdf0e10cSrcweir 68*cdf0e10cSrcweir osl::Mutex m_innerMutex; 69*cdf0e10cSrcweir oslThreadIdentifier m_innerThreadId; 70*cdf0e10cSrcweir InnerThread * m_pInnerThread; 71*cdf0e10cSrcweir osl::Condition m_innerCondition; 72*cdf0e10cSrcweir sal_Int32 m_enterCount; 73*cdf0e10cSrcweir 74*cdf0e10cSrcweir osl::Mutex m_outerMutex; 75*cdf0e10cSrcweir oslThreadIdentifier m_outerThreadId; 76*cdf0e10cSrcweir osl::Condition m_outerCondition; 77*cdf0e10cSrcweir OuterThread * m_pOuterThread; 78*cdf0e10cSrcweir 79*cdf0e10cSrcweir explicit AffineBridge(void); 80*cdf0e10cSrcweir virtual ~AffineBridge(void); 81*cdf0e10cSrcweir 82*cdf0e10cSrcweir virtual void v_callInto_v(uno_EnvCallee * pCallee, va_list * pParam); 83*cdf0e10cSrcweir virtual void v_callOut_v (uno_EnvCallee * pCallee, va_list * pParam); 84*cdf0e10cSrcweir 85*cdf0e10cSrcweir virtual void v_enter(void); 86*cdf0e10cSrcweir virtual void v_leave(void); 87*cdf0e10cSrcweir 88*cdf0e10cSrcweir virtual int v_isValid(rtl::OUString * pReason); 89*cdf0e10cSrcweir 90*cdf0e10cSrcweir void innerDispatch(void); 91*cdf0e10cSrcweir void outerDispatch(int loop); 92*cdf0e10cSrcweir }; 93*cdf0e10cSrcweir 94*cdf0e10cSrcweir class SAL_DLLPRIVATE InnerThread : public osl::Thread 95*cdf0e10cSrcweir { 96*cdf0e10cSrcweir virtual void SAL_CALL run(void); 97*cdf0e10cSrcweir 98*cdf0e10cSrcweir AffineBridge * m_pAffineBridge; 99*cdf0e10cSrcweir 100*cdf0e10cSrcweir public: 101*cdf0e10cSrcweir InnerThread(AffineBridge * threadEnvironment) 102*cdf0e10cSrcweir : m_pAffineBridge(threadEnvironment) 103*cdf0e10cSrcweir { 104*cdf0e10cSrcweir create(); 105*cdf0e10cSrcweir } 106*cdf0e10cSrcweir }; 107*cdf0e10cSrcweir 108*cdf0e10cSrcweir void InnerThread::run(void) 109*cdf0e10cSrcweir { 110*cdf0e10cSrcweir m_pAffineBridge->enter(); 111*cdf0e10cSrcweir m_pAffineBridge->innerDispatch(); 112*cdf0e10cSrcweir m_pAffineBridge->leave(); 113*cdf0e10cSrcweir } 114*cdf0e10cSrcweir 115*cdf0e10cSrcweir class SAL_DLLPRIVATE OuterThread : public osl::Thread 116*cdf0e10cSrcweir { 117*cdf0e10cSrcweir virtual void SAL_CALL run(void); 118*cdf0e10cSrcweir 119*cdf0e10cSrcweir AffineBridge * m_pAffineBridge; 120*cdf0e10cSrcweir 121*cdf0e10cSrcweir public: 122*cdf0e10cSrcweir OuterThread(AffineBridge * threadEnvironment); 123*cdf0e10cSrcweir }; 124*cdf0e10cSrcweir 125*cdf0e10cSrcweir OuterThread::OuterThread(AffineBridge * threadEnvironment) 126*cdf0e10cSrcweir : m_pAffineBridge(threadEnvironment) 127*cdf0e10cSrcweir { 128*cdf0e10cSrcweir create(); 129*cdf0e10cSrcweir } 130*cdf0e10cSrcweir 131*cdf0e10cSrcweir void OuterThread::run(void) 132*cdf0e10cSrcweir { 133*cdf0e10cSrcweir osl::MutexGuard guard(m_pAffineBridge->m_outerMutex); 134*cdf0e10cSrcweir 135*cdf0e10cSrcweir m_pAffineBridge->m_outerThreadId = getIdentifier(); 136*cdf0e10cSrcweir m_pAffineBridge->outerDispatch(0); 137*cdf0e10cSrcweir m_pAffineBridge->m_outerThreadId = 0; 138*cdf0e10cSrcweir 139*cdf0e10cSrcweir m_pAffineBridge->m_pOuterThread = NULL; 140*cdf0e10cSrcweir m_pAffineBridge = NULL; 141*cdf0e10cSrcweir } 142*cdf0e10cSrcweir 143*cdf0e10cSrcweir 144*cdf0e10cSrcweir AffineBridge::AffineBridge(void) 145*cdf0e10cSrcweir : m_innerThreadId(0), 146*cdf0e10cSrcweir m_pInnerThread (NULL), 147*cdf0e10cSrcweir m_enterCount (0), 148*cdf0e10cSrcweir m_outerThreadId(0), 149*cdf0e10cSrcweir m_pOuterThread (NULL) 150*cdf0e10cSrcweir { 151*cdf0e10cSrcweir LOG_LIFECYCLE_AffineBridge_emit(fprintf(stderr, "LIFE: %s -> %p\n", "AffineBridge::AffineBridge(uno_Environment * pEnv)", this)); 152*cdf0e10cSrcweir } 153*cdf0e10cSrcweir 154*cdf0e10cSrcweir AffineBridge::~AffineBridge(void) 155*cdf0e10cSrcweir { 156*cdf0e10cSrcweir LOG_LIFECYCLE_AffineBridge_emit(fprintf(stderr, "LIFE: %s -> %p\n", "AffineBridge::~AffineBridge(void)", this)); 157*cdf0e10cSrcweir 158*cdf0e10cSrcweir if (m_pInnerThread && osl_getThreadIdentifier(NULL) != m_innerThreadId) 159*cdf0e10cSrcweir { 160*cdf0e10cSrcweir m_message = CB_DONE; 161*cdf0e10cSrcweir m_innerCondition.set(); 162*cdf0e10cSrcweir 163*cdf0e10cSrcweir m_pInnerThread->join(); 164*cdf0e10cSrcweir } 165*cdf0e10cSrcweir 166*cdf0e10cSrcweir delete m_pInnerThread; 167*cdf0e10cSrcweir 168*cdf0e10cSrcweir if (m_pOuterThread) 169*cdf0e10cSrcweir { 170*cdf0e10cSrcweir m_pOuterThread->join(); 171*cdf0e10cSrcweir delete m_pOuterThread; 172*cdf0e10cSrcweir } 173*cdf0e10cSrcweir } 174*cdf0e10cSrcweir 175*cdf0e10cSrcweir 176*cdf0e10cSrcweir void AffineBridge::outerDispatch(int loop) 177*cdf0e10cSrcweir { 178*cdf0e10cSrcweir OSL_ASSERT(m_outerThreadId == osl_getThreadIdentifier(NULL)); 179*cdf0e10cSrcweir OSL_ASSERT(m_innerThreadId != m_outerThreadId); 180*cdf0e10cSrcweir 181*cdf0e10cSrcweir Msg mm; 182*cdf0e10cSrcweir 183*cdf0e10cSrcweir do 184*cdf0e10cSrcweir { 185*cdf0e10cSrcweir // FIXME: created outer thread must not wait 186*cdf0e10cSrcweir // in case of no message 187*cdf0e10cSrcweir // note: no message can happen in case newly created 188*cdf0e10cSrcweir // outer thread acquire outerMutex after a real outer 189*cdf0e10cSrcweir // thread enters outerDispatch! 190*cdf0e10cSrcweir m_outerCondition.wait(); 191*cdf0e10cSrcweir m_outerCondition.reset(); 192*cdf0e10cSrcweir 193*cdf0e10cSrcweir mm = m_message; 194*cdf0e10cSrcweir 195*cdf0e10cSrcweir switch(mm) 196*cdf0e10cSrcweir { 197*cdf0e10cSrcweir case CB_DONE: 198*cdf0e10cSrcweir break; 199*cdf0e10cSrcweir 200*cdf0e10cSrcweir case CB_FPOINTER: 201*cdf0e10cSrcweir { 202*cdf0e10cSrcweir m_pCallee(m_pParam); 203*cdf0e10cSrcweir 204*cdf0e10cSrcweir m_message = CB_DONE; 205*cdf0e10cSrcweir m_innerCondition.set(); 206*cdf0e10cSrcweir break; 207*cdf0e10cSrcweir } 208*cdf0e10cSrcweir default: 209*cdf0e10cSrcweir abort(); 210*cdf0e10cSrcweir } 211*cdf0e10cSrcweir } 212*cdf0e10cSrcweir while(mm != CB_DONE && loop); 213*cdf0e10cSrcweir } 214*cdf0e10cSrcweir 215*cdf0e10cSrcweir void AffineBridge::innerDispatch(void) 216*cdf0e10cSrcweir { 217*cdf0e10cSrcweir OSL_ASSERT(m_innerThreadId == osl_getThreadIdentifier(NULL)); 218*cdf0e10cSrcweir OSL_ASSERT(m_innerThreadId != m_outerThreadId); 219*cdf0e10cSrcweir 220*cdf0e10cSrcweir Msg mm; 221*cdf0e10cSrcweir 222*cdf0e10cSrcweir do 223*cdf0e10cSrcweir { 224*cdf0e10cSrcweir m_innerCondition.wait(); 225*cdf0e10cSrcweir m_innerCondition.reset(); 226*cdf0e10cSrcweir 227*cdf0e10cSrcweir mm = m_message; 228*cdf0e10cSrcweir 229*cdf0e10cSrcweir switch(mm) 230*cdf0e10cSrcweir { 231*cdf0e10cSrcweir case CB_DONE: 232*cdf0e10cSrcweir break; 233*cdf0e10cSrcweir 234*cdf0e10cSrcweir case CB_FPOINTER: 235*cdf0e10cSrcweir { 236*cdf0e10cSrcweir m_pCallee(m_pParam); 237*cdf0e10cSrcweir 238*cdf0e10cSrcweir m_message = CB_DONE; 239*cdf0e10cSrcweir m_outerCondition.set(); 240*cdf0e10cSrcweir break; 241*cdf0e10cSrcweir } 242*cdf0e10cSrcweir default: 243*cdf0e10cSrcweir abort(); 244*cdf0e10cSrcweir } 245*cdf0e10cSrcweir } 246*cdf0e10cSrcweir while(mm != CB_DONE); 247*cdf0e10cSrcweir } 248*cdf0e10cSrcweir 249*cdf0e10cSrcweir void AffineBridge::v_callInto_v(uno_EnvCallee * pCallee, va_list * pParam) 250*cdf0e10cSrcweir { 251*cdf0e10cSrcweir osl::MutexGuard guard(m_outerMutex); // only one thread at a time can call into 252*cdf0e10cSrcweir 253*cdf0e10cSrcweir if (m_innerThreadId == 0) // no inner thread yet 254*cdf0e10cSrcweir { 255*cdf0e10cSrcweir m_pInnerThread = new InnerThread(this); 256*cdf0e10cSrcweir m_pInnerThread->resume(); 257*cdf0e10cSrcweir } 258*cdf0e10cSrcweir 259*cdf0e10cSrcweir bool resetId = false; 260*cdf0e10cSrcweir if (!m_outerThreadId) 261*cdf0e10cSrcweir { 262*cdf0e10cSrcweir m_outerThreadId = osl_getThreadIdentifier(NULL); 263*cdf0e10cSrcweir resetId = true; 264*cdf0e10cSrcweir } 265*cdf0e10cSrcweir 266*cdf0e10cSrcweir m_message = CB_FPOINTER; 267*cdf0e10cSrcweir m_pCallee = pCallee; 268*cdf0e10cSrcweir m_pParam = pParam; 269*cdf0e10cSrcweir m_innerCondition.set(); 270*cdf0e10cSrcweir 271*cdf0e10cSrcweir outerDispatch(1); 272*cdf0e10cSrcweir 273*cdf0e10cSrcweir if (resetId) 274*cdf0e10cSrcweir m_outerThreadId = 0; 275*cdf0e10cSrcweir } 276*cdf0e10cSrcweir 277*cdf0e10cSrcweir void AffineBridge::v_callOut_v(uno_EnvCallee * pCallee, va_list * pParam) 278*cdf0e10cSrcweir { 279*cdf0e10cSrcweir OSL_ASSERT(m_innerThreadId); 280*cdf0e10cSrcweir 281*cdf0e10cSrcweir osl::MutexGuard guard(m_innerMutex); 282*cdf0e10cSrcweir 283*cdf0e10cSrcweir if (m_outerThreadId == 0) // no outer thread yet 284*cdf0e10cSrcweir { 285*cdf0e10cSrcweir osl::MutexGuard guard_m_outerMutex(m_outerMutex); 286*cdf0e10cSrcweir 287*cdf0e10cSrcweir if (m_outerThreadId == 0) 288*cdf0e10cSrcweir { 289*cdf0e10cSrcweir if (m_pOuterThread) 290*cdf0e10cSrcweir { 291*cdf0e10cSrcweir m_pOuterThread->join(); 292*cdf0e10cSrcweir delete m_pOuterThread; 293*cdf0e10cSrcweir } 294*cdf0e10cSrcweir 295*cdf0e10cSrcweir m_pOuterThread = new OuterThread(this); 296*cdf0e10cSrcweir } 297*cdf0e10cSrcweir } 298*cdf0e10cSrcweir 299*cdf0e10cSrcweir m_message = CB_FPOINTER; 300*cdf0e10cSrcweir m_pCallee = pCallee; 301*cdf0e10cSrcweir m_pParam = pParam; 302*cdf0e10cSrcweir m_outerCondition.set(); 303*cdf0e10cSrcweir 304*cdf0e10cSrcweir innerDispatch(); 305*cdf0e10cSrcweir } 306*cdf0e10cSrcweir 307*cdf0e10cSrcweir void AffineBridge::v_enter(void) 308*cdf0e10cSrcweir { 309*cdf0e10cSrcweir m_innerMutex.acquire(); 310*cdf0e10cSrcweir 311*cdf0e10cSrcweir if (!m_enterCount) 312*cdf0e10cSrcweir m_innerThreadId = osl_getThreadIdentifier(NULL); 313*cdf0e10cSrcweir 314*cdf0e10cSrcweir OSL_ASSERT(m_innerThreadId == osl_getThreadIdentifier(NULL)); 315*cdf0e10cSrcweir 316*cdf0e10cSrcweir ++ m_enterCount; 317*cdf0e10cSrcweir } 318*cdf0e10cSrcweir 319*cdf0e10cSrcweir void AffineBridge::v_leave(void) 320*cdf0e10cSrcweir { 321*cdf0e10cSrcweir OSL_ASSERT(m_innerThreadId == osl_getThreadIdentifier(NULL)); 322*cdf0e10cSrcweir 323*cdf0e10cSrcweir -- m_enterCount; 324*cdf0e10cSrcweir if (!m_enterCount) 325*cdf0e10cSrcweir m_innerThreadId = 0; 326*cdf0e10cSrcweir 327*cdf0e10cSrcweir m_innerMutex.release(); 328*cdf0e10cSrcweir } 329*cdf0e10cSrcweir 330*cdf0e10cSrcweir int AffineBridge::v_isValid(rtl::OUString * pReason) 331*cdf0e10cSrcweir { 332*cdf0e10cSrcweir int result = 1; 333*cdf0e10cSrcweir 334*cdf0e10cSrcweir result = m_enterCount > 0; 335*cdf0e10cSrcweir if (!result) 336*cdf0e10cSrcweir *pReason = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("not entered")); 337*cdf0e10cSrcweir 338*cdf0e10cSrcweir else 339*cdf0e10cSrcweir { 340*cdf0e10cSrcweir result = m_innerThreadId == osl_getThreadIdentifier(NULL); 341*cdf0e10cSrcweir 342*cdf0e10cSrcweir if (!result) 343*cdf0e10cSrcweir *pReason = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("wrong thread")); 344*cdf0e10cSrcweir } 345*cdf0e10cSrcweir 346*cdf0e10cSrcweir if (result) 347*cdf0e10cSrcweir *pReason = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("OK")); 348*cdf0e10cSrcweir 349*cdf0e10cSrcweir return result; 350*cdf0e10cSrcweir } 351*cdf0e10cSrcweir 352*cdf0e10cSrcweir extern "C" void SAL_DLLPUBLIC_EXPORT SAL_CALL uno_initEnvironment(uno_Environment * pEnv) 353*cdf0e10cSrcweir SAL_THROW_EXTERN_C() 354*cdf0e10cSrcweir { 355*cdf0e10cSrcweir cppu::helper::purpenv::Environment_initWithEnterable(pEnv, new AffineBridge()); 356*cdf0e10cSrcweir } 357*cdf0e10cSrcweir 358*cdf0e10cSrcweir extern "C" void SAL_DLLPUBLIC_EXPORT SAL_CALL uno_ext_getMapping(uno_Mapping ** ppMapping, 359*cdf0e10cSrcweir uno_Environment * pFrom, 360*cdf0e10cSrcweir uno_Environment * pTo ) 361*cdf0e10cSrcweir { 362*cdf0e10cSrcweir cppu::helper::purpenv::createMapping(ppMapping, pFrom, pTo); 363*cdf0e10cSrcweir } 364*cdf0e10cSrcweir 365