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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

296 Tasks<br />

A package Pack_types is def<strong>in</strong>ed to allow commonly-used types to be conveniently kept together.<br />

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

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

package Pack_Types is<br />

type P_File_Type is access all <strong>Ada</strong>.Text_Io.File_Type;<br />

Eot : constant Character := Character'Val(0);<br />

Cr : constant Character := Character'Val(15);<br />

Queue_Size : constant := 3;<br />

type Queue_No is new Integer range 0 .. Queue_Size;<br />

type Queue_Index is mod Queue_Size;<br />

subtype Queue_Range is Queue_Index;<br />

type Queue_Array is array ( Queue_Range ) of Character;<br />

end Pack_Types;<br />

Note:<br />

The above package is used to def<strong>in</strong>e the type P_File_Type which is used by several other program<br />

units.<br />

The specification for the buffer protected type is as follows:<br />

with Pack_Types;<br />

use Pack_Types;<br />

package Pack_Threads is<br />

protected type PT_Buffer is --Task type specification<br />

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

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

private<br />

Elements : Queue_Array; --Array of elements<br />

Head : Queue_Index := 0; --Index<br />

Tail : Queue_Index := 0; --Index<br />

No_In_Queue : Queue_No := 0; --Number <strong>in</strong> queue<br />

F<strong>in</strong> : Boolean := False; --F<strong>in</strong>ish;<br />

end PT_Buffer ;<br />

type P_PT_Buffer is access all PT_Buffer ;<br />

The <strong>Ada</strong> specification for the reader and writer tasks are as follows:<br />

task type Task_Read( P_Buffer:P_PT_Buffer ;<br />

Fd_In:P_File_Type) is<br />

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

end Task_Read;<br />

task type Task_Write( P_Buffer:P_PT_Buffer ;<br />

Fd_Out:P_File_Type) is<br />

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

end Task_Write;<br />

end Pack_Threads;<br />

Note:<br />

To allow the reader and writer tasks to communicate with the buffer, a reference to the buffer protected<br />

object is passed to these tasks. A reference to the buffer protected object has to be passed as a<br />

protected object is of limited type.<br />

The same strategy is used to pass an <strong>in</strong>stance of File_Type.<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!