Version 7.1.7.1, tag libreoffice-7.1.7.1
[LibreOffice.git] / pyuno / source / module / pyuno_gc.cxx
blobe4ed6cb9d0a68bcdfb93c4cf3509e3a6142f03e4
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "pyuno_impl.hxx"
22 #include <sal/config.h>
24 #include <rtl/ref.hxx>
25 #include <salhelper/thread.hxx>
27 namespace pyuno
30 static bool g_destructorsOfStaticObjectsHaveBeenCalled;
32 namespace {
34 class StaticDestructorGuard
36 public:
37 ~StaticDestructorGuard()
39 g_destructorsOfStaticObjectsHaveBeenCalled = true;
45 static StaticDestructorGuard guard;
47 static bool isAfterUnloadOrPy_Finalize()
49 return g_destructorsOfStaticObjectsHaveBeenCalled ||
50 !Py_IsInitialized();
53 namespace {
55 class GCThread: public salhelper::Thread {
56 public:
57 GCThread( PyInterpreterState *interpreter, PyObject * object );
59 private:
60 virtual ~GCThread() override {}
62 virtual void execute() override;
64 PyObject *mPyObject;
65 PyInterpreterState *mPyInterpreter;
70 GCThread::GCThread( PyInterpreterState *interpreter, PyObject * object ) :
71 Thread( "pyunoGCThread" ), mPyObject( object ),
72 mPyInterpreter( interpreter )
75 void GCThread::execute()
77 // otherwise we crash here, when main has been left already
78 if( isAfterUnloadOrPy_Finalize() )
79 return;
80 try
82 PyThreadAttach g( mPyInterpreter );
84 Runtime runtime;
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() )
91 runtime.getImpl()->cargo->mappedObjects.erase( ii );
94 Py_XDECREF( mPyObject );
97 catch( const css::uno::RuntimeException & e )
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());
104 void decreaseRefCount( PyInterpreterState *interpreter, PyObject *object )
106 // otherwise we crash in the last after main ...
107 if( isAfterUnloadOrPy_Finalize() )
108 return;
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 rtl::Reference< GCThread >(new GCThread(interpreter, object))->launch();
115 //TODO: a protocol is missing how to join with the launched thread
116 // before exit(3), to ensure the thread is no longer relying on any
117 // infrastructure while that infrastructure is being shut down in
118 // atexit handlers
123 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */