LibreOffice Module cppu (master) 1
threadident.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <osl/thread.hxx>
21#include <osl/diagnose.h>
22
23#include <rtl/process.h>
24#include <rtl/byteseq.hxx>
25
26#include <uno/threadpool.h>
27
28#include "current.hxx"
29
30using namespace ::osl;
31using namespace ::cppu;
32
33static void createLocalId( sal_Sequence **ppThreadId )
34{
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]) );
42}
43
44extern "C" void SAL_CALL
47{
48 IdContainer& id = getIdContainer();
49 if (!id.bInit)
50 {
51 // first time, that the thread enters the bridge
52 createLocalId( ppThreadId );
53
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;
62 }
63 else
64 {
65 id.nRefCountOfCurrentId ++;
66 if( *ppThreadId )
67 {
68 rtl_byte_sequence_release( *ppThreadId );
69 }
70 *ppThreadId = id.pCurrentId;
71 rtl_byte_sequence_acquire( *ppThreadId );
72 }
73}
74
75extern "C" void SAL_CALL uno_releaseIdFromCurrentThread()
76 SAL_THROW_EXTERN_C()
77{
78 IdContainer& id = getIdContainer();
79 OSL_ASSERT( id.bInit );
80 OSL_ASSERT( id.nRefCountOfCurrentId );
81
82 id.nRefCountOfCurrentId --;
83 if( ! id.nRefCountOfCurrentId && (id.pLocalThreadId != id.pCurrentId) )
84 {
85 rtl_byte_sequence_assign( &(id.pCurrentId) , id.pLocalThreadId );
86 }
87}
88
89extern "C" sal_Bool SAL_CALL uno_bindIdToCurrentThread( sal_Sequence *pThreadId )
91{
92 IdContainer& id = getIdContainer();
93 if (!id.bInit)
94 {
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;
101 }
102 else
103 {
104 OSL_ASSERT( 0 == id.nRefCountOfCurrentId );
105 if( 0 == id.nRefCountOfCurrentId )
106 {
107 rtl_byte_sequence_assign(&( id.pCurrentId ), pThreadId );
108 id.nRefCountOfCurrentId ++;
109 }
110 else
111 {
112 return false;
113 }
114
115 }
116 return true;
117}
118
119/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
IdContainer & getIdContainer()
Definition: current.cxx:109
void SAL_CALL uno_releaseIdFromCurrentThread() SAL_THROW_EXTERN_C()
Definition: threadident.cxx:75
static void createLocalId(sal_Sequence **ppThreadId)
Definition: threadident.cxx:33
sal_Bool SAL_CALL uno_bindIdToCurrentThread(sal_Sequence *pThreadId) SAL_THROW_EXTERN_C()
Definition: threadident.cxx:89
void SAL_CALL uno_getIdOfCurrentThread(sal_Sequence **ppThreadId) SAL_THROW_EXTERN_C()
Definition: threadident.cxx:45
unsigned char sal_uInt8
unsigned char sal_Bool
#define SAL_THROW_EXTERN_C()