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