Salome HOME
Porting to Mandrake 10.1 and new products:
[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 #include <pthread.h>
37 using namespace std;
38
39 #ifdef _DEBUG_
40 static int MYDEBUG = 0;
41 #else
42 static int MYDEBUG = 0;
43 #endif
44
45
46 static pthread_t myThread;
47
48 void SALOME_Event::GetSessionThread(){
49   myThread = pthread_self();
50 }
51
52 bool SALOME_Event::IsSessionThread(){
53   bool aResult = myThread == pthread_self();
54   if(MYDEBUG) INFOS("IsSessionThread() - "<<aResult);
55   return aResult;
56 }
57
58
59 //===========================================================
60 /*!
61  *  SALOME_Event::SALOME_Event
62  *  Constructor
63  */
64 //===========================================================
65 SALOME_Event::SALOME_Event(){
66   if(MYDEBUG) MESSAGE( "SALOME_Event::SALOME_Event(): this = "<<this );
67   // Prepare the semaphore 
68   mySemaphore = new QSemaphore( 2 );
69   *mySemaphore += 2;
70 }
71
72 //===========================================================
73 /*!
74  *  SALOME_Event::~SALOME_Event
75  *  Destructor
76  */
77 //===========================================================
78 SALOME_Event::~SALOME_Event(){
79   if(MYDEBUG) MESSAGE( "SALOME_Event::~SALOME_Event(): this = "<<this );
80   if ( mySemaphore->available() < mySemaphore->total() )
81     *mySemaphore -= mySemaphore->total() - mySemaphore->available();
82   delete mySemaphore;
83 }
84
85 //===========================================================
86 /*!
87  *  SALOME_Event::process
88  *  Posts the event and optionally waits for its completion
89  */
90 //===========================================================
91 void SALOME_Event::process()
92 {
93   QThread::postEvent( qApp, new QCustomEvent( SALOME_EVENT, (void*)this ) );
94   if(MYDEBUG) MESSAGE( "SALOME_Event::process(): this = "<<this<<", *mySemaphore += 1 " );
95   *mySemaphore += 1;
96   if(MYDEBUG) MESSAGE( "SALOME_Event::process(): this = "<<this<<" - COMPLETED" );
97 }
98
99 //===========================================================
100 /*!
101  *  SALOME_Event::processed
102  *  Signals that this event has been processed
103  */
104 //===========================================================
105 void SALOME_Event::processed()
106 {
107   if(MYDEBUG) MESSAGE( "SALOME_Event::processed(): this = "<<this );
108   // process() takes control over mySemaphore after the next line is executed
109   *mySemaphore -= 1;
110 }