How can I freeze a task?
I mean, if I have a task
task body My_Task is
begin
accept Start;
loop
Put ("1");
Put ("2");
Put ("3");
...
Put ("n");
end loop;
end My_Task;
is there a way that I can "freeze" the task in its current state? If, for instance, the execution finished executing Put ("2");
, how can I freeze it and later I can turn it to continue? I want to provoque a freeze from outside the task, and also from outside, order it to continue.
Update
I could sure implement, if I had the spec like
type State_Type is
(RUN,
FROZEN);
task type My_Task (State : State_Type) is
entry Start;
end My_Task;
the body
task body My_Task is
begin
accept Start;
loop
Put ("1");
Put ("2");
Put ("3");
...
Put ("n");
loop
if State = RUN then exit; end if;
end loop;
end loop;
end My_Task;
but it would not be the case because I had to wait for the nth Put instruction line (i.e., the task would not be actually frozen, because the inside loop would be running).
You can use a "selective accept" to allow your task to be interrupted by an external caller (another task or the main program). You can't (easily) interrupt a task at an arbitrary point in its execution; instead, the task itself needs to determine when it will accept entry calls.
So you'll probably want to replace your sequence
Put("1");
Put("2");
Put("3");
...
Put("n");
by a loop that calls Put
once on each iteration, with a select
statement that's executed each time.
Here's a demo program that I just threw together. The counter increments once per second (approximately). The counter value is printed if Running
is true; otherwise the loop proceeds silently. The main program alternatively calls Pause and Resume every 3 seconds.
The else
clause in the select
statement cause it to continue executing if neither entry has been called.
with Ada.Text_IO; use Ada.Text_IO;
procedure T is
task Looper is
entry Pause;
entry Resume;
end Looper;
task body Looper is
Running: Boolean := True;
Count: Integer := 0;
begin
loop
select
accept Pause do
Running := False;
end;
or
accept Resume do
Running := True;
end;
else
null;
end select;
delay 1.0;
if Running then
Put_Line("Running, Count = " & Integer'Image(Count));
end if;
Count := Count + 1;
end loop;
end Looper;
begin -- T
loop
delay 3.0;
Looper.Pause;
delay 3.0;
Looper.Resume;
end loop;
end T;
There may be a more elegant approach than this. It's been a long time since I've really used Ada.
Wow, haven't seen an Ada question in awhile. Anyway whenever you need to pause a task you use the delay
or delay until
reserved words.
you would specify a time you want to resume execution, then say:
delay <time>
or
delay until <time>
I don't remember the exact details, but here is the Ada95 specification example: http://www.adaic.org/resources/add_content/docs/95style/html/sec_6/6-1-7.html
One aspect of the intent of the design underlying the Ada language is that behavior be explicit. An executing task that suddenly found itself "frozen" at an arbitrary point for no readily discernible reason--there being nothing in the task that would lead to that behavior--would be disconcerting at best. And possibly a bug if the task was not designed to allow for unexpected, arbitrary execution suspension. (I know OS time-slicing imposes this behavior on executing programs, but that's a function of the OS, not the programming language.)
In short, I would seriously question a design approach that relied on externally imposed task suspension. A task should know it's own state, thus know when it's safe to pause, and what invariants have to hold for pausing in order to ensure a proper resumption.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With