merged tag ooo/DEV300_m102
[LibreOffice.git] / cppu / source / threadpool / threadident.cxx
blob0fb6c11961856892c1662b088a3864fa31dc3efc
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_cppu.hxx"
30 #include <stdio.h>
32 #include <list>
34 #include <osl/mutex.hxx>
35 #include <osl/thread.h>
36 #include <osl/diagnose.h>
38 #include <rtl/process.h>
39 #include <rtl/byteseq.hxx>
41 #include <uno/threadpool.h>
43 #include "current.hxx"
46 using namespace ::std;
47 using namespace ::osl;
48 using namespace ::rtl;
49 using namespace ::cppu;
52 static inline void createLocalId( sal_Sequence **ppThreadId )
54 rtl_byte_sequence_constructNoDefault( ppThreadId , 4 + 16 );
55 *((sal_Int32*) ((*ppThreadId)->elements)) = osl_getThreadIdentifier(0);
57 rtl_getGlobalProcessId( (sal_uInt8 * ) &( (*ppThreadId)->elements[4]) );
61 extern "C" void SAL_CALL
62 uno_getIdOfCurrentThread( sal_Sequence **ppThreadId )
63 SAL_THROW_EXTERN_C()
65 IdContainer * p = getIdContainer();
66 if( ! p->bInit )
68 // first time, that the thread enters the bridge
69 createLocalId( ppThreadId );
71 // TODO
72 // note : this is a leak !
73 p->pLocalThreadId = *ppThreadId;
74 p->pCurrentId = *ppThreadId;
75 p->nRefCountOfCurrentId = 1;
76 rtl_byte_sequence_acquire( p->pLocalThreadId );
77 rtl_byte_sequence_acquire( p->pCurrentId );
78 p->bInit = sal_True;
80 else
82 p->nRefCountOfCurrentId ++;
83 if( *ppThreadId )
85 rtl_byte_sequence_release( *ppThreadId );
87 *ppThreadId = p->pCurrentId;
88 rtl_byte_sequence_acquire( *ppThreadId );
93 extern "C" void SAL_CALL uno_releaseIdFromCurrentThread()
94 SAL_THROW_EXTERN_C()
96 IdContainer *p = getIdContainer();
97 OSL_ASSERT( p );
98 OSL_ASSERT( p->nRefCountOfCurrentId );
100 p->nRefCountOfCurrentId --;
101 if( ! p->nRefCountOfCurrentId && (p->pLocalThreadId != p->pCurrentId) )
103 rtl_byte_sequence_assign( &(p->pCurrentId) , p->pLocalThreadId );
107 extern "C" sal_Bool SAL_CALL uno_bindIdToCurrentThread( sal_Sequence *pThreadId )
108 SAL_THROW_EXTERN_C()
110 IdContainer *p = getIdContainer();
111 if( ! p->bInit )
113 p->pLocalThreadId = 0;
114 createLocalId( &(p->pLocalThreadId) );
115 p->nRefCountOfCurrentId = 1;
116 p->pCurrentId = pThreadId;
117 rtl_byte_sequence_acquire( p->pCurrentId );
118 p->bInit = sal_True;
120 else
122 OSL_ASSERT( 0 == p->nRefCountOfCurrentId );
123 if( 0 == p->nRefCountOfCurrentId )
125 rtl_byte_sequence_assign(&( p->pCurrentId ), pThreadId );
126 p->nRefCountOfCurrentId ++;
128 else
130 return sal_False;
134 return sal_True;