00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 #ifndef OSDL_SCHEDULER_H_
00028 #define OSDL_SCHEDULER_H_
00029 
00030 
00031 
00032 
00033 
00034 #include "OSDLEngineCommon.h"         
00035 #include "OSDLEvents.h"               
00036 #include "OSDLPeriodicSlot.h"         
00037 
00038 
00039 
00040 
00041 #include "Ceylan.h"                   
00042 
00043 
00044 
00045 #include <string>
00046 #include <list>
00047 #include <map>
00048 
00049 
00050 
00051 
00052 namespace OSDL
00053 {
00054 
00055 
00056 
00057     namespace Events
00058     {
00059     
00060     
00061         
00062         class EventsModule ;
00063         
00064     }
00065 
00066 
00067 
00068     namespace Rendering
00069     {
00070     
00071     
00072         
00073         class Renderer ;
00074         
00075     }
00076 
00077     
00078     
00079     namespace Video
00080     {
00081     
00082         
00083 
00084 
00085 
00086 
00087         class VideoModule ;
00088         
00089     }
00090     
00091     
00092     
00093     namespace Engine 
00094     {
00095     
00096         
00097         
00098         
00099         class ActiveObject ;
00100 
00101 
00102 
00103         
00104         class PeriodicSlot ;
00105         
00106         
00107         
00108                 
00265         class OSDL_DLL Scheduler : public Ceylan::Object
00266         {
00267         
00268         
00269             public:
00270             
00271             
00272             
00273                 
00274 
00275 
00276 
00277 
00278                  
00279                  
00280                 
00287                 virtual bool hasRenderer() const ;
00288 
00289 
00290 
00297                 virtual Rendering::Renderer & getRenderer() const ;
00298 
00299 
00300 
00312                 virtual void setRenderer( Rendering::Renderer & newRenderer ) ;
00313                 
00314                 
00315                 
00351                 virtual void setScreenshotMode( bool on, 
00352                     const std::string & frameFilenamePrefix, 
00353                     Ceylan::Maths::Hertz frameFrequency = 25 ) ;
00354                 
00355                 
00356                 
00375                 virtual void setTimeSliceDuration( 
00376                     Ceylan::System::Microsecond engineTickDuration ) ;
00377 
00378 
00379     
00390                 virtual Ceylan::System::Microsecond getTimeSliceDuration() 
00391                     const ;
00392                 
00393                 
00394                 
00408                 virtual void setSimulationFrequency( 
00409                     Ceylan::Maths::Hertz frequency ) ;
00410 
00411 
00412                 
00421                 virtual Events::Period getSimulationTickCount() const ;
00422                 
00423                 
00424                 
00437                 virtual void setRenderingFrequency( 
00438                         Ceylan::Maths::Hertz frequency ) ;
00439 
00440 
00441                 
00450                 virtual Events::Period getRenderingTickCount() const ;
00451             
00452             
00453                 
00467                 virtual void setScreenshotFrequency( 
00468                     Ceylan::Maths::Hertz frequency ) ;
00469 
00470                 
00471                 
00480                 virtual Events::Period getScreenshotTickCount() const ;
00481                 
00482                 
00483                 
00496                 virtual void setInputPollingFrequency( 
00497                     Ceylan::Maths::Hertz frequency ) ;
00498 
00499 
00500 
00509                 virtual Events::Period getInputPollingTickCount() const ;
00510 
00511                 
00512                 
00546                 virtual void setIdleCallback( 
00547                     Ceylan::System::Callback idleCallback = 0 , 
00548                     void * callbackData = 0,
00549                     Ceylan::System::Microsecond callbackExpectedMaxDuration = 0
00550                 ) ;
00551                 
00552                 
00553                 
00558                 virtual Events::EngineTick getCurrentEngineTick() const ;
00559                 
00560                 
00561                 
00566                 virtual Events::SimulationTick getCurrentSimulationTick() 
00567                     const ;
00568                 
00569                 
00570                 
00575                 virtual Events::RenderingTick getCurrentRenderingTick() const ;
00576                 
00577                 
00578                 
00583                 virtual Events::InputTick getCurrentInputTick() const ;
00584                 
00585                 
00586                 
00587                 
00589                 
00590                 
00591                 
00602                 virtual Events::SimulationTick getNumberOfSimulationTicksFor(
00603                     Ceylan::System::Millisecond duration ) const ;
00604                     
00605                     
00606                     
00617                 virtual Events::RenderingTick getNumberOfRenderingTicksFor(
00618                     Ceylan::System::Millisecond duration ) const ;
00619                     
00620                     
00621                     
00632                 virtual Events::InputTick getNumberOfInputTicksFor(
00633                     Ceylan::System::Millisecond duration ) const ;
00634                     
00635                 
00636                 
00637                 
00646                 virtual void registerPeriodicalObject( 
00647                     PeriodicalActiveObject & toRegister ) ;
00648                     
00649                                 
00650                 
00664                 virtual void unregisterPeriodicalObject( 
00665                     PeriodicalActiveObject & toUnregister ) ;
00666 
00667 
00668                 
00677                 virtual void registerProgrammedObject( 
00678                     ProgrammedActiveObject & toRegister ) ;
00679                         
00680                 
00681                         
00694                 virtual void unregisterProgrammedObject( 
00695                     ProgrammedActiveObject & toUnregister ) ;
00696                         
00697                         
00698                         
00707                 virtual void schedule() ;
00708                 
00709                             
00710                                 
00716                 virtual void stop() ;
00717                 
00718                 
00719                 
00732                 virtual void setStopCallback( 
00733                     Ceylan::System::Callback stopCallback = 0 , 
00734                     void * callbackData = 0 ) ;
00735                 
00736                  
00737                                         
00749                 virtual const std::string toString( 
00750                     Ceylan::VerbosityLevels level = Ceylan::high ) const ;
00751 
00752 
00753 
00754 
00755 
00756                 
00757 
00758 
00759 
00772                 static Scheduler & GetExistingScheduler() ;
00773 
00774 
00775             
00789                 static Scheduler & GetScheduler() ;
00790 
00791 
00792 
00799                 static void DeleteExistingScheduler() ;
00800         
00801         
00802         
00807                 static void DeleteScheduler() ;
00808         
00809         
00810         
00823                 static void StopExistingScheduler() ;
00824         
00825     
00826                     
00836                 static const Ceylan::System::Microsecond
00837                     DefaultEngineTickDuration = 1000 ;
00838                     
00839                     
00840                     
00847                 static const Ceylan::Maths::Hertz DefaultSimulationFrequency 
00848                     = 100 ;
00849                     
00850                     
00851                     
00859                 static const Ceylan::Maths::Hertz DefaultRenderingFrequency 
00860                     = 40 ;
00861                     
00862                     
00863                     
00872                 static const Ceylan::Uint32 DefaultMovieFrameFrequency = 25 ;
00873                 
00874                 
00875                 
00883                 static const Ceylan::Maths::Hertz DefaultInputFrequency = 20 ;
00884                     
00885 
00886 
00887 
00888 
00889                     
00890             protected:
00891             
00892 
00893 
00905                 explicit Scheduler() ;
00906                 
00907                 
00908                 
00916                 virtual ~Scheduler() throw() ;
00917 
00918 
00919 
00928                 virtual void scheduleBestEffort() ;
00929                 
00930 
00931 
00946                 virtual void scheduleNoDeadline( bool pollInputs = true ) ;
00947                 
00948                 
00949                 
00964                 virtual Events::EngineTick computeEngineTickFromCurrentTime() ;
00965 
00966 
00967 
00977                 virtual void scheduleSimulation( 
00978                     Events::SimulationTick current ) ;
00979 
00980 
00981 
00991                 virtual void scheduleProgrammedObjects( 
00992                     Events::SimulationTick current ) ;
00993 
00994 
00995 
01005                 virtual void schedulePeriodicObjects( 
01006                     Events::SimulationTick currentSimulationTick ) ;
01007 
01008 
01009 
01016                 virtual void scheduleRendering( 
01017                     Events::RenderingTick currentRenderingTick ) ;
01018                      
01019                      
01020                      
01027                 virtual void scheduleInput( 
01028                     Events::InputTick currentInputTick ) ;
01029 
01030 
01031 
01043                 virtual void scheduleProgrammedObjectList( 
01044                     Events::RenderingTick currentSimulationTick, 
01045                     ListOfProgrammedActiveObjects & objectList ) ;
01046                     
01047                     
01048                     
01070                 virtual void onSimulationSkipped( 
01071                     Events::SimulationTick skipped ) ;
01072                 
01073                 
01074 
01090                 virtual void onRenderingSkipped( 
01091                     Events::RenderingTick skipped ) ;
01092                                 
01093 
01094 
01111                 virtual void onInputSkipped( Events::InputTick skipped ) ;
01112                     
01113                                 
01114                 
01124                 virtual void onIdle() ;
01125 
01126 
01127 
01133                 static const Delay ShutdownBucketLevel ;
01134                 
01135                 
01136                 
01158                 virtual void onScheduleFailure( Delay currentBucket ) ;
01159 
01160 
01161                 
01173                 virtual void programTriggerFor( 
01174                     ProgrammedActiveObject & objectToProgram,
01175                     Events::SimulationTick targetTick ) ;
01176                 
01177 
01178 
01187                 virtual PeriodicSlot & getPeriodicSlotFor( 
01188                     Events::Period period ) ;
01189 
01190 
01191 
01200                 virtual PeriodicSlot & returnPeriodicSlotFor( 
01201                     Events::Period period ) ;
01202                 
01203                 
01204 
01205                 
01206 
01207 
01208 
01209 
01210 
01211 
01212 
01213                 virtual void setInitialBirthTicks(
01214                     Events::SimulationTick birthSimulationTick ) ;
01215                 
01216 
01217 
01223                 std::string describeProgrammedTicks() const ; 
01224 
01225 
01226 
01227 
01228                 
01229                 
01230                 
01231                 
01241                 bool _screenshotMode ;      
01242                 
01243                 
01244                 
01253                 std::string _frameFilenamePrefix ;      
01254 
01255                 
01256                 
01265                 Ceylan::Maths::Hertz _desiredScreenshotFrequency ;
01266                 
01267                 
01268                 
01278                  Events::Period _screenshotPeriod ;     
01279                 
01280 
01281 
01282 
01283 
01284 
01285 
01286 
01287 
01288 #pragma warning( push )
01289 #pragma warning( disable: 4251 )
01290             
01291             
01301                 std::list<PeriodicSlot *> _periodicSlots ; 
01302                 
01303             
01304             
01313                 std::list<ActiveObject *> _initialRegisteredObjects ; 
01314                 
01315                 
01316                 
01341                 std::map<Events::SimulationTick, ListOfProgrammedActiveObjects>
01342                     _programmedActivated ;      
01343                 
01344                 
01345 #pragma warning( pop ) 
01346 
01347                 
01353                 Ceylan::System::Microsecond _engineTickDuration ;
01354                 
01355                 
01356                 
01364                 Ceylan::Uint32 _secondToEngineTick ;
01365                 
01366                 
01367                 
01390                 Events::EngineTick _currentEngineTick ;
01391                 
01392             
01393                 
01415                 Events::SimulationTick _currentSimulationTick ;
01416                 
01417                 
01418                 
01440                 Events::RenderingTick _currentRenderingTick ;
01441                 
01442                 
01443                 
01465                 Events::RenderingTick _currentInputTick ;
01466                 
01467                 
01468                 
01474                 Events::Period _simulationPeriod ;
01475                 
01476                 
01477                 
01483                 Events::Period _renderingPeriod ;
01484                 
01485                 
01486                 
01492                 Events::Period _inputPeriod ;
01493                 
01494                 
01495                 
01503                 Ceylan::Maths::Hertz _desiredSimulationFrequency ;
01504                 
01505                 
01506                 
01514                 Ceylan::Maths::Hertz _desiredRenderingFrequency ;
01515         
01516         
01517         
01525                 Ceylan::Maths::Hertz _desiredInputFrequency ;
01526 
01527 
01528 
01529                 
01530                 
01531                 
01532                 
01533 
01538                 Ceylan::System::Callback _idleCallback ;
01539                         
01540                             
01545                 void * _idleCallbackData ;              
01546         
01547         
01548                 
01557                 Ceylan::System::Microsecond _idleCallbackMaxDuration ;
01558         
01559         
01560         
01566                 Ceylan::System::Microsecond _idleCallbackMinMeasuredDuration ;
01567         
01568         
01569         
01575                 Ceylan::System::Microsecond _idleCallbackMaxMeasuredDuration ;
01576         
01577                 
01583                 Ceylan::Uint32 _idleCallsCount ;
01584         
01585         
01586         
01588                 bool _isRunning ;
01589                 
01590                 
01591                 
01592                 
01593         
01594         
01596                 bool _stopRequested ;
01597         
01598         
01603                 Ceylan::System::Callback _stopCallback ;
01604         
01605                                 
01610                 void * _stopCallbackData ;              
01611         
01612         
01613         
01618                 Ceylan::System::Second _scheduleStartingSecond ;
01619                 
01620                 
01625                 Ceylan::System::Microsecond _scheduleStartingMicrosecond ;      
01626         
01627         
01628         
01634                 Ceylan::Uint32 _recoveredSimulationTicks ;
01635         
01636         
01637         
01643                 Ceylan::Uint32 _missedSimulationTicks ;
01644         
01645         
01646         
01652                 Ceylan::Uint32 _recoveredRenderingTicks ;
01653         
01654         
01655         
01661                 Ceylan::Uint32 _missedRenderingTicks ;
01662         
01663         
01664         
01670                 Ceylan::Uint32 _recoveredInputPollingTicks ;
01671         
01672         
01673         
01679                 Ceylan::Uint32 _missedInputPollingTicks ;
01680         
01681         
01688                 Ceylan::Uint32 _scheduleFailureCount ;
01689         
01690         
01691 
01699                 Events::EventsModule * _eventsModule ;
01700                 
01701                 
01702         
01707                 Rendering::Renderer * _renderer ;
01708         
01709         
01710         
01722                 Video::VideoModule * _videoModule ;
01723                 
01724                 
01725                 
01731                 bool _subSecondSleepsAvailable ;
01732                  
01733                 
01734                 
01735                  
01736             private:
01737             
01738             
01739             
01748                 explicit Scheduler( const Scheduler & source ) ;
01749             
01750             
01751             
01760                 Scheduler & operator = ( const Scheduler & source ) ;
01761                     
01762                             
01763                 
01765                 static Scheduler * _internalScheduler ;
01766             
01767                 
01768         } ;
01769         
01770 
01771     }
01772 
01773 }
01774 
01775 
01776 
01777 #endif // OSDL_SCHEDULER_H_
01778