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. private with Ada.Strings.Unbounded; 
  7.  
  8. package Processes.Managers is 
  9.  
  10.     -- A Process_Manager executes Process objects at fixed periodic intervals. 
  11.     -- Each Process_Manager is backed by a single internal thread, with which it 
  12.     -- delegates execution time to attached Process objects in time slices 
  13.     -- (execution frames). Each Process is given a chance to execute via its 
  14.     -- Tick method and is called at an approximate frequency determined by the 
  15.     -- Process_Manager. 
  16.     -- 
  17.     -- Hard time constraints are not guaranteed because any Process taking up 
  18.     -- too much time will slow down the Process_Manager's tick rate. 
  19.     type Process_Manager is new Limited_Object with private; 
  20.     type A_Process_Manager is access all Process_Manager'Class; 
  21.  
  22.     -- Reference to a procedure that is responsible for destroying a process. 
  23.     type A_Destructor is access procedure( process : in out A_Process ); 
  24.  
  25.     -- An update rate of 0 hertz means maximum update rate, without delay 
  26.     -- between frames. 'minHertz' is not the minimum actual rate of the process 
  27.     -- manager because it is unable to interrupt or skip processes; it is the 
  28.     -- minimum time delta reported to the processes on tick. If some processes 
  29.     -- can't handle large time deltas but sometimes they do happen, 'minHertz' 
  30.     -- will prevent the processes from getting a dt that's too big. 
  31.     function Create_Process_Manager( name     : String; 
  32.                                      hertz    : Natural := 0; 
  33.                                      minHertz : Natural := 0 ) return A_Process_Manager; 
  34.     pragma Postcondition( Create_Process_Manager'Result /= null ); 
  35.  
  36.     -- Asynchronously attaches a process to the process manager for execution. 
  37.     -- It will be executed last in the rotation of attached processes. This can 
  38.     -- be called at any time before Stop. Constraint_Error will be raised if the 
  39.     -- process manager has been stopped. 
  40.     procedure Attach_Async( this : not null access Process_Manager'Class; 
  41.                             proc : not null A_Process ); 
  42.  
  43.     -- Asynchronously detaches a Process from the process manager. This can be 
  44.     -- called at any time before Stop. 
  45.     -- 
  46.     -- Note that 'prop' cannot be immediately detached because it could be 
  47.     -- executing. While the process manager is running, it is not safe for the 
  48.     -- caller to call Detach and then delete 'proc', without additional 
  49.     -- synchronization. 
  50.     procedure Detach_Async( this : not null access Process_Manager'Class; 
  51.                             proc : not null A_Process ); 
  52.  
  53.     -- Asynchronously detaches a Process from the process manager. This can be 
  54.     -- called at any time. The 'destructor' procedure will be used to delete 
  55.     -- 'proc' after it is fully detached from the process manager. 'proc' is 
  56.     -- consumed. 
  57.     procedure Detach_Async( this       : not null access Process_Manager'Class; 
  58.                             proc       : in out A_Process; 
  59.                             destructor : not null A_Destructor ); 
  60.     pragma Postcondition( proc = null ); 
  61.  
  62.     -- Returns the name of the process manager as given at creation. 
  63.     function Get_Name( this : not null access Process_Manager'Class ) return String; 
  64.  
  65.     -- Returns the actual rate in Hz that the process manager is ticking its 
  66.     -- processes, if rate tracking is enabled. If rate tracking is not enabled, 
  67.     -- then 0 will be returned. 
  68.     function Get_Rate( this : not null access Process_Manager'Class ) return Natural; 
  69.  
  70.     -- Asynchronously pauses/resumes the given Process if it's attached to this 
  71.     -- manager. If paused, Tick will not be called until after the Process has 
  72.     -- been resumed. Constraint_Error will be raised if the process manager has 
  73.     -- been stopped. 
  74.     procedure Pause_Async( this   : not null access Process_Manager'Class; 
  75.                            proc   : not null A_Process; 
  76.                            paused : Boolean ); 
  77.  
  78.     -- Starts execution of the processes. 
  79.     procedure Start( this : not null access Process_Manager'Class ); 
  80.  
  81.     -- Stops execution of the processes. 
  82.     procedure Stop( this : not null access Process_Manager'Class ); 
  83.  
  84.     -- Instructs the process manager to track the rate at which it is executing 
  85.     -- processes. This is useful for determining frame rates or finding which 
  86.     -- process manager threads are running too slowly. Note that rate tracking 
  87.     -- does impose some overhead and should only be enabled for debugging 
  88.     -- purposes. 
  89.     procedure Track_Rate( this    : not null access Process_Manager'Class; 
  90.                           enabled : Boolean ); 
  91.  
  92.     -- Deletes a process manager. Its attached processes, if it has any at the 
  93.     -- time of deletion, are detached and left unchanged. This may result in a 
  94.     -- leak if the processes are not deleted elsewhere. 
  95.     procedure Delete( this : in out A_Process_Manager ); 
  96.     pragma Postcondition( this = null ); 
  97.  
  98. private 
  99.  
  100.     use Ada.Strings.Unbounded; 
  101.  
  102.     -- The execution context of a Process. 
  103.     type Execution is 
  104.         record 
  105.             process   : A_Process := null; 
  106.             firstTick : Time := Time_First; 
  107.             lastTick  : Time := Time_First; 
  108.             paused    : Boolean := False; 
  109.             pauseTime : Time := Time_First; 
  110.         end record; 
  111.     type A_Execution is access all Execution; 
  112.  
  113.     -- Deletes the Execution. 
  114.     procedure Delete( exec : in out A_Execution ); 
  115.  
  116.     -- Compares 'l' and 'r' by their Process. 
  117.     function Eq( l, r : A_Execution ) return Boolean; 
  118.  
  119.     -- An ordered list of Execution contexts that the Ticker_Task repeatedly 
  120.     -- iterates to tick processes. 
  121.     package Execution_Lists is new Ada.Containers.Doubly_Linked_Lists( A_Execution, Eq ); 
  122.     use Execution_Lists; 
  123.  
  124.     ---------------------------------------------------------------------------- 
  125.  
  126.     -- An Operation represents a queued asynchronous operation involving a 
  127.     -- Process: Attach, Detach, Pause, etc. All of these operations are 
  128.     -- asynchronous so they can be performed by a Process in its Tick method. 
  129.     type Operation is abstract tagged 
  130.         record 
  131.             process : A_Process := null; 
  132.         end record; 
  133.     type A_Operation is access all Operation'Class; 
  134.  
  135.     -- Executes the Operation on the process manager's Process list. 
  136.     procedure Execute( this  : access Operation; 
  137.                        plist : in out Execution_Lists.List ) is abstract; 
  138.  
  139.     -- Deletes the Operation. 
  140.     procedure Delete( this : in out A_Operation ); 
  141.  
  142.     -- A list of Operations to be used as a queue. 
  143.     package Operation_Lists is new Ada.Containers.Doubly_Linked_Lists( A_Operation, "=" ); 
  144.  
  145.     -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
  146.  
  147.     -- Appends a process to the execution list. 
  148.     type Attach_Operation is new Operation with null record; 
  149.  
  150.     procedure Execute( this     : access Attach_Operation; 
  151.                        execList : in out Execution_Lists.List ); 
  152.  
  153.     -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
  154.  
  155.     -- Removes a process from the execution list. 
  156.     type Detach_Operation is new Operation with 
  157.         record 
  158.             destroy : A_Destructor := null; 
  159.         end record; 
  160.  
  161.     -- Detaches the operation's process from 'execList'. If a destructor was 
  162.     -- provided, it will be called with the detached process. 
  163.     procedure Execute( this     : access Detach_Operation; 
  164.                        execList : in out Execution_Lists.List ); 
  165.  
  166.     -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
  167.  
  168.     -- Pauses the execution of a process before its next Tick. 
  169.     type Pause_Operation is new Operation with null record; 
  170.  
  171.     procedure Execute( this     : access Pause_Operation; 
  172.                        execList : in out Execution_Lists.List ); 
  173.  
  174.     -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
  175.  
  176.     -- Resumes the execution of a process on its next Tick. 
  177.     type Resume_Operation is new Operation with null record; 
  178.  
  179.     procedure Execute( this     : access Resume_Operation; 
  180.                        execList : in out Execution_Lists.List ); 
  181.  
  182.     -- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
  183.  
  184.     function Create_Attach_Operation( process : not null A_Process ) return A_Operation; 
  185.  
  186.     -- If 'destructor' is not null, the detached process will be passed to it. 
  187.     function Create_Detach_Operation( process    : not null A_Process; 
  188.                                       destructor : A_Destructor ) return A_Operation; 
  189.  
  190.     function Create_Pause_Operation( process : not null A_Process ) return A_Operation; 
  191.  
  192.     function Create_Resume_Operation( process : not null A_Process ) return A_Operation; 
  193.  
  194.     ---------------------------------------------------------------------------- 
  195.  
  196.     -- The internal task that runs the Process.Tick procedures. Each process 
  197.     -- manager has exactly one Ticker_Task. 
  198.     task type Ticker_Task is 
  199.         entry Init( pman : A_Process_Manager; hertz, minHertz : Natural ); 
  200.         entry Start; 
  201.         entry Stop; 
  202.     end Ticker_Task; 
  203.     type A_Ticker_Task is access all Ticker_Task; 
  204.  
  205.     -- Deletes the Ticker_Task. 
  206.     procedure Delete( ticker : in out A_Ticker_Task ); 
  207.  
  208.     ---------------------------------------------------------------------------- 
  209.  
  210.     type Process_Manager is new Limited_Object with 
  211.         record 
  212.             lock       : A_Locking_Object;       -- protects all other fields 
  213.             name       : Unbounded_String; 
  214.             operations : Operation_Lists.List; 
  215.             trackRate  : Boolean := False; 
  216.             rate       : Natural := 0; 
  217.             ticker     : A_Ticker_Task := null; 
  218.             started    : Boolean := False; 
  219.             stopped    : Boolean := False; 
  220.         end record; 
  221.  
  222.     procedure Construct( this     : access Process_Manager; 
  223.                          name     : String; 
  224.                          hertz    : Natural; 
  225.                          minHertz : Natural ); 
  226.  
  227.     procedure Delete( this : in out Process_Manager ); 
  228.  
  229. end Processes.Managers;