1 // KERNEL SALOME_Event : Define event posting mechanism
3 // Copyright (C) 2003 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.
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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : SALOME_Event.cxx
25 // Author : Sergey ANIKIN
29 #include "SALOME_Event.hxx"
31 #include "utilities.h"
33 #include <qsemaphore.h>
34 #include <qapplication.h>
38 static int MYDEBUG = 0;
40 static int MYDEBUG = 0;
43 //===========================================================
45 * SALOME_Event::SALOME_Event
48 //===========================================================
49 SALOME_Event::SALOME_Event():
51 myAutoRelease( false )
53 if(MYDEBUG) MESSAGE( "SALOME_Event::SALOME_Event(): this = "<<this<<", myWait = "<<myWait );
55 // Prepare the semaphore
56 mySemaphore = new QSemaphore( 2 );
57 mySemaphore->operator+=( 2 );
61 //===========================================================
63 * SALOME_Event::~SALOME_Event
66 //===========================================================
67 SALOME_Event::~SALOME_Event()
69 if(MYDEBUG) MESSAGE( "SALOME_Event::~SALOME_Event(): this = "<<this<<", myWait = "<<myWait );
71 if ( mySemaphore->available() < mySemaphore->total() )
72 mySemaphore->operator-=( mySemaphore->total() - mySemaphore->available() );
77 //===========================================================
79 * SALOME_Event::process
80 * Posts the event and optionally waits for its completion
82 //===========================================================
83 void SALOME_Event::process()
85 if(MYDEBUG) MESSAGE( "SALOME_Event::process(): this = "<<this<<", myWait = "<<myWait );
86 QThread::postEvent( qApp, new QCustomEvent( SALOME_EVENT, (void*)this ) );
88 if(MYDEBUG) MESSAGE( "SALOME_Event::process(): available = " << mySemaphore->available() );
89 if ( !mySemaphore->available() )
90 mySemaphore->operator+=( 1 );
92 if(MYDEBUG) MESSAGE( "SALOME_Event::process() COMPLETED: this = "<<this<<", myWait = "<<myWait );
98 //===========================================================
100 * SALOME_Event::processed
101 * Signals that this event has been processed
103 //===========================================================
104 void SALOME_Event::processed()
106 if(MYDEBUG) MESSAGE( "SALOME_Event::processed(): this = "<<this<<", myWait = "<<myWait );
108 if(MYDEBUG) MESSAGE( "SALOME_Event::processed(): available = " << mySemaphore->available() );
109 if ( !mySemaphore->available() ) {
110 // process() takes control over mySemaphore after the next line is executed
111 mySemaphore->operator-=( 1 );
113 if(MYDEBUG) MESSAGE( "SALOME_Event::processed(): semaphore DECREMENTED" );
115 // Current thread will block here until process() completes
116 mySemaphore->operator+=( mySemaphore->total() );
119 if(MYDEBUG) MESSAGE( "SALOME_Event::processed() COMPLETED: this = "<<this<<", myWait = "<<myWait );
122 //===========================================================
124 * SALOME_Event::release
125 * Wakes up the desktop
127 //===========================================================
128 void SALOME_Event::release()
130 if(MYDEBUG) MESSAGE( "SALOME_Event::release(): this = "<<this<<", myWait = "<<myWait );
132 if(MYDEBUG) MESSAGE( "SALOME_Event::release(): available = " << mySemaphore->available() );
133 mySemaphore->operator-=( mySemaphore->total() - mySemaphore->available() );
135 if(MYDEBUG) MESSAGE( "SALOME_Event::release() COMPLETED: this = "<<this<<", myWait = "<<myWait );