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.

Tasks 299<br />

to the entry prevents the entry be<strong>in</strong>g processed until there is room <strong>in</strong> the buffer. If the buffer is full then the<br />

reader task is suspended (blocked) until a successful get entry is made. The guards for an entry statement are<br />

re-evaluated after a successful call on the protected object. The full implementation of the protected type<br />

PT_Buffer is as follows:<br />

protected body PT_Buffer is<br />

The queue is implemented <strong>in</strong> sequential store with the two <strong>in</strong>dices head and tail keep<strong>in</strong>g track of the<br />

current extraction and <strong>in</strong>sertion po<strong>in</strong>ts respectively. A count of the active cells used <strong>in</strong> the buffer is held <strong>in</strong><br />

no_<strong>in</strong>_queue. Figure 20.5 illustrates the queue after add<strong>in</strong>g the characters ‘t’, ‘e’, ‘x’, ‘t’.<br />

Head<br />

Tail<br />

T e<br />

x t<br />

Figure 20.5 Queue hold<strong>in</strong>g the characters ‘text’.<br />

The procedure Put <strong>in</strong> the body of the protected object adds new data to the queue. Data can only be added to<br />

the queue when there is room. The <strong>in</strong>dex Tail marks the position of the next data item to be added. When no<br />

more data is available to add to the queue the variable F<strong>in</strong> is set to true.<br />

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

when No_In_Queue < Queue_Size is<br />

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

if No_More then<br />

--Last<br />

F<strong>in</strong> := True;<br />

--Set flag<br />

else<br />

Elements( Tail ) := Ch; --Add to queue<br />

Tail := Tail+1; --Next position<br />

No_In_Queue := No_In_Queue + 1; --<br />

end if;<br />

end;<br />

The procedure Get extracts data from the queue. The head <strong>in</strong>dexes the data item at the front of the queue.<br />

The parameter eof is set to true when no more data is available. This is different from a temporary unavailability<br />

of data due to the reader task block<strong>in</strong>g.<br />

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

when No_In_Queue > 0 or else F<strong>in</strong> is<br />

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

if No_In_Queue > 0 then --Item available<br />

Ch := Elements( Head ); --Get item<br />

Head := Head+1; --Next position<br />

No_In_Queue := No_In_Queue - 1; --<br />

No_More := False; --Not end<br />

else<br />

No_More := True;<br />

end if;<br />

end;<br />

end PT_Buffer ;<br />

end Pack_Threads;<br />

--End of items<br />

Note:<br />

When all the data has been exhausted from the buffer, the procedure get will return false <strong>in</strong> its second<br />

parameter.<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!