1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 ::std
;
31 using namespace ::osl
;
32 using namespace ::cppu
;
34 static void createLocalId( sal_Sequence
**ppThreadId
)
36 rtl_byte_sequence_constructNoDefault( ppThreadId
, 4 + 16 );
37 sal_uInt32 id
= osl::Thread::getCurrentIdentifier();
38 (*ppThreadId
)->elements
[0] = id
& 0xFF;
39 (*ppThreadId
)->elements
[1] = (id
>> 8) & 0xFF;
40 (*ppThreadId
)->elements
[2] = (id
>> 16) & 0xFF;
41 (*ppThreadId
)->elements
[3] = (id
>> 24) & 0xFF;
42 rtl_getGlobalProcessId( reinterpret_cast<sal_uInt8
*>(&(*ppThreadId
)->elements
[4]) );
45 extern "C" void SAL_CALL
46 uno_getIdOfCurrentThread( sal_Sequence
**ppThreadId
)
49 IdContainer
& id
= getIdContainer();
52 // first time, that the thread enters the bridge
53 createLocalId( ppThreadId
);
56 // note : this is a leak !
57 id
.pLocalThreadId
= *ppThreadId
;
58 id
.pCurrentId
= *ppThreadId
;
59 id
.nRefCountOfCurrentId
= 1;
60 rtl_byte_sequence_acquire( id
.pLocalThreadId
);
61 rtl_byte_sequence_acquire( id
.pCurrentId
);
66 id
.nRefCountOfCurrentId
++;
69 rtl_byte_sequence_release( *ppThreadId
);
71 *ppThreadId
= id
.pCurrentId
;
72 rtl_byte_sequence_acquire( *ppThreadId
);
76 extern "C" void SAL_CALL
uno_releaseIdFromCurrentThread()
79 IdContainer
& id
= getIdContainer();
80 OSL_ASSERT( id
.bInit
);
81 OSL_ASSERT( id
.nRefCountOfCurrentId
);
83 id
.nRefCountOfCurrentId
--;
84 if( ! id
.nRefCountOfCurrentId
&& (id
.pLocalThreadId
!= id
.pCurrentId
) )
86 rtl_byte_sequence_assign( &(id
.pCurrentId
) , id
.pLocalThreadId
);
90 extern "C" sal_Bool SAL_CALL
uno_bindIdToCurrentThread( sal_Sequence
*pThreadId
)
93 IdContainer
& id
= getIdContainer();
96 id
.pLocalThreadId
= nullptr;
97 createLocalId( &(id
.pLocalThreadId
) );
98 id
.nRefCountOfCurrentId
= 1;
99 id
.pCurrentId
= pThreadId
;
100 rtl_byte_sequence_acquire(id
.pCurrentId
);
105 OSL_ASSERT( 0 == id
.nRefCountOfCurrentId
);
106 if( 0 == id
.nRefCountOfCurrentId
)
108 rtl_byte_sequence_assign(&( id
.pCurrentId
), pThreadId
);
109 id
.nRefCountOfCurrentId
++;
120 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */