Lady Ada

Ada '83 Language Reference Manual

Copyright 1980, 1982, 1983 owned by the United States Government. Direct reproduction and usage requests to the Ada Information Clearinghouse.


9.4. Task Dependence - Termination of Tasks

[PREVIOUS][UP][NEXT]

Each task depends on at least one master. A master is a construct that is either a task, a currently executing block statement or subprogram, or a library package (a package declared within another program unit is not a master). The dependence on a master is a direct dependence in the following two cases:

  1. The task designated by a task object that is the object, or a subcomponent of the object, created by the evaluation of an allocator depends on the master that elaborates the corresponding access type definition.

  2. The task designated by any other task object depends on the master whose execution creates the task object.
Furthermore, if a task depends on a given master that is a block statement executed by another master, then the task depends also on this other master, in an indirect manner; the same holds if the given master is a subprogram called by another master, and if the given master is a task that depends (directly or indirectly) on another master. Dependences exist for objects of a private type whose full declaration is in terms of a task type.

A task is said to have completed its execution when it has finished the execution of the sequence of statements that appears after the reserved word begin in the corresponding body. Similarly a block or a subprogram is said to have completed its execution when it has finished the execution of the corresponding sequence of statements. For a block statement, the execution is also said to be completed when it reaches an exit, return, or goto statement transferring control out of the block. For a procedure, the execution is also said to be completed when a corresponding return statement is reached. For a function, the execution is also said to be completed after the evaluation of the result expression of a return statement. Finally the execution of a task, block statement, or subprogram is completed if an exception is raised by the execution of its sequence of statements and there is no corresponding handler, or, if there is one, when it has finished the execution of the corresponding handler.

If a task has no dependent task, its termination takes place when it has completed its execution. After its termination, a task is said to be terminated. If a task has dependent tasks, its termination takes place when the execution of the task is completed and all dependent tasks are terminated. A block statement or subprogram body whose execution is completed is not left until all of its dependent tasks are terminated.

Termination of a task otherwise takes place if and only if its execution has reached an open terminate alternative in a select statement (see 9.7.1), and the following conditions are satisfied:

When both conditions are satisfied, the task considered becomes terminated, together with all tasks that depend on the master considered.

Example:

    declare
       type GLOBAL is access RESOURCE;          --  see 9.1
       A, B : RESOURCE;
       G    : GLOBAL;
    begin
       --  activation of A and B
       declare
          type LOCAL is access RESOURCE;
          X : GLOBAL := new RESOURCE;  --  activation of X.all
          L : LOCAL  := new RESOURCE;  --  activation of L.all
          C : RESOURCE;
       begin
          --  activation of C
          G := X;  --  both G and X designate the same task object
          ...
       end;  --  await termination of C and L.all (but not X.all)
       ...
    end;  --  await termination of A, B, and G.all 

Notes:

The rules given for termination imply that all tasks that depend (directly or indirectly) on a given master and that are not already terminated, can be terminated (collectively) if and only if each of them is waiting on an open terminate alternative of a select statement and the execution of the given master is completed.

The usual rules apply to the main program. Consequently, termination of the main program awaits termination of any dependent task even if the corresponding task type is declared in a library package. On the other hand, termination of the main program does not await termination of tasks that depend on library packages; the language does not define whether such tasks are required to terminate.

For an access type derived from another access type, the corresponding access type definition is that of the parent type; the dependence is on the master that elaborates the ultimate parent access type definition.

A renaming declaration defines a new name for an existing entity and hence creates no further dependence.

References: access type, allocator, block statement, declaration, designate, and 9.1, exception, exception handler, exit statement, function, goto statement, library unit, main program, object, open alternative, package, program unit, renaming declaration, return statement, selective wait, sequence of statements, statement, subcomponent, subprogram body, subprogram call, task body, task object, terminate alternative.

Style Guide references: 6.2.3 Attributes 'Count, 'Callable and 'Terminated, 6.3 Termination

[INDEX][CONTENTS]


[Ada Information Clearinghouse]

Address any questions or comments to adainfo@sw-eng.falls-church.va.us.