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.1. Task Specifications and Task Bodies

[UP][NEXT]

A task unit consists of a task specification and a task body. A task specification that starts with the reserved words task type declares a task type. The value of an object of a task type designates a task having the entries, if any, that are declared in the task specification; these entries are also called entries of this object. The execution of the task is defined by the corresponding task body.

A task specification without the reserved word type defines a single task. A task declaration with this form of specification is equivalent to the declaration of an anonymous task type immediately followed by the declaration of an object of the task type, and the task unit identifier names the object. In the remainder of this chapter, explanations are given in terms of task type declarations; the corresponding explanations for single task declarations follow from the stated equivalence.

    task_declaration ::= task_specification; 

    task_specification ::=
       task [type] identifier [is
          {entry_declaration}
          {representation_clause}
       end [task_simple_name]] 

    task_body ::=
        task body task_simple_name is
           [declarative_part]
        begin
            sequence_of_statements
       [exception
            exception_handler
           {exception_handler}]
        end [task_simple_name]; 

The simple name at the start of a task body must repeat the task unit identifier. Similarly if a simple name appears at the end of the task specification or body, it must repeat the task unit identifier. Within a task body, the name of the corresponding task unit can also be used to refer to the task object that designates the task currently executing the body; furthermore, the use of this name as a type mark is not allowed within the task unit itself.

For the elaboration of a task specification, entry declarations and representation clauses, if any, are elaborated in the order given. Such representation clauses only apply to the entries declared in the task specification (see 13.5).

The elaboration of a task body has no other effect than to establish that the body can from then on be used for the execution of tasks designated by objects of the corresponding task type.

The execution of a task body is invoked by the activation of a task object of the corresponding type (see 9.3). The optional exception handlers at the end of a task body handle exceptions raised during the execution of the sequence of statements of the task body (see 11.4).

Examples of specifications of task types:

    task type RESOURCE is
       entry SEIZE;
       entry RELEASE;
    end RESOURCE;  

    task type KEYBOARD_DRIVER is
       entry READ (C : out CHARACTER);
       entry WRITE(C : in  CHARACTER);
    end KEYBOARD_DRIVER;                                                     

Examples of specifications of single tasks:

    task PRODUCER_CONSUMER is
       entry READ (V : out ITEM);
       entry WRITE(E : in  ITEM);
    end; 

    task CONTROLLER is
       entry REQUEST(LEVEL)(D : ITEM);  --  a family of entries
    end CONTROLLER; 

    task USER;  --  has no entries 

Example of task specification and corresponding body:

    task PROTECTED_ARRAY is
       --  INDEX and ITEM are global types
       entry READ (N : in INDEX; V : out ITEM);
       entry WRITE(N : in INDEX; E : in  ITEM);
    end; 

    task body PROTECTED_ARRAY is
       TABLE : array(INDEX) of ITEM := (INDEX => NULL_ITEM);
    begin
       loop
          select
             accept READ (N : in INDEX; V : out ITEM) do
                V := TABLE(N);
             end READ;
          or
             accept WRITE(N : in INDEX; E : in  ITEM) do
                TABLE(N) := E;
             end WRITE;
          end select;
       end loop;
    end PROTECTED_ARRAY;

Note:

A task specification specifies the interface of tasks of the task type with other tasks of the same or of different types, and also with the main program.

References: declaration, declarative part, elaboration, entry, entry declaration, exception handler, identifier, main program, object, object declaration, representation clause, reserved word, sequence of statements, simple name, type, type declaration.

Rationale references: 13.2.1 Tasks: Textual Layout

Style Guide references: 2.1.7 Pagination, 3.2.4 Program Unit Names, 3.3.7 Marker Comments, 5.1.4 Naming End Statements, 6.1.1 Tasks, 6.1.4 Priorities, 6.3.3 The Abort Statement, 6.3.4 Abnormal Termination, 7.1.3 Comments, 7.4.6 Abort, 8.4.2 Coupling Due to Pragmas

[INDEX][CONTENTS]


[Ada Information Clearinghouse]

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