1*129fa3d1SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*129fa3d1SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*129fa3d1SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*129fa3d1SAndrew Rist  * distributed with this work for additional information
6*129fa3d1SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*129fa3d1SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*129fa3d1SAndrew Rist  * "License"); you may not use this file except in compliance
9*129fa3d1SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*129fa3d1SAndrew Rist  *
11*129fa3d1SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*129fa3d1SAndrew Rist  *
13*129fa3d1SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*129fa3d1SAndrew Rist  * software distributed under the License is distributed on an
15*129fa3d1SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*129fa3d1SAndrew Rist  * KIND, either express or implied.  See the License for the
17*129fa3d1SAndrew Rist  * specific language governing permissions and limitations
18*129fa3d1SAndrew Rist  * under the License.
19*129fa3d1SAndrew Rist  *
20*129fa3d1SAndrew Rist  *************************************************************/
21*129fa3d1SAndrew Rist 
22*129fa3d1SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_cppu.hxx"
26cdf0e10cSrcweir #include <stdio.h>
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include <list>
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include <osl/mutex.hxx>
31cdf0e10cSrcweir #include <osl/thread.h>
32cdf0e10cSrcweir #include <osl/diagnose.h>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #include <rtl/process.h>
35cdf0e10cSrcweir #include <rtl/byteseq.hxx>
36cdf0e10cSrcweir 
37cdf0e10cSrcweir #include <uno/threadpool.h>
38cdf0e10cSrcweir 
39cdf0e10cSrcweir #include "current.hxx"
40cdf0e10cSrcweir 
41cdf0e10cSrcweir 
42cdf0e10cSrcweir using namespace ::std;
43cdf0e10cSrcweir using namespace ::osl;
44cdf0e10cSrcweir using namespace ::rtl;
45cdf0e10cSrcweir using namespace ::cppu;
46cdf0e10cSrcweir 
47cdf0e10cSrcweir 
48cdf0e10cSrcweir static inline void createLocalId( sal_Sequence **ppThreadId )
49cdf0e10cSrcweir {
50cdf0e10cSrcweir 	rtl_byte_sequence_constructNoDefault( ppThreadId , 4 + 16 );
51cdf0e10cSrcweir 	*((sal_Int32*) ((*ppThreadId)->elements)) = osl_getThreadIdentifier(0);
52cdf0e10cSrcweir 
53cdf0e10cSrcweir 	rtl_getGlobalProcessId( (sal_uInt8 * ) &( (*ppThreadId)->elements[4]) );
54cdf0e10cSrcweir }
55cdf0e10cSrcweir 
56cdf0e10cSrcweir 
57cdf0e10cSrcweir extern "C" void SAL_CALL
58cdf0e10cSrcweir uno_getIdOfCurrentThread( sal_Sequence **ppThreadId )
59cdf0e10cSrcweir 	SAL_THROW_EXTERN_C()
60cdf0e10cSrcweir {
61cdf0e10cSrcweir 	IdContainer * p = getIdContainer();
62cdf0e10cSrcweir 	if( ! p->bInit )
63cdf0e10cSrcweir 	{
64cdf0e10cSrcweir 		// first time, that the thread enters the bridge
65cdf0e10cSrcweir 		createLocalId( ppThreadId );
66cdf0e10cSrcweir 
67cdf0e10cSrcweir 		// TODO
68cdf0e10cSrcweir 		// note : this is a leak !
69cdf0e10cSrcweir 		p->pLocalThreadId = *ppThreadId;
70cdf0e10cSrcweir 		p->pCurrentId = *ppThreadId;
71cdf0e10cSrcweir 		p->nRefCountOfCurrentId = 1;
72cdf0e10cSrcweir 		rtl_byte_sequence_acquire( p->pLocalThreadId );
73cdf0e10cSrcweir 		rtl_byte_sequence_acquire( p->pCurrentId );
74cdf0e10cSrcweir 		p->bInit = sal_True;
75cdf0e10cSrcweir 	}
76cdf0e10cSrcweir 	else
77cdf0e10cSrcweir 	{
78cdf0e10cSrcweir 		p->nRefCountOfCurrentId ++;
79cdf0e10cSrcweir 		if( *ppThreadId )
80cdf0e10cSrcweir 		{
81cdf0e10cSrcweir 			rtl_byte_sequence_release( *ppThreadId );
82cdf0e10cSrcweir 		}
83cdf0e10cSrcweir 		*ppThreadId = p->pCurrentId;
84cdf0e10cSrcweir 		rtl_byte_sequence_acquire( *ppThreadId );
85cdf0e10cSrcweir 	}
86cdf0e10cSrcweir }
87cdf0e10cSrcweir 
88cdf0e10cSrcweir 
89cdf0e10cSrcweir extern "C"   void SAL_CALL uno_releaseIdFromCurrentThread()
90cdf0e10cSrcweir 	SAL_THROW_EXTERN_C()
91cdf0e10cSrcweir {
92cdf0e10cSrcweir 	IdContainer *p = getIdContainer();
93cdf0e10cSrcweir 	OSL_ASSERT( p );
94cdf0e10cSrcweir 	OSL_ASSERT( p->nRefCountOfCurrentId );
95cdf0e10cSrcweir 
96cdf0e10cSrcweir 	p->nRefCountOfCurrentId --;
97cdf0e10cSrcweir 	if( ! p->nRefCountOfCurrentId && (p->pLocalThreadId != p->pCurrentId) )
98cdf0e10cSrcweir 	{
99cdf0e10cSrcweir 		rtl_byte_sequence_assign( &(p->pCurrentId) , p->pLocalThreadId );
100cdf0e10cSrcweir 	}
101cdf0e10cSrcweir }
102cdf0e10cSrcweir 
103cdf0e10cSrcweir extern "C"  sal_Bool SAL_CALL uno_bindIdToCurrentThread( sal_Sequence *pThreadId )
104cdf0e10cSrcweir 	SAL_THROW_EXTERN_C()
105cdf0e10cSrcweir {
106cdf0e10cSrcweir 	IdContainer *p = getIdContainer();
107cdf0e10cSrcweir 	if( ! p->bInit )
108cdf0e10cSrcweir 	{
109cdf0e10cSrcweir 		p->pLocalThreadId = 0;
110cdf0e10cSrcweir 		createLocalId( &(p->pLocalThreadId) );
111cdf0e10cSrcweir 		p->nRefCountOfCurrentId = 1;
112cdf0e10cSrcweir 		p->pCurrentId = pThreadId;
113cdf0e10cSrcweir 		rtl_byte_sequence_acquire( p->pCurrentId );
114cdf0e10cSrcweir 		p->bInit = sal_True;
115cdf0e10cSrcweir 	}
116cdf0e10cSrcweir 	else
117cdf0e10cSrcweir 	{
118cdf0e10cSrcweir 		OSL_ASSERT( 0 == p->nRefCountOfCurrentId );
119cdf0e10cSrcweir 		if( 0 == p->nRefCountOfCurrentId )
120cdf0e10cSrcweir 		{
121cdf0e10cSrcweir 			rtl_byte_sequence_assign(&( p->pCurrentId ), pThreadId );
122cdf0e10cSrcweir 			p->nRefCountOfCurrentId ++;
123cdf0e10cSrcweir 		}
124cdf0e10cSrcweir 		else
125cdf0e10cSrcweir 		{
126cdf0e10cSrcweir 			return sal_False;
127cdf0e10cSrcweir 		}
128cdf0e10cSrcweir 
129cdf0e10cSrcweir 	}
130cdf0e10cSrcweir 	return sal_True;
131cdf0e10cSrcweir }
132