1. with Locking_Objects;                   use Locking_Objects; 
  2. with Objects;                           use Objects; 
  3. with Processes;                         use Processes; 
  4.  
  5. private with Ada.Containers.Doubly_Linked_Lists; 
  6.  
  7. package Processes.Managers is 
  8.  
  9.     type Process_Manager is new Object with private; 
  10.     type A_Process_Manager is access all Process_Manager'Class; 
  11.  
  12.     -- Reference to a procedure that is responsible for destroying a process. 
  13.     type A_Destructor is access procedure( process : in out A_Process ); 
  14.  
  15.     -- An update rate of 0 hertz means maximum update rate, without delay 
  16.     -- between frames. 'minHertz' is not the minimum actual rate of the process 
  17.     -- manager because it is unable to interupt or skip processes; it is the 
  18.     -- minimum time delta reported to the processes on tick. If some processes 
  19.     -- can't handle large time deltas but sometimes they do happen, 'minHertz' 
  20.     -- will prevent the processes from getting a dt that's too big. 
  21.     function Create_Process_Manager( hertz    : Natural := 0; 
  22.                                      minHertz : Natural := 0 ) return A_Process_Manager; 
  23.     pragma Postcondition( Create_Process_Manager'Result /= null ); 
  24.  
  25.     -- Attach a process to the process manager for execution. It will be 
  26.     -- executed last in the rotation of attached processes. This can be done 
  27.     -- before or after starting the process manager. 
  28.     procedure Attach( this : not null access Process_Manager'Class; 
  29.                       proc : not null A_Process ); 
  30.  
  31.     -- Detaches a process from the process manager. This can be done at any time. 
  32.     procedure Detach( this       : not null access Process_Manager'Class; 
  33.                       proc       : not null A_Process; 
  34.                       destructor : A_Destructor := null ); 
  35.  
  36.     -- Returns the rate in Hz that the process manager is ticking its processes. 
  37.     function Get_Rate( this : not null access Process_Manager'Class ) return Natural; 
  38.  
  39.     -- Pauses/resumes the given process if it's attached to this manager. If 
  40.     -- paused, Tick will not be called until after the process has been resumed. 
  41.     procedure Pause( this   : not null access Process_Manager'Class; 
  42.                      proc   : not null A_Process; 
  43.                      paused : Boolean ); 
  44.  
  45.     -- Starts execution of the processes. 
  46.     procedure Start( this : not null access Process_Manager'Class ); 
  47.  
  48.     -- Stops execution of the processes. 
  49.     procedure Stop( this : not null access Process_Manager'Class ); 
  50.  
  51.     -- Instructs the process manager to track the rate at which it is executing 
  52.     -- its processes. This is useful for determining frame rates or finding 
  53.     -- which process manager threads are running too slowly. 
  54.     procedure Track_Rate( this    : not null access Process_Manager'Class; 
  55.                           enabled : Boolean ); 
  56.  
  57.     -- Deletes a process manager. Its attached processes, if it has any at the 
  58.     -- time of deletion, are detached and left unchanged. 
  59.     procedure Delete( this : in out A_Process_Manager ); 
  60.     pragma Postcondition( this = null ); 
  61.  
  62. private 
  63.  
  64.     type Execution is 
  65.         record 
  66.             process   : A_Process := null; 
  67.             firstTick : Time := Time_First; 
  68.             lastTick  : Time := Time_First; 
  69.             paused    : Boolean := False; 
  70.             pauseTime : Time := Time_First; 
  71.         end record; 
  72.     type A_Execution is access all Execution; 
  73.  
  74.     procedure Delete( exec : in out A_Execution ); 
  75.  
  76.     function Eq( l, r : A_Execution ) return Boolean; 
  77.  
  78.     package Execution_Lists is new Ada.Containers.Doubly_Linked_Lists( A_Execution, Eq ); 
  79.     use Execution_Lists; 
  80.  
  81.     ---------------------------------------------------------------------------- 
  82.  
  83.     type Operation is abstract tagged 
  84.         record 
  85.             process : A_Process := null; 
  86.         end record; 
  87.     type A_Operation is access all Operation'Class; 
  88.  
  89.     procedure Execute( this  : access Operation; 
  90.                        plist : in out Execution_Lists.List ) is abstract; 
  91.  
  92.     procedure Delete( this : in out A_Operation ); 
  93.  
  94.     type Attach_Operation is new Operation with null record; 
  95.  
  96.     procedure Execute( this     : access Attach_Operation; 
  97.                        execList : in out Execution_Lists.List ); 
  98.  
  99.     type Detach_Operation is new Operation with 
  100.         record 
  101.             destroy : A_Destructor := null; 
  102.         end record; 
  103.  
  104.     procedure Execute( this     : access Detach_Operation; 
  105.                        execList : in out Execution_Lists.List ); 
  106.  
  107.     type Pause_Operation is new Operation with null record; 
  108.  
  109.     procedure Execute( this     : access Pause_Operation; 
  110.                        execList : in out Execution_Lists.List ); 
  111.  
  112.     type Resume_Operation is new Operation with null record; 
  113.  
  114.     procedure Execute( this     : access Resume_Operation; 
  115.                        execList : in out Execution_Lists.List ); 
  116.  
  117.     function Create_Attach_Operation( process : not null A_Process ) return A_Operation; 
  118.  
  119.     function Create_Detach_Operation( process    : not null A_Process; 
  120.                                       destructor : A_Destructor ) return A_Operation; 
  121.  
  122.     function Create_Pause_Operation( process : not null A_Process ) return A_Operation; 
  123.  
  124.     function Create_Resume_Operation( process : not null A_Process ) return A_Operation; 
  125.  
  126.     package Operation_Lists is new Ada.Containers.Doubly_Linked_Lists( A_Operation, "=" ); 
  127.  
  128.     ---------------------------------------------------------------------------- 
  129.  
  130.     task type Ticker_Task is 
  131.         entry Init( pman : A_Process_Manager; hertz, minHertz : Natural ); 
  132.         entry Start; 
  133.         entry Stop; 
  134.     end Ticker_Task; 
  135.     type A_Ticker_Task is access all Ticker_Task; 
  136.  
  137.     procedure Delete( ticker : in out A_Ticker_Task ); 
  138.  
  139.     ---------------------------------------------------------------------------- 
  140.  
  141.     type Process_Manager is new Object with 
  142.         record 
  143.             lock       : A_Locking_Object;       -- protects all other fields 
  144.             operations : Operation_Lists.List; 
  145.             trackRate  : Boolean := False; 
  146.             rate       : Natural := 0; 
  147.             ticker     : A_Ticker_Task := null; 
  148.         end record; 
  149.  
  150.     -- Raises COPY_NOT_ALLOWED. 
  151.     procedure Adjust( this : access Process_Manager ); 
  152.  
  153.     procedure Construct( this     : access Process_Manager; 
  154.                          hertz    : Natural; 
  155.                          minHertz : Natural ); 
  156.  
  157.     procedure Delete( this : in out Process_Manager ); 
  158.  
  159. end Processes.Managers;