* Limiting Task Counts
@ 2001-12-16 2:27 James Rogers
0 siblings, 0 replies; only message in thread
From: James Rogers @ 2001-12-16 2:27 UTC (permalink / raw)
Eric,
Today, while waiting in airports for various delayed flights on my
way home, I came up with the following example code.
This code assumes that your operating system in limited in the number
of tasks you can operate at one time. The code shows a couple of
approaches to have long lived threads, and replace them with new
threads when they are complete.
The examples also show the use of the Ada.Finalization package for
memory management. I thought the combination of these features would
make good examples for your Ada study.
Note that all task types are limited. They have no assignment or
equality operators automatically defined for them.
Jim Rogers
-- package Task_Counter
--
-- Utility to determine how many tasks can be started at once
with Ada.Finalization;
package Task_Counter is
type Controlled_Task is new
Ada.Finalization.Limited_Controlled with private;
procedure Initialize(Object : in out Controlled_Task);
procedure Finalize(Object : in out Controlled_Task);
function Is_Done(Item : in Controlled_Task) return boolean;
type Task_Access is access Controlled_Task;
function Task_Count return Natural;
private
task type Long_Task is
entry Start;
end Long_Task;
type Long_Access is access Long_Task;
type Controlled_Task is new Ada.Finalization.Limited_Controlled
with record
Thread : Long_Access;
end record;
end Task_Counter;
-----------
-- package body for Task_Counter
----------
with Ada.Text_Io;
with Ada.Unchecked_Deallocation;
package body Task_Counter is
protected Tally is
procedure Increment;
procedure Decrement;
function Count return Natural;
private
The_Tally : Natural := 0;
end Tally;
protected body Tally is
procedure Increment is
begin
The_Tally := The_Tally + 1;
end Increment;
procedure Decrement is
begin
The_Tally := The_Tally - 1;
end Decrement;
function Count return Natural is
begin
return The_Tally;
end Count;
end Tally;
protected Counter is
procedure Next_Count(Count : out Natural);
private
The_Count : Natural := 0;
end Counter;
protected body Counter is
procedure Next_Count(Count : out Natural) is
begin
The_Count := The_Count + 1;
Count := The_Count;
end Next_Count;
end Counter;
task body Long_Task is
My_Count : Natural;
begin
Counter.Next_Count(My_Count);
accept Start;
Ada.Text_Io.Put_Line("Task #" &
Natural'Image(My_Count));
delay 10.0;
Tally.Decrement;
end Long_task;
procedure Initialize(Object : in out Controlled_Task) is
begin
Object.Thread := new Long_Task;
Tally.Increment;
Object.Thread.Start;
end Initialize;
procedure Finalize(Object : in out Controlled_Task) is
procedure free is new
Ada.Unchecked_Deallocation(Long_Task, Long_Access);
begin
if Object.Thread /= null then
free(Object.Thread);
end if;
end Finalize;
function Is_Done(Item : in Controlled_Task) return boolean is
begin
return Item.Thread'Terminated;
end Is_Done;
function Task_Count return Natural is
begin
return Tally.Count;
end Task_Count;
end Task_Counter;
------------------------
-- Procedure Count_Tasks
-- Used to demonstrate how the Task_Counter package works
------------------------
with Task_Counter;
with Ada.Text_Io;
procedure Count_Tasks is
type Tasks is array(1..248) of Task_Counter.Controlled_Task;
begin
-- Declare a block of tasks all at once
for num in 1..3 loop
declare
The_Tasks : Tasks;
begin
while not Task_Counter.Is_Done(The_Tasks(The_Tasks'Last)) loop
delay 0.5;
end loop;
end;
end loop;
-- Declare each task one at a time as you need it
-- My Win98 system allows a max of 248 tasks to be started at
-- once. It hangs the program if I attempt to start more without
-- the following logic.
for num in 1..1000 loop
while Task_Counter.Task_Count >= 247 loop
delay 1.0;
Ada.Text_IO.Put_Line("There are " &
Natural'Image(Task_Counter.Task_Count) &
" active tasks.");
end loop;
declare
Task_Ptr : Task_Counter.Controlled_Task;
begin
null;
end;
end loop;
end Count_Tasks;
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2001-12-16 2:27 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-12-16 2:27 Limiting Task Counts James Rogers
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox