Ada 95 Quality and Style Guide Chapter 4

Chapter 4: Program Structure - TOC - 4.2 VISIBILITY

4.2.4 Hiding Tasks

guideline

  • Carefully consider encapsulation of tasks.
  • example
    -------------------------------------------------------------------------
    package Disk_Head_Scheduler is
       type Words        is ...
       type Track_Number is ...
       procedure Transmit (Track : in     Track_Number;
                           Data  : in     Words);
       ...
    end Disk_Head_Scheduler;
    -------------------------------------------------------------------------
    package body Disk_Head_Scheduler is
       ...
       task Control is
          entry Sign_In (Track : in     Track_Number);
          ...
       end Control;
       ----------------------------------------------------------------------
       task Track_Manager is
          entry Transfer(Track_Number) (Data : in     Words);
       end Track_Manager;
       ----------------------------------------------------------------------
       ...
       procedure Transmit (Track : in     Track_Number;
                           Data  : in     Words) is
       begin
          Control.Sign_In(Track);
          Track_Manager.Transfer(Track)(Data);
       end Transmit;
       ----------------------------------------------------------------------
       ...
    end Disk_Head_Scheduler;
    -------------------------------------------------------------------------
    

    rationale

    The decision whether to declare a task in the specification or body of an enclosing package is not a simple one. There are good arguments for both.

    Hiding a task specification in a package body and exporting (via subprograms ) only required entries reduces the amount of extraneous information in the package specification. It allows your subprograms to enforce any order of entry calls necessary to the proper operation of the tasks. It also allows you to impose defensive task communication practices (see Guideline 6.2.2) and proper use of conditional and timed entry calls. Finally, it allows the grouping of entries into sets for export to different classes of users (e.g., producers versus consumers) or the concealment of entries that should not be made public at all (e.g., initialization, completion, signals). Where performance is an issue and there are no ordering rules to enforce, the entries can be renamed as subprograms to avoid the overhead of an extra procedure call.

    An argument, which can be viewed as an advantage or disadvantage, is that hiding the task specification in a package body hides the fact of a tasking implementation from the user. If the application is such that a change to or from a tasking implementation or a reorganization of services among tasks need not concern users of the package, then this is an advantage. However, if the package user must know about the tasking implementation to reason about global tasking behavior, then it is better not to hide the task completely. Either move it to the package specification or add comments stating that there is a tasking implementation, describing when a call may block, etc. Otherwise, it is the package implementor's responsibility to ensure that users of the package do not have to concern themselves with behaviors such as deadlock, starvation, and race conditions.

    Finally, keep in mind that hiding tasks behind a procedural interface prevents the usage of conditional and timed entry calls and entry families, unless you add parameters and extra code to the procedures to make it possible for callers to direct the procedures to use these capabilities.


    < Previous Page Search Contents Index Next Page >
    1 2 3 4 5 6 7 8 9 10 11
    TOC TOC TOC TOC TOC TOC TOC TOC TOC TOC TOC
    Appendix References Bibliography