Salome HOME
c470576e41b00c685d89b511ebb840ff7e0d9bb1
[modules/gui.git] / tools / PyInterp / src / PyInterp_Dispatcher.cxx
1 // Copyright (C) 2007-2022  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  File   : PyInterp_Dispatcher.cxx
23 //  Author : Sergey Anikin (OPEN CASCADE S.A.S.)
24
25 #include "PyInterp_Dispatcher.h"   // !!! WARNING !!! THIS INCLUDE MUST BE THE VERY FIRST !!!
26
27 /**
28    \class PyInterp_Dispatcher
29    \brief Dispatcher of Python events; used to serialize requests to Python interpreter.
30 */
31
32 PyInterp_Dispatcher* PyInterp_Dispatcher::myInstance = 0;
33
34 PyInterp_Dispatcher* PyInterp_Dispatcher::Get()
35 {
36   if ( !myInstance )
37     myInstance = new PyInterp_Dispatcher();
38   return myInstance;
39 }
40
41 PyInterp_Dispatcher::PyInterp_Dispatcher() 
42 : QThread()
43 {
44 }
45
46 PyInterp_Dispatcher::~PyInterp_Dispatcher()
47 {
48   // Clear the request queue
49   myQueueMutex.lock();
50
51   QListIterator<RequestPtr> it( myQueue );
52   while ( it.hasNext() )
53     PyInterp_Request::Destroy( it.next() );
54   myQueue.clear();
55
56   myQueueMutex.unlock();
57
58   // Wait for run() to finish
59   wait();
60 }
61
62 bool PyInterp_Dispatcher::IsBusy() const
63 {
64   return isRunning();
65 }
66
67 void PyInterp_Dispatcher::Exec( PyInterp_Request* theRequest )
68 {
69   if ( !theRequest )
70     return;
71
72   if ( theRequest->IsSync() /*&& !IsBusy()*/)
73   {
74     // synchronous processing
75     processRequest( theRequest );
76   }
77   else
78   {
79     // asynchronous processing
80     myQueueMutex.lock();
81
82     myQueue.enqueue( theRequest );
83     if ( theRequest->listener() ) {
84       connect( theRequest->listener(), SIGNAL( destroyed( QObject* ) ),
85                this, SLOT( objectDestroyed( QObject* ) ) );
86     }
87
88     myQueueMutex.unlock();  
89
90     if ( !IsBusy() )
91       start();
92   }
93 }
94
95 void PyInterp_Dispatcher::run()
96 {
97   PyInterp_Request* aRequest;
98
99   // prepare for queue size check
100   myQueueMutex.lock();
101
102   while ( myQueue.size() )
103   {
104     aRequest = myQueue.head();
105
106     // let other threads append their requests to the end of the queue
107     myQueueMutex.unlock();
108
109     // processRequest() may delete a request, so this pointer must not be used
110     // after request is processed!
111     processRequest( aRequest );
112
113     // prepare for removal of the first request in the queue
114     myQueueMutex.lock();
115   
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
118       myQueue.dequeue();
119   }
120
121   myQueueMutex.unlock();
122 }
123
124 void PyInterp_Dispatcher::processRequest( PyInterp_Request* theRequest )
125 {
126   theRequest->process();
127 }
128
129 void PyInterp_Dispatcher::objectDestroyed( QObject* o )
130 {
131   // prepare for modification of the queue
132   myQueueMutex.lock();
133
134   QMutableListIterator<RequestPtr> it( myQueue );
135   while ( it.hasNext() )
136   {
137     RequestPtr r = it.next();
138     if ( o == r->listener() )
139     {
140       r->setListener( 0 ); // to prevent event posting
141       it.remove();
142     }
143   }
144
145   myQueueMutex.unlock();
146 }