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 .
24 #include <osl/mutex.hxx>
25 #include <osl/thread.h>
26 #include <osl/diagnose.h>
28 #include <rtl/process.h>
29 #include <rtl/byteseq.hxx>
31 #include <uno/threadpool.h>
33 #include "current.hxx"
36 using namespace ::std
;
37 using namespace ::osl
;
38 using namespace ::rtl
;
39 using namespace ::cppu
;
42 static inline void createLocalId( sal_Sequence
**ppThreadId
)
44 rtl_byte_sequence_constructNoDefault( ppThreadId
, 4 + 16 );
45 sal_uInt32 id
= osl_getThreadIdentifier(0);
46 (*ppThreadId
)->elements
[0] = id
& 0xFF;
47 (*ppThreadId
)->elements
[1] = (id
>> 8) & 0xFF;
48 (*ppThreadId
)->elements
[2] = (id
>> 16) & 0xFF;
49 (*ppThreadId
)->elements
[3] = (id
>> 24) & 0xFF;
50 rtl_getGlobalProcessId( (sal_uInt8
* ) &( (*ppThreadId
)->elements
[4]) );
54 extern "C" void SAL_CALL
55 uno_getIdOfCurrentThread( sal_Sequence
**ppThreadId
)
58 IdContainer
* p
= getIdContainer();
61 // first time, that the thread enters the bridge
62 createLocalId( ppThreadId
);
65 // note : this is a leak !
66 p
->pLocalThreadId
= *ppThreadId
;
67 p
->pCurrentId
= *ppThreadId
;
68 p
->nRefCountOfCurrentId
= 1;
69 rtl_byte_sequence_acquire( p
->pLocalThreadId
);
70 rtl_byte_sequence_acquire( p
->pCurrentId
);
75 p
->nRefCountOfCurrentId
++;
78 rtl_byte_sequence_release( *ppThreadId
);
80 *ppThreadId
= p
->pCurrentId
;
81 rtl_byte_sequence_acquire( *ppThreadId
);
86 extern "C" void SAL_CALL
uno_releaseIdFromCurrentThread()
89 IdContainer
*p
= getIdContainer();
91 OSL_ASSERT( p
->nRefCountOfCurrentId
);
93 p
->nRefCountOfCurrentId
--;
94 if( ! p
->nRefCountOfCurrentId
&& (p
->pLocalThreadId
!= p
->pCurrentId
) )
96 rtl_byte_sequence_assign( &(p
->pCurrentId
) , p
->pLocalThreadId
);
100 extern "C" sal_Bool SAL_CALL
uno_bindIdToCurrentThread( sal_Sequence
*pThreadId
)
103 IdContainer
*p
= getIdContainer();
106 p
->pLocalThreadId
= 0;
107 createLocalId( &(p
->pLocalThreadId
) );
108 p
->nRefCountOfCurrentId
= 1;
109 p
->pCurrentId
= pThreadId
;
110 rtl_byte_sequence_acquire( p
->pCurrentId
);
115 OSL_ASSERT( 0 == p
->nRefCountOfCurrentId
);
116 if( 0 == p
->nRefCountOfCurrentId
)
118 rtl_byte_sequence_assign(&( p
->pCurrentId
), pThreadId
);
119 p
->nRefCountOfCurrentId
++;
130 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */