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.

298 Tasks<br />

The implementation of the reader task is then:<br />

with <strong>Ada</strong>.Text_Io;<br />

use <strong>Ada</strong>.Text_Io;<br />

package body Pack_Threads is<br />

task body Task_Read is --Task implementation<br />

Ch : Character;<br />

beg<strong>in</strong><br />

while not End_Of_File( Fd_In.all ) loop<br />

while not End_Of_L<strong>in</strong>e( Fd_In.all ) loop<br />

Get( Fd_In.all, Ch); --Get character<br />

P_Buffer.Put( Ch, False ); --Add to buffer<br />

end loop;<br />

Skip_L<strong>in</strong>e( Fd_In.all ); --Next l<strong>in</strong>e<br />

P_Buffer.Put( Cr, False ); --New l<strong>in</strong>e<br />

end loop;<br />

P_Buffer.Put( Eot, True ); --End of characters<br />

accept F<strong>in</strong>ish;<br />

exception<br />

when Task<strong>in</strong>g_Error =><br />

Put("Exception <strong>in</strong> Task read"); New_L<strong>in</strong>e;<br />

end Task_Read;<br />

The rendezvous F<strong>in</strong>ish is used by the reader to <strong>in</strong>dicate that there is no more data.<br />

Note: As task<strong>in</strong>g errors are not propagated beyond the task, a specific exception handler is used to detect this<br />

eventuality.<br />

The character constant Cr is used to <strong>in</strong>dicate the newl<strong>in</strong>e character.<br />

Similarly, the writer task is implemented as follows:<br />

task body Task_Write is --Task implementation<br />

Last : Boolean := False; --No more data<br />

Ch : Character; --Character read<br />

beg<strong>in</strong><br />

loop<br />

P_Buffer.Get( Ch, Last ); --From buffer<br />

exit when Last;<br />

--No more characters<br />

if Ch = Cr then<br />

New_L<strong>in</strong>e( Fd_Out.all ); --New l<strong>in</strong>e<br />

else<br />

Put( Fd_Out.all, Ch ); --Character<br />

end if;<br />

end loop;<br />

accept F<strong>in</strong>ish;<br />

--F<strong>in</strong>ished<br />

exception<br />

when Task<strong>in</strong>g_Error =><br />

Put("Exception <strong>in</strong> Task write"); New_L<strong>in</strong>e;<br />

end Task_Write;<br />

20.5.1 Barrier condition entry<br />

The protected type uses one additional facility, that of a barrier entry. If the buffer becomes full, a mechanism is<br />

needed to prevent further data be<strong>in</strong>g added. The barrier:<br />

entry Put( Ch:<strong>in</strong> Character; No_More:<strong>in</strong> Boolean )<br />

when No_In_Queue < Queue_Size is<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!