xref: /trunk/main/cppu/source/UnsafeBridge/UnsafeBridge.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_cppu.hxx"
30 
31 #include "osl/mutex.hxx"
32 #include "osl/thread.h"
33 
34 #include "cppu/helper/purpenv/Environment.hxx"
35 #include "cppu/helper/purpenv/Mapping.hxx"
36 
37 
38 #ifdef debug
39 # define LOG_LIFECYCLE_UnsafeBridge
40 #endif
41 
42 #ifdef LOG_LIFECYCLE_UnsafeBridge
43 #  include <iostream>
44 #  define LOG_LIFECYCLE_UnsafeBridge_emit(x) x
45 
46 #else
47 #  define LOG_LIFECYCLE_UnsafeBridge_emit(x)
48 
49 #endif
50 
51 
52 class SAL_DLLPRIVATE UnsafeBridge : public cppu::Enterable
53 {
54     osl::Mutex          m_mutex;
55     sal_Int32           m_count;
56     oslThreadIdentifier m_threadId;
57 
58     virtual  ~UnsafeBridge(void);
59 
60 public:
61     explicit UnsafeBridge(void);
62 
63     virtual void v_callInto_v(uno_EnvCallee * pCallee, va_list * pParam);
64     virtual void v_callOut_v (uno_EnvCallee * pCallee, va_list * pParam);
65 
66     virtual void v_enter(void);
67     virtual void v_leave(void);
68 
69     virtual int  v_isValid(rtl::OUString * pReason);
70 };
71 
72 UnsafeBridge::UnsafeBridge(void)
73     : m_count   (0),
74       m_threadId(0)
75 {
76     LOG_LIFECYCLE_UnsafeBridge_emit(fprintf(stderr, "LIFE: %s -> %p\n", "UnsafeBridge::UnsafeBridge(uno_Environment * pEnv)", this));
77 }
78 
79 UnsafeBridge::~UnsafeBridge(void)
80 {
81     LOG_LIFECYCLE_UnsafeBridge_emit(fprintf(stderr, "LIFE: %s -> %p\n", "UnsafeBridge::~UnsafeBridge(void)", this));
82 
83     OSL_ASSERT(m_count >= 0);
84 }
85 
86 void UnsafeBridge::v_callInto_v(uno_EnvCallee * pCallee, va_list * pParam)
87 {
88     enter();
89     pCallee(pParam);
90     leave();
91 }
92 
93 void UnsafeBridge::v_callOut_v(uno_EnvCallee * pCallee, va_list * pParam)
94 {
95     OSL_ASSERT(m_count > 0);
96 
97     -- m_count;
98     pCallee(pParam);
99     ++ m_count;
100 
101     if (!m_threadId)
102         m_threadId = osl_getThreadIdentifier(NULL);
103 }
104 
105 void UnsafeBridge::v_enter(void)
106 {
107     m_mutex.acquire();
108 
109     OSL_ASSERT(m_count >= 0);
110 
111     if (m_count == 0)
112         m_threadId = osl_getThreadIdentifier(NULL);
113 
114     ++ m_count;
115 }
116 
117 void UnsafeBridge::v_leave(void)
118 {
119     OSL_ASSERT(m_count > 0);
120 
121     -- m_count;
122     if (!m_count)
123         m_threadId = 0;
124 
125 
126     m_mutex.release();
127 }
128 
129 int UnsafeBridge::v_isValid(rtl::OUString * pReason)
130 {
131     int result = 1;
132 
133     result = m_count > 0;
134     if (!result)
135         *pReason = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("not entered"));
136 
137     else
138     {
139         result = m_threadId == osl_getThreadIdentifier(NULL);
140 
141         if (!result)
142             *pReason = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("wrong thread"));
143     }
144 
145     if (result)
146         *pReason = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("OK"));
147 
148     return result;
149 }
150 
151 extern "C" void SAL_DLLPUBLIC_EXPORT SAL_CALL uno_initEnvironment(uno_Environment * pEnv)
152     SAL_THROW_EXTERN_C()
153 {
154     cppu::helper::purpenv::Environment_initWithEnterable(pEnv, new UnsafeBridge());
155 }
156 
157 extern "C" void SAL_DLLPUBLIC_EXPORT SAL_CALL uno_ext_getMapping(uno_Mapping     ** ppMapping,
158                                    uno_Environment  * pFrom,
159                                    uno_Environment  * pTo )
160 {
161     cppu::helper::purpenv::createMapping(ppMapping, pFrom, pTo);
162 }
163 
164