Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / cppu / source / threadpool / threadident.cxx
blobf2b9511e6b46f2598771c6c8f3b4c0c9aeb7d8fb
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <osl/thread.hxx>
21 #include <osl/diagnose.h>
23 #include <rtl/process.h>
24 #include <rtl/byteseq.hxx>
26 #include <uno/threadpool.h>
28 #include "current.hxx"
30 using namespace ::osl;
31 using namespace ::cppu;
33 static void createLocalId( sal_Sequence **ppThreadId )
35 rtl_byte_sequence_constructNoDefault( ppThreadId , 4 + 16 );
36 sal_uInt32 id = osl::Thread::getCurrentIdentifier();
37 (*ppThreadId)->elements[0] = id & 0xFF;
38 (*ppThreadId)->elements[1] = (id >> 8) & 0xFF;
39 (*ppThreadId)->elements[2] = (id >> 16) & 0xFF;
40 (*ppThreadId)->elements[3] = (id >> 24) & 0xFF;
41 rtl_getGlobalProcessId( reinterpret_cast<sal_uInt8 *>(&(*ppThreadId)->elements[4]) );
44 extern "C" void SAL_CALL
45 uno_getIdOfCurrentThread( sal_Sequence **ppThreadId )
46 SAL_THROW_EXTERN_C()
48 IdContainer& id = getIdContainer();
49 if (!id.bInit)
51 // first time, that the thread enters the bridge
52 createLocalId( ppThreadId );
54 // TODO
55 // note : this is a leak !
56 id.pLocalThreadId = *ppThreadId;
57 id.pCurrentId = *ppThreadId;
58 id.nRefCountOfCurrentId = 1;
59 rtl_byte_sequence_acquire( id.pLocalThreadId );
60 rtl_byte_sequence_acquire( id.pCurrentId );
61 id.bInit = true;
63 else
65 id.nRefCountOfCurrentId ++;
66 if( *ppThreadId )
68 rtl_byte_sequence_release( *ppThreadId );
70 *ppThreadId = id.pCurrentId;
71 rtl_byte_sequence_acquire( *ppThreadId );
75 extern "C" void SAL_CALL uno_releaseIdFromCurrentThread()
76 SAL_THROW_EXTERN_C()
78 IdContainer& id = getIdContainer();
79 OSL_ASSERT( id.bInit );
80 OSL_ASSERT( id.nRefCountOfCurrentId );
82 id.nRefCountOfCurrentId --;
83 if( ! id.nRefCountOfCurrentId && (id.pLocalThreadId != id.pCurrentId) )
85 rtl_byte_sequence_assign( &(id.pCurrentId) , id.pLocalThreadId );
89 extern "C" sal_Bool SAL_CALL uno_bindIdToCurrentThread( sal_Sequence *pThreadId )
90 SAL_THROW_EXTERN_C()
92 IdContainer& id = getIdContainer();
93 if (!id.bInit)
95 id.pLocalThreadId = nullptr;
96 createLocalId( &(id.pLocalThreadId) );
97 id.nRefCountOfCurrentId = 1;
98 id.pCurrentId = pThreadId;
99 rtl_byte_sequence_acquire(id.pCurrentId);
100 id.bInit = true;
102 else
104 OSL_ASSERT( 0 == id.nRefCountOfCurrentId );
105 if( 0 == id.nRefCountOfCurrentId )
107 rtl_byte_sequence_assign(&( id.pCurrentId ), pThreadId );
108 id.nRefCountOfCurrentId ++;
110 else
112 return false;
116 return true;
119 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */