Line data Source code
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 <stdio.h>
21 :
22 : #include <list>
23 :
24 : #include <osl/mutex.hxx>
25 : #include <osl/thread.hxx>
26 : #include <osl/diagnose.h>
27 :
28 : #include <rtl/process.h>
29 : #include <rtl/byteseq.hxx>
30 :
31 : #include <uno/threadpool.h>
32 :
33 : #include "current.hxx"
34 :
35 :
36 : using namespace ::std;
37 : using namespace ::osl;
38 : using namespace ::cppu;
39 :
40 :
41 1709 : static inline void createLocalId( sal_Sequence **ppThreadId )
42 : {
43 1709 : rtl_byte_sequence_constructNoDefault( ppThreadId , 4 + 16 );
44 1709 : sal_uInt32 id = osl::Thread::getCurrentIdentifier();
45 1709 : (*ppThreadId)->elements[0] = id & 0xFF;
46 1709 : (*ppThreadId)->elements[1] = (id >> 8) & 0xFF;
47 1709 : (*ppThreadId)->elements[2] = (id >> 16) & 0xFF;
48 1709 : (*ppThreadId)->elements[3] = (id >> 24) & 0xFF;
49 1709 : rtl_getGlobalProcessId( (sal_uInt8 * ) &( (*ppThreadId)->elements[4]) );
50 1709 : }
51 :
52 :
53 : extern "C" void SAL_CALL
54 61664 : uno_getIdOfCurrentThread( sal_Sequence **ppThreadId )
55 : SAL_THROW_EXTERN_C()
56 : {
57 61664 : IdContainer * p = getIdContainer();
58 61664 : if( ! p->bInit )
59 : {
60 : // first time, that the thread enters the bridge
61 58 : createLocalId( ppThreadId );
62 :
63 : // TODO
64 : // note : this is a leak !
65 58 : p->pLocalThreadId = *ppThreadId;
66 58 : p->pCurrentId = *ppThreadId;
67 58 : p->nRefCountOfCurrentId = 1;
68 58 : rtl_byte_sequence_acquire( p->pLocalThreadId );
69 58 : rtl_byte_sequence_acquire( p->pCurrentId );
70 58 : p->bInit = true;
71 : }
72 : else
73 : {
74 61606 : p->nRefCountOfCurrentId ++;
75 61606 : if( *ppThreadId )
76 : {
77 0 : rtl_byte_sequence_release( *ppThreadId );
78 : }
79 61606 : *ppThreadId = p->pCurrentId;
80 61606 : rtl_byte_sequence_acquire( *ppThreadId );
81 : }
82 61664 : }
83 :
84 :
85 373449 : extern "C" void SAL_CALL uno_releaseIdFromCurrentThread()
86 : SAL_THROW_EXTERN_C()
87 : {
88 373449 : IdContainer *p = getIdContainer();
89 : OSL_ASSERT( p );
90 : OSL_ASSERT( p->nRefCountOfCurrentId );
91 :
92 373449 : p->nRefCountOfCurrentId --;
93 373449 : if( ! p->nRefCountOfCurrentId && (p->pLocalThreadId != p->pCurrentId) )
94 : {
95 311785 : rtl_byte_sequence_assign( &(p->pCurrentId) , p->pLocalThreadId );
96 : }
97 373449 : }
98 :
99 311785 : extern "C" sal_Bool SAL_CALL uno_bindIdToCurrentThread( sal_Sequence *pThreadId )
100 : SAL_THROW_EXTERN_C()
101 : {
102 311785 : IdContainer *p = getIdContainer();
103 311785 : if( ! p->bInit )
104 : {
105 1651 : p->pLocalThreadId = 0;
106 1651 : createLocalId( &(p->pLocalThreadId) );
107 1651 : p->nRefCountOfCurrentId = 1;
108 1651 : p->pCurrentId = pThreadId;
109 1651 : rtl_byte_sequence_acquire( p->pCurrentId );
110 1651 : p->bInit = true;
111 : }
112 : else
113 : {
114 : OSL_ASSERT( 0 == p->nRefCountOfCurrentId );
115 310134 : if( 0 == p->nRefCountOfCurrentId )
116 : {
117 310134 : rtl_byte_sequence_assign(&( p->pCurrentId ), pThreadId );
118 310134 : p->nRefCountOfCurrentId ++;
119 : }
120 : else
121 : {
122 0 : return sal_False;
123 : }
124 :
125 : }
126 311785 : return sal_True;
127 : }
128 :
129 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|