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=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,d495ab2e69ad1962 X-Google-Attributes: gid103376,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news1.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!bcklog2.nntp.dca.giganews.com!nntp.comcast.com!news.comcast.com.POSTED!not-for-mail NNTP-Posting-Date: Tue, 04 Sep 2007 22:00:16 -0500 From: "Steve" Newsgroups: comp.lang.ada References: <1188914005.607732.277400@57g2000hsv.googlegroups.com> Subject: Re: Ravenscar-compliant bounded buffer Date: Tue, 4 Sep 2007 20:00:13 -0700 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2900.3138 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3138 X-RFC2646: Format=Flowed; Original Message-ID: X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 24.20.111.206 X-Trace: sv3-w2kGy4yPgYEDc4HdVU0lpogYEPXQGEa6rO+Qe0Ef7R/j1Fermu8VK2IKwIz9Tv+CLjjYEfJFhQQoiHp!ReFPKSEw31KqhkVo3f9DanSaUcx21gqjwAIBHRU11xYIAfZ/1euqbs3xPv0+mux0GA4LxEr6g6Em!ix/G0y0vqzMRjx0wXduE0H6l02CTpQ== X-Complaints-To: abuse@comcast.net X-DMCA-Complaints-To: dmca@comcast.net X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.35 Xref: g2news2.google.com comp.lang.ada:1741 Date: 2007-09-04T20:00:13-07:00 List-Id: While I don't know much about the Ravenscar profile (other than what it is) I am famialar with the use of protected types. The sample code is a good illustration of "abstraction inversion". I know this because I posted similar code on this group several years ago and that is how it was described. The code uses a high level abstraction "protected type" to create a low leve abstraction "Binary_Semaphore". The code uses two semaphores to restrict access to the bounded buffer. In this simple example it is easy to follow, but in a more complex example pairing Acquire's and Release's can be a chore. Consider an alternate implementation: -- 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 Exclusive_Access is entry Put (Buffer : in out Buffer_Type; Item : in Integer); entry Get (Buffer : in out Buffer_Type; Item : out Integer); private Available_For_Reading : Boolean := False; Available_For_Writing : Boolean := True; end Exclusive_Access; type Buffer_Type is limited record Storage : Integer; Protected_Access : Exclusive_Access; end record; end Bounded_Buffer; -- bounded_buffer.adb package body Bounded_Buffer is protected body Exclusive_Access is entry Put (Buffer : in out Buffer_Type; Item : in Integer) when Available_For_Writing is begin Buffer.Storage := Item; Available_For_Reading := True; Available_For_Writing := False; end Put; entry Get (Buffer : in out Buffer_Type; Item : out Integer) when Available_For_Reading is begin Item := Buffer.Storage; Available_For_Reading := False; Available_For_Writing := True; end Get; end Exclusive_Access; procedure Put (Buffer : in out Buffer_Type; Item : in Integer) is begin Buffer.Protected_Access.Put( Buffer, Item ); end Put; procedure Get (Buffer : in out Buffer_Type; Item : out Integer) is begin Buffer.Protected_Access.Get( Buffer, Item ); end Get; end Bounded_Buffer; ---------------------------------- Here a single Exclusive_Access protected type is used to restrict access to the buffer. It avoids the situation that you will run into with "who's not releasing the semaphore". BTW: I apologize for my choice of type and variable names, I didn't spend much time thinking about them. Regards, Steve (The Duck) "Maciej Sobczak" wrote in message news:1188914005.607732.277400@57g2000hsv.googlegroups.com... > 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/ >