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.h>
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 ::rtl;
39 : using namespace ::cppu;
40 :
41 :
42 0 : static inline void createLocalId( sal_Sequence **ppThreadId )
43 : {
44 0 : rtl_byte_sequence_constructNoDefault( ppThreadId , 4 + 16 );
45 0 : sal_uInt32 id = osl_getThreadIdentifier(0);
46 0 : (*ppThreadId)->elements[0] = id & 0xFF;
47 0 : (*ppThreadId)->elements[1] = (id >> 8) & 0xFF;
48 0 : (*ppThreadId)->elements[2] = (id >> 16) & 0xFF;
49 0 : (*ppThreadId)->elements[3] = (id >> 24) & 0xFF;
50 0 : rtl_getGlobalProcessId( (sal_uInt8 * ) &( (*ppThreadId)->elements[4]) );
51 0 : }
52 :
53 :
54 : extern "C" void SAL_CALL
55 0 : uno_getIdOfCurrentThread( sal_Sequence **ppThreadId )
56 : SAL_THROW_EXTERN_C()
57 : {
58 0 : IdContainer * p = getIdContainer();
59 0 : if( ! p->bInit )
60 : {
61 : // first time, that the thread enters the bridge
62 0 : createLocalId( ppThreadId );
63 :
64 : // TODO
65 : // note : this is a leak !
66 0 : p->pLocalThreadId = *ppThreadId;
67 0 : p->pCurrentId = *ppThreadId;
68 0 : p->nRefCountOfCurrentId = 1;
69 0 : rtl_byte_sequence_acquire( p->pLocalThreadId );
70 0 : rtl_byte_sequence_acquire( p->pCurrentId );
71 0 : p->bInit = sal_True;
72 : }
73 : else
74 : {
75 0 : p->nRefCountOfCurrentId ++;
76 0 : if( *ppThreadId )
77 : {
78 0 : rtl_byte_sequence_release( *ppThreadId );
79 : }
80 0 : *ppThreadId = p->pCurrentId;
81 0 : rtl_byte_sequence_acquire( *ppThreadId );
82 : }
83 0 : }
84 :
85 :
86 0 : extern "C" void SAL_CALL uno_releaseIdFromCurrentThread()
87 : SAL_THROW_EXTERN_C()
88 : {
89 0 : IdContainer *p = getIdContainer();
90 : OSL_ASSERT( p );
91 : OSL_ASSERT( p->nRefCountOfCurrentId );
92 :
93 0 : p->nRefCountOfCurrentId --;
94 0 : if( ! p->nRefCountOfCurrentId && (p->pLocalThreadId != p->pCurrentId) )
95 : {
96 0 : rtl_byte_sequence_assign( &(p->pCurrentId) , p->pLocalThreadId );
97 : }
98 0 : }
99 :
100 0 : extern "C" sal_Bool SAL_CALL uno_bindIdToCurrentThread( sal_Sequence *pThreadId )
101 : SAL_THROW_EXTERN_C()
102 : {
103 0 : IdContainer *p = getIdContainer();
104 0 : if( ! p->bInit )
105 : {
106 0 : p->pLocalThreadId = 0;
107 0 : createLocalId( &(p->pLocalThreadId) );
108 0 : p->nRefCountOfCurrentId = 1;
109 0 : p->pCurrentId = pThreadId;
110 0 : rtl_byte_sequence_acquire( p->pCurrentId );
111 0 : p->bInit = sal_True;
112 : }
113 : else
114 : {
115 : OSL_ASSERT( 0 == p->nRefCountOfCurrentId );
116 0 : if( 0 == p->nRefCountOfCurrentId )
117 : {
118 0 : rtl_byte_sequence_assign(&( p->pCurrentId ), pThreadId );
119 0 : p->nRefCountOfCurrentId ++;
120 : }
121 : else
122 : {
123 0 : return sal_False;
124 : }
125 :
126 : }
127 0 : return sal_True;
128 : }
129 :
130 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|