comp.lang.ada
 help / color / mirror / Atom feed
From: "Nick Roberts" <nickroberts@callnetuk.com>
Subject: Re: How can I avoid Using a Semaphore?
Date: Sun, 21 Jan 2001 16:53:21 -0000
Date: 2001-01-21T16:53:21+00:00	[thread overview]
Message-ID: <94f577$d9s66$1@ID-25716.news.dfncis.de> (raw)
In-Reply-To: 94co6t$v27$1@nnrp1.deja.com

I concur with Robert's comments on Ada's stream facilities.

I would have thought some companies which sell Ada compilers might
(depending on their clientele etc.) consider it a good idea to provide a
facility like GNAT's for changing all the default representations to the
network format, as well as (or by means of) providing something like
System.Stream_Attributes.

To solve your buffer synchronisation problem, Steve, please see the generic
package spec attached. The idea here is that the packet picking task(s)
does/do something like this:

   loop
      read the packet's length
      call Create to create a buffer of the correct length
      read the packet's data into the buffer
      call Dispatch to cause the buffer to be serviced
      (further calls to Dispatch on the buffer possible)
   end loop

Each servicing task does something like this:

   loop
      call Obtain to get a buffer that needs a specific service
      perform the service
      (calls to Dispatch possible, to cause further servicing of the buffer)
      call Release to signal that the service has been completed
   end loop;

A buffer is deleted automatically whenever a call to Release makes its
Disp_Count go down to zero.

This scheme has a lot of flexibility. It enables service tasks to dispatch a
buffer for further processing. It enables several tasks to perform the same
service, and it could enable one task to perform many services under some
circumstances. It makes it possible to have several packet picking tasks,
provided you ensure they actually pick the packets atomically.

I have left the package body, and the details of implementation, to you.
However, I've suggested that you would need two arrays, one for the
allocation of buffers, and one for the allocation of 'dispatches'.

Incidentally, I think you will need:

   type Array_Access is access all Data_Array;

if you are going to use an internal array for the buffer data, instead of
allocation with 'new'. The internal array will have to be aliased.

Hope this is helpful,

--
Nick Roberts
http://www.AdaOS.org


=====

generic
   type Datum_Type is private;
   type Service_Type is (<>);

package Buffer_Distribution_Management is

   type Data_Array is array (Positive range <>) of Datum_Type;

   type Array_Access is access Data_Array;

   type Buffer_Token   is private;
   type Dispatch_Token is private;

   -- Utility declarations (effectively private):

   type Buffer_Index   is new Positive;
   type Dispatch_Index is new Positive;

   type Buffer_Descriptor is
      record
         Allocated:  Boolean := False;
         Data:       Array_Access;
         Disp_Count: Natural := 0;
      end record;

   type Dispatch_Descriptor is
      record
         Allocated:  Boolean := False;
         Buffer:     Buffer_Index;
         Service:    Service_Type;
      end record;

   type Buffer_Array   is (Buffer_Index   range <>) of Buffer_Descriptor;
   type Dispatch_Array is (Dispatch_Index range <>) of Dispatch_Descriptor;

   -- The most important declaration for this package:

   protected type Buffer_Distributor (Buffer_Cap, Dispatch_Cap: Natural) is

      entry Create (Length: in  Natural;
                    Buffer: out Array_Access;
                    Token:  out Buffer_Token);

      procedure Delete (Token: in Buffer_Token);

      entry Dispatch (Token:   in Buffer_Token;
                      Service: in Service_Type);

      entry Obtain (Service: in  Service_Type;
                    Buffer:  out Array_Access;
                    Token:   out Dispatch_Token);

      procedure Release (Token: in Dispatch_Token);

      procedure Reset;

   private
      Buffers:    Buffer_Array   (1..Buffer_Cap);
      Dispatches: Dispatch_Array (1..Dispatch_Cap);
      ...

   end Buffer_Distributor;

private

   type Buffer_Token   is new Buffer_Index;
   type Dispatch_Token is new Dispatch_Index;

end Buffer_Distribution_Management;







  parent reply	other threads:[~2001-01-21 16:53 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-01-13 16:18 How can I avoid Using a Semaphore? (long) DuckE
2001-01-15  1:06 ` How can I avoid Using a Semaphore? Nick Roberts
2001-01-15  3:17   ` Robert Dewar
2001-01-16  3:53   ` DuckE
2001-01-17 15:42     ` Nick Roberts
2001-01-20 18:16       ` DuckE
2001-01-20 19:16         ` Robert Dewar
2001-01-21  1:28           ` DuckE
2001-01-21 16:04             ` Robert Dewar
2001-01-21 23:23               ` DuckE
2001-01-22  0:28                 ` mark_lundquist
2001-01-22  1:51                 ` Robert Dewar
2001-01-23  2:36                   ` DuckE
2001-01-22  0:35               ` Built-in types (was " mark_lundquist
2001-01-22  1:54                 ` Robert Dewar
2001-01-22 16:18                   ` mark_lundquist
2001-01-22 17:20                     ` Robert Dewar
2001-01-22 23:17                       ` Mark Lundquist
     [not found]                         ` <m33deaaeks.fsf@ns40.infomatch.bc.ca>
2001-02-02 22:01                           ` Mark Lundquist
     [not found]                         ` <94km00$bv8$1@nnrp1.deja.com>
2001-02-02 22:03                           ` Mark Lundquist
2001-01-21 16:53           ` Nick Roberts [this message]
2001-01-21 18:24             ` Robert Dewar
2001-01-23  0:21               ` Nick Roberts
2001-01-22  0:16         ` mark_lundquist
2001-01-22 16:51 ` How can I avoid Using a Semaphore? (long) mark_lundquist
2001-01-23  6:02   ` DuckE
2001-02-02 22:00     ` Sucking (was Re: How can I avoid Using a Semaphore? (long)) Mark Lundquist
2001-02-03  1:44       ` Jeffrey Carter
2001-02-03  3:21       ` DuckE
2001-02-05 20:07         ` Mark Lundquist
2001-02-06  7:16           ` Sven Nilsson
2001-02-02 22:18     ` How can I avoid Using a Semaphore? (long) Mark Lundquist
2001-02-03  3:01       ` DuckE
2001-02-02 21:38 ` Niklas Holsti
replies disabled

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