Ada 95 Quality and Style Guide Chapter 6

Chapter 6: Concurrency - TOC - 6.2 COMMUNICATION

6.2.1 Efficient Task Communication

guideline

  • Minimize the work performed during a rendezvous.
  • Minimize the work performed in the selective accept loop of a task.
  • Consider using protected objects for data synchronization and communication.

  • example

    In the following example, the statements in the accept body are performed as part of the execution of both the caller task and the task Server, which contains Operation and Operation2. The statements after the accept body are executed before Server can accept additional calls to Operation or Operation2.

       ...
       loop
          select
             accept Operation do
                -- These statements are executed during rendezvous.
                -- Both caller and server are blocked during this time.
                ...
             end Operation;
             ...
             -- These statements are not executed during rendezvous.
             -- The execution of these statements increases the time required
             --   to get back to the accept and might be a candidate for another task.
    
          or
             accept Operation_2 do
                -- These statements are executed during rendezvous.
                -- Both caller and server are blocked during this time.
                ...
             end Operation_2;
          end select;
          -- These statements are also not executed during rendezvous,
          -- The execution of these statements increases the time required
          --   to get back to the accept and might be a candidate for another task.
    
       end loop;
    

    rationale

    To minimize the time required to rendezvous, only work that needs to be performed during a rendezvous, such as saving or generating parameters, should be allowed in the accept bodies.

    When work is removed from the accept body and placed later in the selective accept loop, the additional work might still suspend the caller task. If the caller task calls entry Operation again before the server task completes its additional work, the caller is delayed until the server completes the additional work. If the potential delay is unacceptable and the additional work does not need to be completed before the next service of the caller task, the additional work can form the basis of a new task that will not block the caller task.

    Operations on protected objects incur less execution overhead than tasks and are more efficient for data synchronization and communication than the rendezvous. You must design protected operations to be bounded, short, and not potentially blocking.

    notes

    In some cases, additional functions can be added to a task. For example, a task controlling a communication device might be responsible for a periodic function to ensure that the device is operating correctly. This type of addition should be done with care, realizing that the response time of the task might be impacted (see the above rationale).

    Minimizing the work performed during a rendezvous or selective accept loop of a task can increase the rate of execution only when it results in additional overlaps in processing between the caller and callee or when other tasks can be scheduled due to the shorter period of execution. Therefore, the largest increases in execution rates will be seen in multiprocessor environments. In single-processor environments, the increased execution rate will not be as significant and there might even be a small net loss. The guideline is still applicable, however, if the application could ever be ported to a multiprocessor environment.


    < 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