1 // Copyright (C) 2007-2020 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File : PyInterp_Dispatcher.cxx
23 // Author : Sergey Anikin (OPEN CASCADE S.A.S.)
25 #include "PyInterp_Dispatcher.h" // !!! WARNING !!! THIS INCLUDE MUST BE THE VERY FIRST !!!
28 \class PyInterp_Dispatcher
29 \brief Dispatcher of Python events; used to serialize requests to Python interpreter.
32 PyInterp_Dispatcher* PyInterp_Dispatcher::myInstance = 0;
34 PyInterp_Dispatcher* PyInterp_Dispatcher::Get()
37 myInstance = new PyInterp_Dispatcher();
41 PyInterp_Dispatcher::PyInterp_Dispatcher()
46 PyInterp_Dispatcher::~PyInterp_Dispatcher()
48 // Clear the request queue
51 QListIterator<RequestPtr> it( myQueue );
52 while ( it.hasNext() )
53 PyInterp_Request::Destroy( it.next() );
56 myQueueMutex.unlock();
58 // Wait for run() to finish
62 bool PyInterp_Dispatcher::IsBusy() const
67 void PyInterp_Dispatcher::Exec( PyInterp_Request* theRequest )
72 if ( theRequest->IsSync() /*&& !IsBusy()*/)
74 // synchronous processing
75 processRequest( theRequest );
79 // asynchronous processing
82 myQueue.enqueue( theRequest );
83 if ( theRequest->listener() ) {
84 connect( theRequest->listener(), SIGNAL( destroyed( QObject* ) ),
85 this, SLOT( objectDestroyed( QObject* ) ) );
88 myQueueMutex.unlock();
95 void PyInterp_Dispatcher::run()
97 PyInterp_Request* aRequest;
99 // prepare for queue size check
102 while ( myQueue.size() )
104 aRequest = myQueue.head();
106 // let other threads append their requests to the end of the queue
107 myQueueMutex.unlock();
109 // processRequest() may delete a request, so this pointer must not be used
110 // after request is processed!
111 processRequest( aRequest );
113 // prepare for removal of the first request in the queue
116 // IMPORTANT: the first item could have been removed by objectDestroyed() --> we have to check it
117 if ( myQueue.head() == aRequest ) // if it is still here --> remove it
121 myQueueMutex.unlock();
124 void PyInterp_Dispatcher::processRequest( PyInterp_Request* theRequest )
126 theRequest->process();
129 void PyInterp_Dispatcher::objectDestroyed( QObject* o )
131 // prepare for modification of the queue
134 QMutableListIterator<RequestPtr> it( myQueue );
135 while ( it.hasNext() )
137 RequestPtr r = it.next();
138 if ( o == r->listener() )
140 r->setListener( 0 ); // to prevent event posting
145 myQueueMutex.unlock();