Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Freezing an Ada task

Tags:

task

ada

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).

like image 574
Rego Avatar asked Nov 17 '11 00:11

Rego


3 Answers

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.

like image 153
Keith Thompson Avatar answered Oct 17 '22 13:10

Keith Thompson


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

like image 4
Hunter McMillen Avatar answered Oct 17 '22 14:10

Hunter McMillen


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.

like image 4
Marc C Avatar answered Oct 17 '22 15:10

Marc C