comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: FIFO
Date: Sun, 17 Sep 2017 11:57:16 +0300
Date: 2017-09-17T11:57:16+03:00	[thread overview]
Message-ID: <f26rncFldakU1@mid.individual.net> (raw)
In-Reply-To: <87poap93jr.fsf@jacob-sparre.dk>

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
       .      @       .

  parent reply	other threads:[~2017-09-17  8:57 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-09-16 15:24 FIFO Frank Buss
2017-09-16 16:19 ` FIFO Jacob Sparre Andersen
2017-09-16 17:07   ` FIFO Dmitry A. Kazakov
2017-09-16 17:12   ` FIFO Frank Buss
2017-09-16 17:22     ` FIFO Dmitry A. Kazakov
2017-09-16 18:32       ` FIFO Frank Buss
2017-09-17  7:39         ` FIFO Jacob Sparre Andersen
2017-09-17  8:38           ` FIFO Jacob Sparre Andersen
2017-09-17  8:57           ` Niklas Holsti [this message]
2017-09-17 11:30         ` FIFO AdaMagica
     [not found]           ` <8b99f47a-63bf-4f07-9077-6dab3cf32a7f@googlegroups.com>
     [not found]             ` <oppbcq$7jj$1@newsreader4.netcologne.de>
     [not found]               ` <d37417dd-3a94-4a43-bc9c-071f2da6181d@googlegroups.com>
     [not found]                 ` <f2bs2lFq4j3U1@mid.individual.net>
     [not found]                   ` <opqq32$3bd$1@newsreader4.netcologne.de>
2017-09-25 20:57                     ` FIFO Niklas Holsti
2017-09-16 17:52 ` FIFO Robert A Duff
2017-09-16 18:11   ` FIFO Frank Buss
2017-09-17  9:46   ` FIFO Simon Wright
2017-09-16 22:04 ` FIFO Jeffrey R. Carter
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox