Branch data 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 : :
21 : : #include "osl/mutex.hxx"
22 : : #include "osl/thread.h"
23 : :
24 : : #include "cppu/helper/purpenv/Environment.hxx"
25 : : #include "cppu/helper/purpenv/Mapping.hxx"
26 : :
27 : :
28 : : #ifdef debug
29 : : # define LOG_LIFECYCLE_UnsafeBridge
30 : : #endif
31 : :
32 : : #ifdef LOG_LIFECYCLE_UnsafeBridge
33 : : # include <iostream>
34 : : # define LOG_LIFECYCLE_UnsafeBridge_emit(x) x
35 : :
36 : : #else
37 : : # define LOG_LIFECYCLE_UnsafeBridge_emit(x)
38 : :
39 : : #endif
40 : :
41 : :
42 : : class SAL_DLLPRIVATE UnsafeBridge : public cppu::Enterable
43 : : {
44 : : osl::Mutex m_mutex;
45 : : sal_Int32 m_count;
46 : : oslThreadIdentifier m_threadId;
47 : :
48 : : virtual ~UnsafeBridge(void);
49 : :
50 : : public:
51 : : explicit UnsafeBridge(void);
52 : :
53 : : virtual void v_callInto_v(uno_EnvCallee * pCallee, va_list * pParam);
54 : : virtual void v_callOut_v (uno_EnvCallee * pCallee, va_list * pParam);
55 : :
56 : : virtual void v_enter(void);
57 : : virtual void v_leave(void);
58 : :
59 : : virtual int v_isValid(rtl::OUString * pReason);
60 : : };
61 : :
62 : 0 : UnsafeBridge::UnsafeBridge(void)
63 : : : m_count (0),
64 : 0 : m_threadId(0)
65 : : {
66 : : LOG_LIFECYCLE_UnsafeBridge_emit(fprintf(stderr, "LIFE: %s -> %p\n", "UnsafeBridge::UnsafeBridge(uno_Environment * pEnv)", this));
67 : 0 : }
68 : :
69 : 0 : UnsafeBridge::~UnsafeBridge(void)
70 : : {
71 : : LOG_LIFECYCLE_UnsafeBridge_emit(fprintf(stderr, "LIFE: %s -> %p\n", "UnsafeBridge::~UnsafeBridge(void)", this));
72 : :
73 : : OSL_ASSERT(m_count >= 0);
74 : 0 : }
75 : :
76 : 0 : void UnsafeBridge::v_callInto_v(uno_EnvCallee * pCallee, va_list * pParam)
77 : : {
78 : 0 : enter();
79 : 0 : pCallee(pParam);
80 : 0 : leave();
81 : 0 : }
82 : :
83 : 0 : void UnsafeBridge::v_callOut_v(uno_EnvCallee * pCallee, va_list * pParam)
84 : : {
85 : : OSL_ASSERT(m_count > 0);
86 : :
87 : 0 : -- m_count;
88 : 0 : pCallee(pParam);
89 : 0 : ++ m_count;
90 : :
91 : 0 : if (!m_threadId)
92 : 0 : m_threadId = osl_getThreadIdentifier(NULL);
93 : 0 : }
94 : :
95 : 0 : void UnsafeBridge::v_enter(void)
96 : : {
97 : 0 : m_mutex.acquire();
98 : :
99 : : OSL_ASSERT(m_count >= 0);
100 : :
101 : 0 : if (m_count == 0)
102 : 0 : m_threadId = osl_getThreadIdentifier(NULL);
103 : :
104 : 0 : ++ m_count;
105 : 0 : }
106 : :
107 : 0 : void UnsafeBridge::v_leave(void)
108 : : {
109 : : OSL_ASSERT(m_count > 0);
110 : :
111 : 0 : -- m_count;
112 : 0 : if (!m_count)
113 : 0 : m_threadId = 0;
114 : :
115 : :
116 : 0 : m_mutex.release();
117 : 0 : }
118 : :
119 : 0 : int UnsafeBridge::v_isValid(rtl::OUString * pReason)
120 : : {
121 : 0 : int result = 1;
122 : :
123 : 0 : result = m_count > 0;
124 : 0 : if (!result)
125 : 0 : *pReason = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("not entered"));
126 : :
127 : : else
128 : : {
129 : 0 : result = m_threadId == osl_getThreadIdentifier(NULL);
130 : :
131 : 0 : if (!result)
132 : 0 : *pReason = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("wrong thread"));
133 : : }
134 : :
135 : 0 : if (result)
136 : 0 : *pReason = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("OK"));
137 : :
138 : 0 : return result;
139 : : }
140 : :
141 : 0 : extern "C" void SAL_DLLPUBLIC_EXPORT SAL_CALL uno_initEnvironment(uno_Environment * pEnv)
142 : : SAL_THROW_EXTERN_C()
143 : : {
144 : 0 : cppu::helper::purpenv::Environment_initWithEnterable(pEnv, new UnsafeBridge());
145 : 0 : }
146 : :
147 : 0 : extern "C" void SAL_DLLPUBLIC_EXPORT SAL_CALL uno_ext_getMapping(uno_Mapping ** ppMapping,
148 : : uno_Environment * pFrom,
149 : : uno_Environment * pTo )
150 : : {
151 : 0 : cppu::helper::purpenv::createMapping(ppMapping, pFrom, pTo);
152 : 0 : }
153 : :
154 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|