Back to index

salome-gui  6.5.0
SALOME_Event.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 //  KERNEL SALOME_Event : Define event posting mechanism
00024 //  File   : SALOME_Event.cxx
00025 //  Author : Sergey ANIKIN
00026 //
00027 #include "SALOME_Event.h"
00028 
00029 #include <QSemaphore>
00030 #include <QApplication>
00031 
00032 // asv 21.02.05 : introducing multi-platform approach of thread comparison
00033 // - on Unix using pthread_t type for storing ThreadId
00034 // - on Win32 using integer type for storing ThreadId
00035 // NOT using integer ThreadId on both Unix and Win32 because (from documentation):
00036 // "...Do not allow your program to rely on the internal structure or size of the pthread_t..."
00037 
00038 #ifdef WIN32
00039 #include <windows.h>
00040 static DWORD myThread;
00041 #else
00042 #include <pthread.h>
00043 static pthread_t myThread;
00044 #endif
00045 
00051 class InitEvent : public SALOME_Event
00052 {
00053 public:
00054   InitEvent();
00055   virtual      ~InitEvent();
00056   virtual void Execute();
00057 };
00058 
00063 InitEvent::InitEvent()
00064 {
00065   GetSessionThread();
00066 }
00067 
00071 InitEvent::~InitEvent()
00072 {
00073 }
00074 
00078 void InitEvent::Execute()
00079 {
00080 }
00081 
00082 // NOTE: Here the SALOME event mechanism is initalized by the 
00083 // current thread ID that is always assumed to be the main thread ID.
00084 // This should be revised as soon as the application library is no longer
00085 // linked against the Event library (i.e. this static object is not created or created 
00086 // outside the main thread).
00087 static InitEvent myInitEvent;
00088 
00103 SALOME_CustomEvent::SALOME_CustomEvent( int type )
00104 : QEvent( (QEvent::Type)type ), d( 0 )
00105 {
00106 }
00107 
00113 SALOME_CustomEvent::SALOME_CustomEvent( QEvent::Type type, void* data )
00114 : QEvent( type ), d( data )
00115 {
00116 }
00117 
00122 void* SALOME_CustomEvent::data() const
00123 {
00124   return d;
00125 }
00126 
00131 void SALOME_CustomEvent::setData( void* data )
00132 {
00133   d = data;
00134 }
00135 
00171 
00172 const int NumberOfResources = 2;
00173 
00180 void SALOME_Event::GetSessionThread(){
00181 #ifdef WIN32
00182   myThread = ::GetCurrentThreadId();
00183 #else
00184   myThread = pthread_self();
00185 #endif
00186 }
00187 
00192 bool SALOME_Event::IsSessionThread(){
00193   bool aResult = false;
00194 #ifdef WIN32
00195   aResult = myThread == ::GetCurrentThreadId();
00196 #else
00197   aResult = myThread == pthread_self();
00198 #endif
00199   return aResult;
00200 }
00201 
00205 SALOME_Event::SALOME_Event(){
00206   // Prepare the semaphore 
00207   mySemaphore = new QSemaphore( NumberOfResources );
00208   mySemaphore->acquire( NumberOfResources );
00209 }
00210 
00214 SALOME_Event::~SALOME_Event(){
00215   if ( mySemaphore->available() < NumberOfResources )
00216     mySemaphore->release( NumberOfResources - mySemaphore->available() );
00217   delete mySemaphore;
00218 }
00219 
00226 void SALOME_Event::ExecutePostedEvent()
00227 {
00228   // Diagnose incorrect usage of SALOME_Event API
00229   if ( !IsSessionThread() ){
00230     qWarning( "SALOME_Event::ExecutePostedEvent() is called from a secondary thread that might mean an error in application logic!" );
00231   }
00232   // Actual execution specific for particular kind of event
00233   Execute();
00234   // Signal the calling thread that the event has been processed
00235   processed();
00236 }
00237 
00243 void SALOME_Event::process()
00244 {
00245   // Diagnose incorrect usage of SALOME_Event API
00246   if ( IsSessionThread() ){
00247     qWarning( "SALOME_Event::process() is called from the main GUI thread that might mean an error in application logic!" );
00248   }
00249 
00250   QApplication::postEvent( qApp, new SALOME_CustomEvent( SALOME_EVENT, (void*)this ) );
00251   mySemaphore->acquire( 1 );
00252 }
00253 
00257 void SALOME_Event::processed()
00258 {
00259   mySemaphore->release( 1 );
00260 }
00261