xref: /AOO41X/main/cppu/source/threadpool/current.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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 "rtl/uuid.h"
32*cdf0e10cSrcweir #include "osl/thread.h"
33*cdf0e10cSrcweir #include "osl/mutex.hxx"
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #include "uno/environment.hxx"
36*cdf0e10cSrcweir #include "uno/mapping.hxx"
37*cdf0e10cSrcweir #include "uno/lbnames.h"
38*cdf0e10cSrcweir #include "typelib/typedescription.h"
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir #include "current.hxx"
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir using namespace ::osl;
44*cdf0e10cSrcweir using namespace ::rtl;
45*cdf0e10cSrcweir using namespace ::cppu;
46*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir namespace cppu
49*cdf0e10cSrcweir {
50*cdf0e10cSrcweir 
51*cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
52*cdf0e10cSrcweir class SAL_NO_VTABLE XInterface
53*cdf0e10cSrcweir {
54*cdf0e10cSrcweir public:
55*cdf0e10cSrcweir     virtual void SAL_CALL slot_queryInterface() = 0;
56*cdf0e10cSrcweir     virtual void SAL_CALL acquire() throw () = 0;
57*cdf0e10cSrcweir     virtual void SAL_CALL release() throw () = 0;
58*cdf0e10cSrcweir };
59*cdf0e10cSrcweir //--------------------------------------------------------------------------------------------------
60*cdf0e10cSrcweir static typelib_InterfaceTypeDescription * get_type_XCurrentContext()
61*cdf0e10cSrcweir {
62*cdf0e10cSrcweir     static typelib_InterfaceTypeDescription * s_type_XCurrentContext = 0;
63*cdf0e10cSrcweir     if (0 == s_type_XCurrentContext)
64*cdf0e10cSrcweir     {
65*cdf0e10cSrcweir         ::osl::MutexGuard aGuard( ::osl::Mutex::getGlobalMutex() );
66*cdf0e10cSrcweir         if (0 == s_type_XCurrentContext)
67*cdf0e10cSrcweir         {
68*cdf0e10cSrcweir             OUString sTypeName( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.uno.XCurrentContext") );
69*cdf0e10cSrcweir             typelib_InterfaceTypeDescription * pTD = 0;
70*cdf0e10cSrcweir             typelib_TypeDescriptionReference * pMembers[1] = { 0 };
71*cdf0e10cSrcweir             OUString sMethodName0(
72*cdf0e10cSrcweir                 RTL_CONSTASCII_USTRINGPARAM("com.sun.star.uno.XCurrentContext::getValueByName") );
73*cdf0e10cSrcweir             typelib_typedescriptionreference_new(
74*cdf0e10cSrcweir                 &pMembers[0],
75*cdf0e10cSrcweir                 typelib_TypeClass_INTERFACE_METHOD,
76*cdf0e10cSrcweir                 sMethodName0.pData );
77*cdf0e10cSrcweir             typelib_typedescription_newInterface(
78*cdf0e10cSrcweir                 &pTD,
79*cdf0e10cSrcweir                 sTypeName.pData, 0x00000000, 0x0000, 0x0000, 0x00000000, 0x00000000,
80*cdf0e10cSrcweir                 * typelib_static_type_getByTypeClass( typelib_TypeClass_INTERFACE ),
81*cdf0e10cSrcweir                 1,
82*cdf0e10cSrcweir                 pMembers );
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir             typelib_typedescription_register( (typelib_TypeDescription**)&pTD );
85*cdf0e10cSrcweir             typelib_typedescriptionreference_release( pMembers[0] );
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir             typelib_InterfaceMethodTypeDescription * pMethod = 0;
88*cdf0e10cSrcweir             typelib_Parameter_Init aParameters[1];
89*cdf0e10cSrcweir             OUString sParamName0( RTL_CONSTASCII_USTRINGPARAM("Name") );
90*cdf0e10cSrcweir             OUString sParamType0( RTL_CONSTASCII_USTRINGPARAM("string") );
91*cdf0e10cSrcweir             aParameters[0].pParamName = sParamName0.pData;
92*cdf0e10cSrcweir             aParameters[0].eTypeClass = typelib_TypeClass_STRING;
93*cdf0e10cSrcweir             aParameters[0].pTypeName = sParamType0.pData;
94*cdf0e10cSrcweir             aParameters[0].bIn = sal_True;
95*cdf0e10cSrcweir             aParameters[0].bOut = sal_False;
96*cdf0e10cSrcweir             rtl_uString * pExceptions[1];
97*cdf0e10cSrcweir             OUString sExceptionName0(
98*cdf0e10cSrcweir                 RTL_CONSTASCII_USTRINGPARAM("com.sun.star.uno.RuntimeException") );
99*cdf0e10cSrcweir             pExceptions[0] = sExceptionName0.pData;
100*cdf0e10cSrcweir             OUString sReturnType0( RTL_CONSTASCII_USTRINGPARAM("any") );
101*cdf0e10cSrcweir             typelib_typedescription_newInterfaceMethod(
102*cdf0e10cSrcweir                 &pMethod,
103*cdf0e10cSrcweir                 3, sal_False,
104*cdf0e10cSrcweir                 sMethodName0.pData,
105*cdf0e10cSrcweir                 typelib_TypeClass_ANY, sReturnType0.pData,
106*cdf0e10cSrcweir                 1, aParameters, 1, pExceptions );
107*cdf0e10cSrcweir             typelib_typedescription_register( (typelib_TypeDescription**)&pMethod );
108*cdf0e10cSrcweir             typelib_typedescription_release( (typelib_TypeDescription*)pMethod );
109*cdf0e10cSrcweir             // another static ref:
110*cdf0e10cSrcweir             ++reinterpret_cast< typelib_TypeDescription * >( pTD )->
111*cdf0e10cSrcweir                 nStaticRefCount;
112*cdf0e10cSrcweir             s_type_XCurrentContext = pTD;
113*cdf0e10cSrcweir         }
114*cdf0e10cSrcweir     }
115*cdf0e10cSrcweir     return s_type_XCurrentContext;
116*cdf0e10cSrcweir }
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir //##################################################################################################
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir //==================================================================================================
121*cdf0e10cSrcweir class ThreadKey
122*cdf0e10cSrcweir {
123*cdf0e10cSrcweir 	sal_Bool	 _bInit;
124*cdf0e10cSrcweir 	oslThreadKey _hThreadKey;
125*cdf0e10cSrcweir 	oslThreadKeyCallbackFunction _pCallback;
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir public:
128*cdf0e10cSrcweir 	inline oslThreadKey getThreadKey() SAL_THROW( () );
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir 	inline ThreadKey( oslThreadKeyCallbackFunction pCallback ) SAL_THROW( () );
131*cdf0e10cSrcweir 	inline ~ThreadKey() SAL_THROW( () );
132*cdf0e10cSrcweir };
133*cdf0e10cSrcweir //__________________________________________________________________________________________________
134*cdf0e10cSrcweir inline ThreadKey::ThreadKey( oslThreadKeyCallbackFunction pCallback ) SAL_THROW( () )
135*cdf0e10cSrcweir 	: _bInit( sal_False )
136*cdf0e10cSrcweir 	, _pCallback( pCallback )
137*cdf0e10cSrcweir {
138*cdf0e10cSrcweir }
139*cdf0e10cSrcweir //__________________________________________________________________________________________________
140*cdf0e10cSrcweir inline ThreadKey::~ThreadKey() SAL_THROW( () )
141*cdf0e10cSrcweir {
142*cdf0e10cSrcweir 	if (_bInit)
143*cdf0e10cSrcweir 	{
144*cdf0e10cSrcweir 		::osl_destroyThreadKey( _hThreadKey );
145*cdf0e10cSrcweir 	}
146*cdf0e10cSrcweir }
147*cdf0e10cSrcweir //__________________________________________________________________________________________________
148*cdf0e10cSrcweir inline oslThreadKey ThreadKey::getThreadKey() SAL_THROW( () )
149*cdf0e10cSrcweir {
150*cdf0e10cSrcweir 	if (! _bInit)
151*cdf0e10cSrcweir 	{
152*cdf0e10cSrcweir 		MutexGuard aGuard( Mutex::getGlobalMutex() );
153*cdf0e10cSrcweir 		if (! _bInit)
154*cdf0e10cSrcweir 		{
155*cdf0e10cSrcweir 			_hThreadKey = ::osl_createThreadKey( _pCallback );
156*cdf0e10cSrcweir 			_bInit = sal_True;
157*cdf0e10cSrcweir 		}
158*cdf0e10cSrcweir 	}
159*cdf0e10cSrcweir 	return _hThreadKey;
160*cdf0e10cSrcweir }
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir //==================================================================================================
163*cdf0e10cSrcweir extern "C" void SAL_CALL delete_IdContainer( void * p )
164*cdf0e10cSrcweir {
165*cdf0e10cSrcweir 	if (p)
166*cdf0e10cSrcweir 	{
167*cdf0e10cSrcweir 		IdContainer * pId = reinterpret_cast< IdContainer * >( p );
168*cdf0e10cSrcweir 		if (pId->pCurrentContext)
169*cdf0e10cSrcweir 		{
170*cdf0e10cSrcweir             (*pId->pCurrentContextEnv->releaseInterface)(
171*cdf0e10cSrcweir                 pId->pCurrentContextEnv, pId->pCurrentContext );
172*cdf0e10cSrcweir             (*((uno_Environment *)pId->pCurrentContextEnv)->release)(
173*cdf0e10cSrcweir                 (uno_Environment *)pId->pCurrentContextEnv );
174*cdf0e10cSrcweir 		}
175*cdf0e10cSrcweir 		if (pId->bInit)
176*cdf0e10cSrcweir 		{
177*cdf0e10cSrcweir 			::rtl_byte_sequence_release( pId->pLocalThreadId );
178*cdf0e10cSrcweir 			::rtl_byte_sequence_release( pId->pCurrentId );
179*cdf0e10cSrcweir 		}
180*cdf0e10cSrcweir 		delete pId;
181*cdf0e10cSrcweir 	}
182*cdf0e10cSrcweir }
183*cdf0e10cSrcweir //==================================================================================================
184*cdf0e10cSrcweir IdContainer * getIdContainer() SAL_THROW( () )
185*cdf0e10cSrcweir {
186*cdf0e10cSrcweir 	static ThreadKey s_key( delete_IdContainer );
187*cdf0e10cSrcweir 	oslThreadKey aKey = s_key.getThreadKey();
188*cdf0e10cSrcweir 
189*cdf0e10cSrcweir 	IdContainer * pId = reinterpret_cast< IdContainer * >( ::osl_getThreadKeyData( aKey ) );
190*cdf0e10cSrcweir 	if (! pId)
191*cdf0e10cSrcweir 	{
192*cdf0e10cSrcweir 		pId = new IdContainer();
193*cdf0e10cSrcweir 		pId->pCurrentContext = 0;
194*cdf0e10cSrcweir         pId->pCurrentContextEnv = 0;
195*cdf0e10cSrcweir 		pId->bInit = sal_False;
196*cdf0e10cSrcweir 		::osl_setThreadKeyData( aKey, pId );
197*cdf0e10cSrcweir 	}
198*cdf0e10cSrcweir 	return pId;
199*cdf0e10cSrcweir }
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir }
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir //##################################################################################################
204*cdf0e10cSrcweir extern "C" sal_Bool SAL_CALL uno_setCurrentContext(
205*cdf0e10cSrcweir 	void * pCurrentContext,
206*cdf0e10cSrcweir 	rtl_uString * pEnvTypeName, void * pEnvContext )
207*cdf0e10cSrcweir 	SAL_THROW_EXTERN_C()
208*cdf0e10cSrcweir {
209*cdf0e10cSrcweir     IdContainer * pId = getIdContainer();
210*cdf0e10cSrcweir     OSL_ASSERT( pId );
211*cdf0e10cSrcweir 
212*cdf0e10cSrcweir     // free old one
213*cdf0e10cSrcweir     if (pId->pCurrentContext)
214*cdf0e10cSrcweir     {
215*cdf0e10cSrcweir         (*pId->pCurrentContextEnv->releaseInterface)(
216*cdf0e10cSrcweir             pId->pCurrentContextEnv, pId->pCurrentContext );
217*cdf0e10cSrcweir         (*((uno_Environment *)pId->pCurrentContextEnv)->release)(
218*cdf0e10cSrcweir             (uno_Environment *)pId->pCurrentContextEnv );
219*cdf0e10cSrcweir         pId->pCurrentContextEnv = 0;
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir         pId->pCurrentContext = 0;
222*cdf0e10cSrcweir     }
223*cdf0e10cSrcweir 
224*cdf0e10cSrcweir     if (pCurrentContext)
225*cdf0e10cSrcweir     {
226*cdf0e10cSrcweir         uno_Environment * pEnv = 0;
227*cdf0e10cSrcweir         ::uno_getEnvironment( &pEnv, pEnvTypeName, pEnvContext );
228*cdf0e10cSrcweir         OSL_ASSERT( pEnv && pEnv->pExtEnv );
229*cdf0e10cSrcweir         if (pEnv)
230*cdf0e10cSrcweir         {
231*cdf0e10cSrcweir             if (pEnv->pExtEnv)
232*cdf0e10cSrcweir             {
233*cdf0e10cSrcweir                 pId->pCurrentContextEnv = pEnv->pExtEnv;
234*cdf0e10cSrcweir                 (*pId->pCurrentContextEnv->acquireInterface)(
235*cdf0e10cSrcweir                     pId->pCurrentContextEnv, pCurrentContext );
236*cdf0e10cSrcweir                 pId->pCurrentContext = pCurrentContext;
237*cdf0e10cSrcweir             }
238*cdf0e10cSrcweir             else
239*cdf0e10cSrcweir             {
240*cdf0e10cSrcweir                 (*pEnv->release)( pEnv );
241*cdf0e10cSrcweir                 return sal_False;
242*cdf0e10cSrcweir             }
243*cdf0e10cSrcweir         }
244*cdf0e10cSrcweir         else
245*cdf0e10cSrcweir         {
246*cdf0e10cSrcweir             return sal_False;
247*cdf0e10cSrcweir         }
248*cdf0e10cSrcweir     }
249*cdf0e10cSrcweir     return sal_True;
250*cdf0e10cSrcweir }
251*cdf0e10cSrcweir //##################################################################################################
252*cdf0e10cSrcweir extern "C" sal_Bool SAL_CALL uno_getCurrentContext(
253*cdf0e10cSrcweir 	void ** ppCurrentContext, rtl_uString * pEnvTypeName, void * pEnvContext )
254*cdf0e10cSrcweir 	SAL_THROW_EXTERN_C()
255*cdf0e10cSrcweir {
256*cdf0e10cSrcweir     IdContainer * pId = getIdContainer();
257*cdf0e10cSrcweir     OSL_ASSERT( pId );
258*cdf0e10cSrcweir 
259*cdf0e10cSrcweir     Environment target_env;
260*cdf0e10cSrcweir 
261*cdf0e10cSrcweir     // release inout parameter
262*cdf0e10cSrcweir     if (*ppCurrentContext)
263*cdf0e10cSrcweir     {
264*cdf0e10cSrcweir         target_env = Environment(rtl::OUString(pEnvTypeName), pEnvContext);
265*cdf0e10cSrcweir         OSL_ASSERT( target_env.is() );
266*cdf0e10cSrcweir         if (! target_env.is())
267*cdf0e10cSrcweir             return sal_False;
268*cdf0e10cSrcweir         uno_ExtEnvironment * pEnv = target_env.get()->pExtEnv;
269*cdf0e10cSrcweir         OSL_ASSERT( 0 != pEnv );
270*cdf0e10cSrcweir         if (0 == pEnv)
271*cdf0e10cSrcweir             return sal_False;
272*cdf0e10cSrcweir         (*pEnv->releaseInterface)( pEnv, *ppCurrentContext );
273*cdf0e10cSrcweir 
274*cdf0e10cSrcweir         *ppCurrentContext = 0;
275*cdf0e10cSrcweir     }
276*cdf0e10cSrcweir 
277*cdf0e10cSrcweir     // case: null-ref
278*cdf0e10cSrcweir     if (0 == pId->pCurrentContext)
279*cdf0e10cSrcweir         return sal_True;
280*cdf0e10cSrcweir 
281*cdf0e10cSrcweir     if (! target_env.is())
282*cdf0e10cSrcweir     {
283*cdf0e10cSrcweir         target_env = Environment(rtl::OUString(pEnvTypeName), pEnvContext);
284*cdf0e10cSrcweir         OSL_ASSERT( target_env.is() );
285*cdf0e10cSrcweir         if (! target_env.is())
286*cdf0e10cSrcweir             return sal_False;
287*cdf0e10cSrcweir     }
288*cdf0e10cSrcweir 
289*cdf0e10cSrcweir     Mapping mapping((uno_Environment *) pId->pCurrentContextEnv, target_env.get());
290*cdf0e10cSrcweir     OSL_ASSERT( mapping.is() );
291*cdf0e10cSrcweir     if (! mapping.is())
292*cdf0e10cSrcweir         return sal_False;
293*cdf0e10cSrcweir 
294*cdf0e10cSrcweir     mapping.mapInterface(ppCurrentContext, pId->pCurrentContext, ::cppu::get_type_XCurrentContext() );
295*cdf0e10cSrcweir 
296*cdf0e10cSrcweir     return sal_True;
297*cdf0e10cSrcweir }
298