LibreOffice Module pyuno (master) 1
pyuno_gc.cxx
Go to the documentation of this file.
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 "pyuno_impl.hxx"
21
22#include <sal/config.h>
23
24#include <rtl/ref.hxx>
25#include <salhelper/thread.hxx>
26
27namespace pyuno
28{
29
31
32namespace {
33
34class StaticDestructorGuard
35{
36public:
37 ~StaticDestructorGuard()
38 {
40 }
41};
42
43}
44
45static StaticDestructorGuard guard;
46
48{
50 !Py_IsInitialized();
51}
52
53namespace {
54
55class GCThread: public salhelper::Thread {
56public:
57 GCThread( PyInterpreterState *interpreter, PyObject * object );
58
59private:
60 virtual ~GCThread() override {}
61
62 virtual void execute() override;
63
64 PyObject *mPyObject;
65 PyInterpreterState *mPyInterpreter;
66};
67
68}
69
70GCThread::GCThread( PyInterpreterState *interpreter, PyObject * object ) :
71 Thread( "pyunoGCThread" ), mPyObject( object ),
72 mPyInterpreter( interpreter )
73{}
74
75void GCThread::execute()
76{
77 // otherwise we crash here, when main has been left already
79 return;
80 try
81 {
82 PyThreadAttach g( mPyInterpreter );
83 {
84 Runtime runtime;
85
86 // remove the reference from the pythonobject2adapter map
87 PyRef2Adapter::iterator ii =
88 runtime.getImpl()->cargo->mappedObjects.find( mPyObject );
89 if( ii != runtime.getImpl()->cargo->mappedObjects.end() )
90 {
91 runtime.getImpl()->cargo->mappedObjects.erase( ii );
92 }
93
94 Py_XDECREF( mPyObject );
95 }
96 }
97 catch( const css::uno::RuntimeException & e )
98 {
99 OString msg = OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US );
100 fprintf( stderr, "Leaking python objects bridged to UNO for reason %s\n",msg.getStr());
101 }
102}
103
104void decreaseRefCount( PyInterpreterState *interpreter, PyObject *object )
105{
106 // otherwise we crash in the last after main ...
108 return;
109
110 // delegate to a new thread, because there does not seem
111 // to be a method, which tells, whether the global
112 // interpreter lock is held or not
113 // TODO: Look for a more efficient solution
114 try
115 {
116 rtl::Reference< GCThread >(new GCThread(interpreter, object))->launch();
117 //TODO: a protocol is missing how to join with the launched thread
118 // before exit(3), to ensure the thread is no longer relying on any
119 // infrastructure while that infrastructure is being shut down in
120 // atexit handlers
121 }
122 catch (std::runtime_error&)
123 {
124 // tdf#146621: Thread creation will fail on Windows with ERROR_ACCESS_DENIED
125 // when called at ExitProcess time; unhandled exception would hang the process
126 abort();
127 }
128}
129
130}
131
132/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
Definition: pyuno.cxx:72
static bool g_destructorsOfStaticObjectsHaveBeenCalled
Definition: pyuno_gc.cxx:30
void decreaseRefCount(PyInterpreterState *interpreter, PyObject *object)
releases a refcount on the interpreter object and on another given python object.
Definition: pyuno_gc.cxx:104
static StaticDestructorGuard guard
Definition: pyuno_gc.cxx:45
static bool isAfterUnloadOrPy_Finalize()
Definition: pyuno_gc.cxx:47
PyInterpreterState * mPyInterpreter
Definition: pyuno_gc.cxx:65
PyObject * mPyObject
Definition: pyuno_gc.cxx:64