18.10.2014 Views

Object-oriented Software in Ada 95

Object-oriented Software in Ada 95

Object-oriented Software in Ada 95

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Tasks 2<strong>95</strong><br />

20.4 Protected type<br />

In essence, an <strong>in</strong>stance of a protected type is an object whose methods have strict concurrency access rules. A<br />

protected object, an <strong>in</strong>stance of a protected type, is composed of data and the procedures and functions that access<br />

the data. The table below summarizes the concurrent access rules for procedures and functions <strong>in</strong> a protected<br />

object.<br />

Unit Commentary Access<br />

procedure A procedure will only execute when no other Read and write.<br />

units are be<strong>in</strong>g executed. If necessary the<br />

procedure will wait until the currently execut<strong>in</strong>g<br />

unit(s) have f<strong>in</strong>ished.<br />

function A function may execute simultaneously with Read only.<br />

other execut<strong>in</strong>g functions. However, a function<br />

cannot execute if a procedure is currently<br />

execut<strong>in</strong>g.<br />

entry Like a procedure but may also have a barrier<br />

condition associated with the entry. If the barrier<br />

condition is false the entry is queued until the<br />

barrier becomes true.<br />

Read and write<br />

20.5 Implementation<br />

The implementation of a program to perform an efficient copy us<strong>in</strong>g an <strong>in</strong> store buffer to even out differences <strong>in</strong><br />

response rates can be implemented as two tasks and a protected object, as illustrated <strong>in</strong> Figure 20.4.<br />

Disk<br />

Reader<br />

task<br />

Writer<br />

task<br />

Disk<br />

Put<br />

Buffer<br />

Ge t<br />

Figure 20.4 Copy implemented us<strong>in</strong>g two tasks and a protected object buffer.<br />

The responsibilities of the components are as follows:<br />

Name <strong>Object</strong> is Responsibilities<br />

Task_Reader Task Read data from the file and then pass the data to the buffer.<br />

Note: The task will block if the buffer is full.<br />

Task_Writer Task Take data from the buffer task and write the data to the file.<br />

Note: The task will block if there is no data <strong>in</strong> the buffer.<br />

PT_Buffer Protected Serialize the stor<strong>in</strong>g and retriev<strong>in</strong>g of data to and from a<br />

type buffer.<br />

Note:<br />

The block<strong>in</strong>g is achieved with a guard to the accept statement. This is described <strong>in</strong> the section on<br />

guarded accepts.<br />

© M A Smith - May not be reproduced without permission

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!