Salome HOME
84928f6232cf5795667c1029dca4aa09a0ed7ab5
[modules/gui.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 // asv 21.02.05 : introducing multi-platform approach of thread comparison
38 // on Unix using pthread_t type for storing ThreadId
39 // on Win32 using integer type for storing ThreadId
40 // NOT using integer ThreadId on both Unix and Win32 because (from documentation):
41 // "...Do not allow your program to rely on the internal structure or size of the pthread_t..."
42
43 #ifdef WIN32
44 #include <windows.h>
45
46 static DWORD myThread;
47 #else
48 #include <qthread.h>
49 #include <pthread.h>
50
51 static pthread_t myThread;
52 #endif
53
54 void SALOME_Event::GetSessionThread(){
55 #ifdef WIN32
56   myThread = ::GetCurrentThreadId();
57 #else
58   myThread = pthread_self();
59 #endif
60 }
61
62 bool SALOME_Event::IsSessionThread(){
63   bool aResult = false;
64 #ifdef WIN32
65   aResult = myThread == ::GetCurrentThreadId();
66 #else
67   aResult = myThread == pthread_self();
68 #endif
69 //  if(MYDEBUG) INFOS("IsSessionThread() - "<<aResult);
70   return aResult;
71 }
72
73
74 //===========================================================
75 /*!
76  *  SALOME_Event::SALOME_Event
77  *  Constructor
78  */
79 //===========================================================
80 SALOME_Event::SALOME_Event(){
81 //  if(MYDEBUG) MESSAGE( "SALOME_Event::SALOME_Event(): this = "<<this );
82   // Prepare the semaphore 
83   mySemaphore = new QSemaphore( 2 );
84   *mySemaphore += 2;
85 }
86
87 //===========================================================
88 /*!
89  *  SALOME_Event::~SALOME_Event
90  *  Destructor
91  */
92 //===========================================================
93 SALOME_Event::~SALOME_Event(){
94 //  if(MYDEBUG) MESSAGE( "SALOME_Event::~SALOME_Event(): this = "<<this );
95   if ( mySemaphore->available() < mySemaphore->total() )
96     *mySemaphore -= mySemaphore->total() - mySemaphore->available();
97   delete mySemaphore;
98 }
99
100 //===========================================================
101 /*!
102  *  SALOME_Event::process
103  *  Posts the event and optionally waits for its completion
104  */
105 //===========================================================
106 void SALOME_Event::process()
107 {
108   QThread::postEvent( qApp, new QCustomEvent( SALOME_EVENT, (void*)this ) );
109 //  if(MYDEBUG) MESSAGE( "SALOME_Event::process(): this = "<<this<<", *mySemaphore += 1 " );
110   *mySemaphore += 1;
111 //  if(MYDEBUG) MESSAGE( "SALOME_Event::process(): this = "<<this<<" - COMPLETED" );
112 }
113
114 //===========================================================
115 /*!
116  *  SALOME_Event::processed
117  *  Signals that this event has been processed
118  */
119 //===========================================================
120 void SALOME_Event::processed()
121 {
122 //  if(MYDEBUG) MESSAGE( "SALOME_Event::processed(): this = "<<this );
123   // process() takes control over mySemaphore after the next line is executed
124   *mySemaphore -= 1;
125 }