From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,d495ab2e69ad1962,start X-Google-Attributes: gid103376,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!postnews.google.com!57g2000hsv.googlegroups.com!not-for-mail From: Maciej Sobczak Newsgroups: comp.lang.ada Subject: Ravenscar-compliant bounded buffer Date: Tue, 04 Sep 2007 06:53:25 -0700 Organization: http://groups.google.com Message-ID: <1188914005.607732.277400@57g2000hsv.googlegroups.com> NNTP-Posting-Host: 128.141.44.97 Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-2" X-Trace: posting.google.com 1188914005 25822 127.0.0.1 (4 Sep 2007 13:53:25 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Tue, 4 Sep 2007 13:53:25 +0000 (UTC) User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6,gzip(gfe),gzip(gfe) Complaints-To: groups-abuse@google.com Injection-Info: 57g2000hsv.googlegroups.com; posting-host=128.141.44.97; posting-account=ps2QrAMAAAA6_jCuRt2JEIpn5Otqf_w0 Xref: g2news2.google.com comp.lang.ada:1728 Date: 2007-09-04T06:53:25-07:00 List-Id: In one of my previous posts I have criticized example 12 from the Ravenscar document. It makes sense to criticize constructively, I think. :-) -- bounded_buffer.ads package Bounded_Buffer is type Buffer_Type is limited private; procedure Put (Buffer : in out Buffer_Type; Item : in Integer); procedure Get (Buffer : in out Buffer_Type; Item : out Integer); private protected type Binary_Semaphore (Initial_State : Boolean) is entry Acquire; procedure Release; private Available : Boolean := Initial_State; end Binary_Semaphore; type Buffer_Type is limited record Storage : Integer; Available_For_Reading : Binary_Semaphore (Initial_State => False); Available_For_Writing : Binary_Semaphore (Initial_State => True); end record; end Bounded_Buffer; -- bounded_buffer.adb package body Bounded_Buffer is procedure Put (Buffer : in out Buffer_Type; Item : in Integer) is begin Buffer.Available_For_Writing.Acquire; Buffer.Storage := Item; Buffer.Available_For_Reading.Release; end Put; procedure Get (Buffer : in out Buffer_Type; Item : out Integer) is begin Buffer.Available_For_Reading.Acquire; Item := Buffer.Storage; Buffer.Available_For_Writing.Release; end Get; protected body Binary_Semaphore is entry Acquire when Available is begin Available := False; end Acquire; procedure Release is begin Available := True; end Release; end Binary_Semaphore; end Bounded_Buffer; -- test.adb with Ada.Text_IO; with Bounded_Buffer; procedure Test is Buffer : Bounded_Buffer.Buffer_Type; task Writer; task body Writer is begin for I in 1 .. 50 loop Bounded_Buffer.Put (Buffer, I); end loop; Bounded_Buffer.Put (Buffer, 0); end Writer; task Reader; task body Reader is I : Integer; begin loop Bounded_Buffer.Get (Buffer, I); exit when I = 0; Ada.Text_IO.Put (Integer'Image (I)); Ada.Text_IO.New_Line; end loop; end Reader; begin null; end Test; I would like to ask you to comment/criticize on the above. (The test.adb driver is provided just to sanity-check the rest.) Note that in this example the shared data (Storage component of the Buffer object) is accessed directly from concurrent subprograms. Protecting this access is not necessary on the basis that binary semaphores provide necessary exclusion and signalling (ordering). With buffer of capacity > 1 the storage array and top/bottom indices would be encapsulated by another protected object. -- Maciej Sobczak http://www.msobczak.com/