Salome HOME
Join modifications from branch BR_DEBUG_3_2_0b1
[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.salome-platform.org/ or email : webmaster.salome@opencascade.com
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 /*!
55   \return thread id
56 */
57 void SALOME_Event::GetSessionThread(){
58 #ifdef WIN32
59   myThread = ::GetCurrentThreadId();
60 #else
61   myThread = pthread_self();
62 #endif
63 }
64
65 /*!
66   \return true if it is session thread
67 */
68 bool SALOME_Event::IsSessionThread(){
69   bool aResult = false;
70 #ifdef WIN32
71   aResult = myThread == ::GetCurrentThreadId();
72 #else
73   aResult = myThread == pthread_self();
74 #endif
75 //  if(MYDEBUG) INFOS("IsSessionThread() - "<<aResult);
76   return aResult;
77 }
78
79
80 /*!
81   Constructor
82 */
83 SALOME_Event::SALOME_Event(){
84 //  if(MYDEBUG) MESSAGE( "SALOME_Event::SALOME_Event(): this = "<<this );
85   // Prepare the semaphore 
86   mySemaphore = new QSemaphore( 2 );
87   *mySemaphore += 2;
88 }
89
90 /*!
91   Destructor
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   Posts the event and optionally waits for its completion
102 */
103 void SALOME_Event::process()
104 {
105   QThread::postEvent( qApp, new QCustomEvent( SALOME_EVENT, (void*)this ) );
106 //  if(MYDEBUG) MESSAGE( "SALOME_Event::process(): this = "<<this<<", *mySemaphore += 1 " );
107   *mySemaphore += 1;
108 //  if(MYDEBUG) MESSAGE( "SALOME_Event::process(): this = "<<this<<" - COMPLETED" );
109 }
110
111 /*!
112   Signals that this event has been processed
113 */
114 void SALOME_Event::processed()
115 {
116 //  if(MYDEBUG) MESSAGE( "SALOME_Event::processed(): this = "<<this );
117   // process() takes control over mySemaphore after the next line is executed
118   *mySemaphore -= 1;
119 }