home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / ada / 2405 < prev    next >
Encoding:
Text File  |  1992-08-22  |  2.7 KB  |  128 lines

  1. Newsgroups: comp.lang.ada
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!news.sei.cmu.edu!rmo
  3. From: rmo@sei.cmu.edu (Robert Ollerton)
  4. Subject: Re: Dynamic Task Creation/Control
  5. Message-ID: <1992Aug22.140034.17727@sei.cmu.edu>
  6. Sender: netnews@sei.cmu.edu (Netnews)
  7. Organization: Software Engineering Institute
  8. References: <1992Aug20.174857.3246@afit.af.mil>
  9. Date: Sat, 22 Aug 1992 14:00:34 GMT
  10. Lines: 116
  11.  
  12.      I encountered the problem of dynamic task creation/destruction in
  13. the development of the Ada Process-Oriented Simulation Library (APOSL).
  14. The essence of the APOSL solution is to encapsulate the task logic within
  15. a loop that stops and starts task execution.  
  16.      All tasks in this system are created through the evaluation of 
  17. allocators declared in the outer scope of library-level packages.  The 
  18. following semi-code is a simplification of the this approach.
  19.  
  20.      Hope this helps.
  21.  
  22. ------
  23.  
  24. package Task_Pkg is
  25.  
  26.   type Task_Ptr is
  27.     private;
  28.  
  29.   procedure Create
  30.     ( The_Task : out Task_Ptr );
  31.  
  32.   procedure Destroy
  33.     ( The_Task : in out Task_Ptr );
  34.  
  35.   procedure Entry_1
  36.     ( The_Task : in Task_Ptr );
  37.  
  38.   procedure Entry_2;
  39.     ( The_Task : in Task_Ptr );
  40.  
  41.   pragma Inline (Entry_1, Entry_2);
  42.       
  43. private
  44.  
  45.   task body Reusable_Task is
  46.     entry Start_Executing;
  47.     entry Stop_Executing;
  48.     entry Entry_1;
  49.     entry Entry_2;
  50.   end Reusable_Task;
  51.  
  52.   type Task_Ptr is
  53.     access Reusable_Task;
  54.  
  55. end Task_Pkg;
  56.  
  57. package body Task_Pkg is
  58.  
  59.   Storage
  60.     : Stack_Of_Task_Ptr;
  61.  
  62.   procedure Create
  63.       ( The_Task : out Task_Ptr ) is
  64.     New_Task
  65.       : Task_Ptr;
  66.   begin
  67.     if Empty(Storage) then
  68.       New_Task := new Example;
  69.     else
  70.       Pop(Storage,New_Task);
  71.     end if;
  72.     New_Task.Start_Executing;
  73.     The_Task := New_Task;
  74.   end Create;
  75.  
  76.   procedure Destroy
  77.     ( The_Task : in out Task_Ptr ) is
  78.   begin
  79.     The_Task.Stop_Executing;
  80.     Push(Storage,The_Task);
  81.     The_Task := null;
  82.   end Destroy;
  83.  
  84.   procedure Entry_1
  85.     ( The_Task : in Task_Ptr ) is
  86.   begin
  87.     The_Task.Entry_1;
  88.   end Entry_1;
  89.  
  90.   procedure Entry_2
  91.     ( The_Task : in Task_Ptr ) is
  92.   begin
  93.     The_Task.Entry_2;
  94.   end Entry_2;
  95.  
  96.   task body Reusable_Task is
  97.   begin
  98.     loop
  99.       entry Start_Executing;
  100.       loop
  101.         select 
  102.           accept Stop_Executing;
  103.           exit;
  104.         or
  105.           accept Entry_1;
  106.            -- Task Entry Logic
  107.         or
  108.           accept Entry_2;
  109.            -- Task Entry Logic
  110.         end select;
  111.       end loop;
  112.     end loop;
  113.   end Reusable_Task;
  114.  
  115. end Task_Pkg;
  116.  
  117. -----
  118.  
  119. These opinions are my own and nobody else's.
  120.  
  121. Robert Ollerton
  122. Navy Resident to SEI
  123. Naval Command, Control and Ocean Surveilance Center Research, Development
  124. Test and Evaluation Division
  125.  
  126. ollerton@nosc.mil
  127. rmo@sei.cmu.edu
  128.