LCOV - code coverage report
Current view: top level - pyuno/source/module - pyuno_gc.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 26 30 86.7 %
Date: 2014-04-11 Functions: 9 9 100.0 %
Legend: Lines: hit not hit

          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             : #include "pyuno_impl.hxx"
      21             : 
      22             : #include "sal/config.h"
      23             : 
      24             : #include "rtl/ref.hxx"
      25             : #include "salhelper/thread.hxx"
      26             : 
      27             : namespace pyuno
      28             : {
      29             : 
      30             : bool g_destructorsOfStaticObjectsHaveBeenCalled;
      31             : class StaticDestructorGuard
      32             : {
      33             : public:
      34           2 :     ~StaticDestructorGuard()
      35             :     {
      36           2 :         g_destructorsOfStaticObjectsHaveBeenCalled = true;
      37           2 :     }
      38             : };
      39           2 : StaticDestructorGuard guard;
      40             : 
      41           8 : static bool isAfterUnloadOrPy_Finalize()
      42             : {
      43          16 :     return g_destructorsOfStaticObjectsHaveBeenCalled ||
      44          16 :         !Py_IsInitialized();
      45             : }
      46             : 
      47             : class GCThread: public salhelper::Thread {
      48             : public:
      49             :     GCThread( PyInterpreterState *interpreter, PyObject * object );
      50             : 
      51             : private:
      52           8 :     virtual ~GCThread() {}
      53             : 
      54             :     virtual void execute() SAL_OVERRIDE;
      55             : 
      56             :     PyObject *mPyObject;
      57             :     PyInterpreterState *mPyInterpreter;
      58             : };
      59             : 
      60           4 : GCThread::GCThread( PyInterpreterState *interpreter, PyObject * object ) :
      61             :     Thread( "pyunoGCThread" ), mPyObject( object ),
      62           4 :     mPyInterpreter( interpreter )
      63           4 : {}
      64             : 
      65           4 : void GCThread::execute()
      66             : {
      67             :     //  otherwise we crash here, when main has been left already
      68           4 :     if( isAfterUnloadOrPy_Finalize() )
      69           4 :         return;
      70             :     try
      71             :     {
      72           4 :         PyThreadAttach g( (PyInterpreterState*)mPyInterpreter );
      73             :         {
      74           4 :             Runtime runtime;
      75             : 
      76             :             // remove the reference from the pythonobject2adapter map
      77             :             PyRef2Adapter::iterator ii =
      78           4 :                 runtime.getImpl()->cargo->mappedObjects.find( mPyObject );
      79           4 :             if( ii != runtime.getImpl()->cargo->mappedObjects.end() )
      80             :             {
      81           4 :                 runtime.getImpl()->cargo->mappedObjects.erase( ii );
      82             :             }
      83             : 
      84           4 :             Py_XDECREF( mPyObject );
      85           4 :         }
      86             :     }
      87           0 :     catch( const com::sun::star::uno::RuntimeException & e )
      88             :     {
      89           0 :         OString msg;
      90           0 :         msg = OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US );
      91           0 :         fprintf( stderr, "Leaking python objects bridged to UNO for reason %s\n",msg.getStr());
      92             :     }
      93             : }
      94             : 
      95           4 : void decreaseRefCount( PyInterpreterState *interpreter, PyObject *object )
      96             : {
      97             :     //  otherwise we crash in the last after main ...
      98           4 :     if( isAfterUnloadOrPy_Finalize() )
      99           4 :         return;
     100             : 
     101             :     // delegate to a new thread, because there does not seem
     102             :     // to be a method, which tells, whether the global
     103             :     // interpreter lock is held or not
     104             :     // TODO: Look for a more efficient solution
     105           4 :     rtl::Reference< GCThread >(new GCThread(interpreter, object))->launch();
     106             :         //TODO: a protocol is missing how to join with the launched thread
     107             :         // before exit(3), to ensure the thread is no longer relying on any
     108             :         // infrastructure while that infrastructure is being shut down in
     109             :         // atexit handlers
     110             : }
     111             : 
     112           6 : }
     113             : 
     114             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10