Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

PsnMultiThreadedAsynchronousScheduler.cxx

Go to the documentation of this file.
00001 /*
00002  * This file is part of openMask © INRIA, CNRS, Universite de Rennes 1 1993-2002, thereinafter the Software
00003  * 
00004  * The Software has been developped within the Siames Project. 
00005  * INRIA, the University of Rennes 1 and CNRS jointly hold intellectual property rights
00006  * 
00007  * The Software has been registered with the Agence pour la Protection des
00008  * Programmes (APP) under registration number IDDN.FR.001.510008.00.S.P.2001.000.41200
00009  *  
00010  * This file may be distributed under the terms of the Q Public License
00011  * version 1.0 as defined by Trolltech AS of Norway and appearing in the file
00012  * LICENSE.QPL included in the packaging of this file.
00013  *
00014  * Licensees holding valid specific licenses issued by INRIA, CNRS or Université de Rennes 1 
00015  * for the software may use this file in accordance with that specific license
00016  *
00017  */
00018 #include "PsnMultiThreadedAsynchronousScheduler.h"
00019 
00020 #include "PsnReferenceObjectHandle.h"
00021 #include "PsUnInitialisedAttributeException.h"
00022 #include "PsSimulatedObject.h"
00023 #include "PsController.h"
00024 #include "unistd.h"
00025 
00026 PsnMultiThreadedAsynchronousScheduler::PsnMultiThreadedAsynchronousScheduler(unsigned int numberOfSteps, 
00027                                                                                    unsigned int numberOfThreads) :
00028    PsnMultiThreadedScheduler(numberOfSteps, numberOfThreads,true),
00029    _running (false),
00030    _started (false),
00031    _nameOfAsynchronousObject("visu")
00032 {
00033    cerr<<"Warning : using a experimental scheduler. UNSUPPORTED"<<endl;
00034    cerr<<"Only supports ONE asynchronous object"<<endl;
00035 }
00036 
00037 PsnMultiThreadedAsynchronousScheduler::~PsnMultiThreadedAsynchronousScheduler() {
00038 }
00039 
00040 void PsnMultiThreadedAsynchronousScheduler::addToScheduable (PsnReferenceObjectHandle * ref, unsigned int when) {
00041    if (ref->getSimulatedObject().getObjectDescriptor().getName() == _nameOfAsynchronousObject) {
00042       _asyncRef = ref ;
00043       setComputingState (ref,PsnReferenceObjectHandle::running ) ; 
00044       int ret = pthread_create(&_asynchronousThreadId,
00045                                0,
00046                                (void *(*)(void *))_asynchronousThreadContainer,
00047                                this) ;
00048        if (ret) {
00049          PsController::error("Couldn't create asynchronous thread") ;
00050       }
00051    }
00052    else {
00053       PsnMultiThreadedScheduler::addToScheduable ( ref , when );
00054    }
00055 }
00056 
00057 
00058 
00059 void PsnMultiThreadedAsynchronousScheduler::removeFromScheduable (PsnReferenceObjectHandle * ref) {
00060    if ( ref == _asyncRef ) {
00061       //nothing to do : the asynchronous thread has been stopped. destroying it would be to painfull code to write ! 
00062    }
00063    else {
00064       PsnMultiThreadedScheduler::removeFromScheduable ( ref );
00065    }
00066 } ;
00067 
00068 
00069 
00070 void PsnMultiThreadedAsynchronousScheduler::schedule(PsnReferenceObjectHandle * ref) {
00071    if ( ref == _asyncRef ) {
00072       _runDataAccess.protect() ;
00073       _running = true ;
00074       _runDataAccess.unprotect () ;
00075       _runCondition.signalChange() ;
00076    }
00077    else {
00078       PsnMultiThreadedScheduler::schedule ( ref );
00079    }
00080 }
00081 
00082 
00083 
00084 void PsnMultiThreadedAsynchronousScheduler::unschedule(PsnReferenceObjectHandle * ref) {
00085    if ( ref == _asyncRef ) {
00086       _runDataAccess.protect () ;
00087       _running = false ;
00088       _runDataAccess.unprotect () ;
00089       _runCondition.signalChange() ;
00090    }
00091    else {
00092       PsnMultiThreadedScheduler::unschedule ( ref );
00093    }
00094 }
00095 
00096 void PsnMultiThreadedAsynchronousScheduler::runStep (unsigned int which) {
00097    _runDataAccess.protect() ;
00098    if (!_started) {
00099       _started = true ;
00100       _runDataAccess.unprotect() ;
00101       _runCondition.signalChange() ;
00102    }
00103    else {
00104       _runDataAccess.unprotect() ;      
00105    }
00106    PsnMultiThreadedScheduler::runStep( which ) ;
00107 }
00108 
00109 
00110 
00111 void * PsnMultiThreadedAsynchronousScheduler::_asynchronousThreadContainer ( void * theScheduler) {
00112    PsnMultiThreadedAsynchronousScheduler * scheduler = (PsnMultiThreadedAsynchronousScheduler *) theScheduler ;
00113 
00114    scheduler->runAsynchronous() ;
00115 
00116    return scheduler ;
00117 }
00118 
00119 
00120 
00121 void PsnMultiThreadedAsynchronousScheduler::runAsynchronous() {
00122    bool init = false ;
00123    _runDataAccess.protect() ;
00124    while (!_started) {
00125          _runDataAccess.unprotect() ;
00126          _runCondition.waitForChange( _runDataAccess ) ;
00127    }
00128    
00129    _runDataAccess.unprotect() ;
00130 
00131    assert (false ) ;
00132    //_asyncRef->getSimulatedObject().init() ;
00133    
00134    while (true) {
00135       _runDataAccess.protect() ;
00136 
00137       while (!(_running && _started) ) {
00138          _runDataAccess.unprotect() ;
00139          _runCondition.waitForChange( _runDataAccess ) ;
00140          
00141       }
00142 
00143       _runDataAccess.unprotect() ;   
00144 
00145       try {
00146 
00147          assert (false) ;
00148          //_asyncRef->getSimulatedObject().compute() ;
00149 
00150       }
00151       catch (PsUnInitialisedAttributeException & e) {
00152          _runDataAccess.protect() ;
00153          _started = false ;
00154          _runDataAccess.unprotect() ;
00155          _runCondition.signalChange() ;
00156       }
00157    }
00158 }
00159 

logo OpenMask

Documentation generated on Mon Nov 25 15:25:00 2002

Generated with doxygen 1.2.12 by Dimitri van Heesch ,   1997-2001