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 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!feeder.eternal-september.org!feed.usenet.farm!feeder3.usenet.farm!newsfeed.xs4all.nl!newsfeed8.news.xs4all.nl!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Niklas Holsti Newsgroups: comp.lang.ada Subject: Re: FIFO Date: Sun, 17 Sep 2017 11:57:16 +0300 Organization: Tidorum Ltd Message-ID: References: <87wp4y8vkn.fsf@jacob-sparre.dk> <87poap93jr.fsf@jacob-sparre.dk> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net CXcMjTMU46Fszagqy+hiZA99JBl9YBcUAZRdc1wshtoLOESBth Cancel-Lock: sha1:WAxT0NbxiO+qberlkbOTmStQegw= User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 In-Reply-To: <87poap93jr.fsf@jacob-sparre.dk> Xref: news.eternal-september.org comp.lang.ada:48165 Date: 2017-09-17T11:57:16+03:00 List-Id: On 17-09-17 10:39 , Jacob Sparre Andersen wrote: > Frank Buss wrote: > >> And everyone feel free to criticize my code, as I'm very new to >> Ada. [snip] >> generic >> Size : Positive; >> type Item is private; >> package Ringbuffers is > > Another option is: > > generic > type Index is mod <>; > type Element is private; > package Ring_Buffer is > type Instance is tagged private; > subtype Class is Instance'Class; > > procedure Write (Buffer : in out Instance; > New_Item : in Element); > function Read (Buffer : in out Instance) return Element; > function Is_Empty (Buffer : Instance) return Boolean; > private > type Element_Array is array (Index) of Element > with Volatile; -- I'm not sure this does what you want. > > type Instance is tagged > record > Head, Tail : Index := Index'First; > Elements : Element_Array; > end record > with Volatile; -- I'm not sure this does what you want either. > end Ring_Buffer; > > Notice that "Volatile" is a bit tricky. I'm not sure if that is what > you need. Nor if it makes sense to declare both types volatile. I would use pragma Atomic on Head and Tail, pragma Volatile_Components on Element_Array, and no pragma on the (whole) Instance record type. In May of this year there was a long and profound thread on comp.lang.ada about the Atomic/Volatile issue, with the subject "Portable memory barrier?". Worth reading if you intend to use those pragmas. On the functional level, it seems to me that the ring-buffer package examples given so far are missing a check for overflow (full queue) in the "write" (enqueue) operation. I would augment the Write procedure to make this check and return a success/failure result to the caller (the interrupt handler) and/or increment a "lost elements" counter in the queue Instance, for use by the queue-reading task, -- Niklas Holsti Tidorum Ltd niklas holsti tidorum fi . @ .