Salome HOME
DCQ : Merge with Ecole_ete_a6.
[modules/kernel.git] / src / Event / SALOME_Event.cxx
1 //  KERNEL SALOME_Event : Define event posting mechanism
2 //
3 //  Copyright (C) 2003  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. 
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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : SALOME_Event.cxx
25 //  Author : Sergey ANIKIN
26 //  Module : KERNEL
27 //  $Header$
28
29 #include "SALOME_Event.hxx"
30
31 #include "utilities.h"
32
33 #include <qsemaphore.h>
34 #include <qapplication.h>
35 #include <qthread.h>
36
37 #ifdef _DEBUG_
38 static int MYDEBUG = 0;
39 #else
40 static int MYDEBUG = 0;
41 #endif
42
43 //===========================================================
44 /*!
45  *  SALOME_Event::SALOME_Event
46  *  Constructor
47  */
48 //===========================================================
49 SALOME_Event::SALOME_Event(): 
50   myWait( true ),
51   myAutoRelease( false )
52 {
53   if(MYDEBUG) MESSAGE( "SALOME_Event::SALOME_Event(): this = "<<this<<", myWait = "<<myWait );
54   if ( myWait ) {
55     // Prepare the semaphore 
56     mySemaphore = new QSemaphore( 2 );
57     mySemaphore->operator+=( 2 );
58   }
59 }
60
61 //===========================================================
62 /*!
63  *  SALOME_Event::~SALOME_Event
64  *  Destructor
65  */
66 //===========================================================
67 SALOME_Event::~SALOME_Event()
68 {
69   if(MYDEBUG) MESSAGE( "SALOME_Event::~SALOME_Event(): this = "<<this<<", myWait = "<<myWait );
70   if ( myWait ) {
71     if ( mySemaphore->available() < mySemaphore->total() )
72       mySemaphore->operator-=( mySemaphore->total() - mySemaphore->available() );
73     delete mySemaphore;
74   }
75 }
76
77 //===========================================================
78 /*!
79  *  SALOME_Event::process
80  *  Posts the event and optionally waits for its completion
81  */
82 //===========================================================
83 void SALOME_Event::process()
84 {
85   if(MYDEBUG) MESSAGE( "SALOME_Event::process(): this = "<<this<<", myWait = "<<myWait );
86   QThread::postEvent( qApp, new QCustomEvent( SALOME_EVENT, (void*)this ) );
87   if ( myWait ) {
88     if(MYDEBUG) MESSAGE( "SALOME_Event::process(): available = " << mySemaphore->available() );
89     if ( !mySemaphore->available() )
90       mySemaphore->operator+=( 1 );
91
92     if(MYDEBUG) MESSAGE( "SALOME_Event::process() COMPLETED: this = "<<this<<", myWait = "<<myWait );
93   }
94   if ( myAutoRelease )
95     release();
96 }
97
98 //===========================================================
99 /*!
100  *  SALOME_Event::processed
101  *  Signals that this event has been processed
102  */
103 //===========================================================
104 void SALOME_Event::processed()
105 {
106   if(MYDEBUG) MESSAGE( "SALOME_Event::processed(): this = "<<this<<", myWait = "<<myWait );
107   if ( 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 );
112
113       if(MYDEBUG) MESSAGE( "SALOME_Event::processed(): semaphore DECREMENTED" );
114
115       // Current thread will block here until process() completes
116       mySemaphore->operator+=( mySemaphore->total() );
117     }
118   }
119   if(MYDEBUG) MESSAGE( "SALOME_Event::processed() COMPLETED: this = "<<this<<", myWait = "<<myWait );
120 }
121
122 //===========================================================
123 /*!
124  *  SALOME_Event::release
125  *  Wakes up the desktop
126  */
127 //===========================================================
128 void SALOME_Event::release()
129 {
130   if(MYDEBUG) MESSAGE( "SALOME_Event::release(): this = "<<this<<", myWait = "<<myWait );
131   if ( myWait ) {
132     if(MYDEBUG) MESSAGE( "SALOME_Event::release(): available = " << mySemaphore->available() );
133     mySemaphore->operator-=( mySemaphore->total() - mySemaphore->available() );
134   }
135   if(MYDEBUG) MESSAGE( "SALOME_Event::release() COMPLETED: this = "<<this<<", myWait = "<<myWait );
136 }
137
138
139
140