1*67c7d1c1SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*67c7d1c1SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*67c7d1c1SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*67c7d1c1SAndrew Rist * distributed with this work for additional information 6*67c7d1c1SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*67c7d1c1SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*67c7d1c1SAndrew Rist * "License"); you may not use this file except in compliance 9*67c7d1c1SAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 11*67c7d1c1SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 13*67c7d1c1SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*67c7d1c1SAndrew Rist * software distributed under the License is distributed on an 15*67c7d1c1SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*67c7d1c1SAndrew Rist * KIND, either express or implied. See the License for the 17*67c7d1c1SAndrew Rist * specific language governing permissions and limitations 18*67c7d1c1SAndrew Rist * under the License. 19cdf0e10cSrcweir * 20*67c7d1c1SAndrew Rist *************************************************************/ 21*67c7d1c1SAndrew Rist 22*67c7d1c1SAndrew Rist 23cdf0e10cSrcweir #include <pyuno_impl.hxx> 24cdf0e10cSrcweir #include <osl/thread.hxx> 25cdf0e10cSrcweir namespace pyuno 26cdf0e10cSrcweir { 27cdf0e10cSrcweir 28cdf0e10cSrcweir bool g_destructorsOfStaticObjectsHaveBeenCalled; 29cdf0e10cSrcweir class StaticDestructorGuard 30cdf0e10cSrcweir { 31cdf0e10cSrcweir public: 32cdf0e10cSrcweir ~StaticDestructorGuard() 33cdf0e10cSrcweir { 34cdf0e10cSrcweir g_destructorsOfStaticObjectsHaveBeenCalled = true; 35cdf0e10cSrcweir } 36cdf0e10cSrcweir }; 37cdf0e10cSrcweir StaticDestructorGuard guard; 38cdf0e10cSrcweir 39cdf0e10cSrcweir static bool isAfterUnloadOrPy_Finalize() 40cdf0e10cSrcweir { 41cdf0e10cSrcweir return g_destructorsOfStaticObjectsHaveBeenCalled || 42cdf0e10cSrcweir !Py_IsInitialized(); 43cdf0e10cSrcweir } 44cdf0e10cSrcweir 45cdf0e10cSrcweir class GCThread : public ::osl::Thread 46cdf0e10cSrcweir { 47cdf0e10cSrcweir PyObject *mPyObject; 48cdf0e10cSrcweir PyInterpreterState *mPyInterpreter; 49cdf0e10cSrcweir GCThread( const GCThread & ); // not implemented 50cdf0e10cSrcweir GCThread &operator =( const GCThread & ); // not implemented 51cdf0e10cSrcweir 52cdf0e10cSrcweir public: 53cdf0e10cSrcweir GCThread( PyInterpreterState *interpreter, PyObject * object ); 54cdf0e10cSrcweir virtual void SAL_CALL run(); 55cdf0e10cSrcweir virtual void SAL_CALL onTerminated(); 56cdf0e10cSrcweir }; 57cdf0e10cSrcweir 58cdf0e10cSrcweir 59cdf0e10cSrcweir GCThread::GCThread( PyInterpreterState *interpreter, PyObject * object ) : 60cdf0e10cSrcweir mPyObject( object ), mPyInterpreter( interpreter ) 61cdf0e10cSrcweir {} 62cdf0e10cSrcweir 63cdf0e10cSrcweir void GCThread::run() 64cdf0e10cSrcweir { 65cdf0e10cSrcweir // otherwise we crash here, when main has been left already 66cdf0e10cSrcweir if( isAfterUnloadOrPy_Finalize() ) 67cdf0e10cSrcweir return; 68cdf0e10cSrcweir try 69cdf0e10cSrcweir { 70cdf0e10cSrcweir PyThreadAttach g( (PyInterpreterState*)mPyInterpreter ); 71cdf0e10cSrcweir { 72cdf0e10cSrcweir Runtime runtime; 73cdf0e10cSrcweir 74cdf0e10cSrcweir // remove the reference from the pythonobject2adapter map 75cdf0e10cSrcweir PyRef2Adapter::iterator ii = 76cdf0e10cSrcweir runtime.getImpl()->cargo->mappedObjects.find( mPyObject ); 77cdf0e10cSrcweir if( ii != runtime.getImpl()->cargo->mappedObjects.end() ) 78cdf0e10cSrcweir { 79cdf0e10cSrcweir runtime.getImpl()->cargo->mappedObjects.erase( ii ); 80cdf0e10cSrcweir } 81cdf0e10cSrcweir 82cdf0e10cSrcweir Py_XDECREF( mPyObject ); 83cdf0e10cSrcweir } 84cdf0e10cSrcweir } 85cdf0e10cSrcweir catch( com::sun::star::uno::RuntimeException & e ) 86cdf0e10cSrcweir { 87cdf0e10cSrcweir rtl::OString msg; 88cdf0e10cSrcweir msg = rtl::OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US ); 89cdf0e10cSrcweir fprintf( stderr, "Leaking python objects bridged to UNO for reason %s\n",msg.getStr()); 90cdf0e10cSrcweir } 91cdf0e10cSrcweir } 92cdf0e10cSrcweir 93cdf0e10cSrcweir 94cdf0e10cSrcweir void GCThread::onTerminated() 95cdf0e10cSrcweir { 96cdf0e10cSrcweir delete this; 97cdf0e10cSrcweir } 98cdf0e10cSrcweir 99cdf0e10cSrcweir void decreaseRefCount( PyInterpreterState *interpreter, PyObject *object ) 100cdf0e10cSrcweir { 101cdf0e10cSrcweir // otherwise we crash in the last after main ... 102cdf0e10cSrcweir if( isAfterUnloadOrPy_Finalize() ) 103cdf0e10cSrcweir return; 104cdf0e10cSrcweir 105cdf0e10cSrcweir // delegate to a new thread, because there does not seem 106cdf0e10cSrcweir // to be a method, which tells, whether the global 107cdf0e10cSrcweir // interpreter lock is held or not 108cdf0e10cSrcweir // TODO: Look for a more efficient solution 109cdf0e10cSrcweir osl::Thread *t = new GCThread( interpreter, object ); 110cdf0e10cSrcweir t->create(); 111cdf0e10cSrcweir } 112cdf0e10cSrcweir 113cdf0e10cSrcweir } 114