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