comp.lang.ada
 help / color / mirror / Atom feed
* Ada Protected Object Tutorial #1
@ 1999-12-15  0:00 James S. Rogers
  1999-12-16  0:00 ` Kaz Kylheku
  1999-12-17  0:00 ` Robert A Duff
  0 siblings, 2 replies; 45+ messages in thread
From: James S. Rogers @ 1999-12-15  0:00 UTC (permalink / raw)


This is the first of hopefully several examples of uses for Ada Protected
Objects.

What is a protected object?

Ada provides the ability to create data that can safely be shared by several
tasks
or "threads". The data in a protected object is supported by full mutual
exclusion
characteristics.

The data in a protected object can be accessed in three ways.
Protected Functions:

 The protected function merely reports the value of some data component in
the
protected object. Although it is possible, it is STRONGLY recommended that
protected functions DO NOT change the values of any data components in the
protected object. Any parameters to a protected function must have the "IN"
mode, indicating that the parameters are read-only. Multiple tasks may
simultaneously access a protected object through functions because the
data values are not changed. Protected procedures and protected entries
may not access the protected object while it is being accessed by a
protected function.

Protected Procedures:

Protected procedures are typically used to alter the value of one or more
data
components in a protected object. The parameters of a protected procedure
may have the "IN" mode, like functions. They may also have "OUT" mode,
indicating that the value is write only, or "IN OUT" mode indicating that
the
parameter value can be read and updated. Only one task at a time may
execute a protected procedure. Protected functions cannot access the
protected object while it is being accessed by a protected procedure.

Protected Entries:

Protected entries are just like protected functions with one small
difference.
Protected entries block untils a boundary condition becomes true. For
example, you would use a protected entry to read from a protected
bounded queue. The entry would block as long as the queue was empty.

You can define only one instance of a protected object. Fortunately, Ada
also allows you to define protected types. Protected types use almost
the same syntax as a protected object. The advantage of a protected
type is that you may create as many instances of the type as you need.
You may also define an access type (similar to a pointer) to a protected
type. In this manner you may define an instance of a protected type, point
to it with an instance of the access type, and then pass that access type
to several tasks so that they can easily share the single instance of the
protected type.


Let's look at a first example of a protected type. This example will
illustrate how to create a protected buffer. The buffer has a protected
procedure to write to the buffer and a protected entry to read from the
buffer. The entry is used so that reading tasks will be suspended
until a writing task writes a first valid value.


The definition of the protected type is encapsulated in a generic package
so that it can be used for any data type. The package is divided into two
parts. The first part is the package specification, which defines the
interface
to the protected type. The second part is the package body which defines
the implementation of the package.

-----------------------------------------------------------------------
-- Package: Generic_Protected_Buffer
--
-- Purpose: This package defines a generic buffer allowing multiple
--          writers and multiple readers with full mutual exclusion
--          The readers cannot read until a writer has first written
--          to the buffer.
-----------------------------------------------------------------------
generic

   type Element_Type is private;

package Generic_Protected_Buffer is

   protected type Protected_Buffer is
      procedure Write(Item : in Element_Type);
      entry Read(Item : out Element_Type);
   private
      Data_Valid : Boolean := False;
      Buffer     : Element_Type;
   end Protected_Buffer;
end Generic_Protected_Buffer;

----------------------------------------------------------------------------

Note that the data component "Data_Valid" is given an initial
value of "False". This simplifies the job of making sure the
Read entry cannot be executed until a Write procedure has
been executed.

package body Generic_Protected_Buffer is

   ----------------------
   -- Protected_Buffer --
   ----------------------

   protected body Protected_Buffer is

      ----------
      -- Read --
      ----------

      entry Read (Item : out Element_Type) when Data_Valid is
      begin
         Item := Buffer;
      end Read;

      -----------
      -- Write --
      -----------

      procedure Write (Item : in Element_Type) is
      begin
         Buffer := Item;
         Data_Valid := True;
      end Write;

   end Protected_Buffer;

end Generic_Protected_Buffer;

----------------------------------------------------------------------------
--------

Notice the boundary condition specified in the implementation of
the Read entry. Read will execute "when Data_Valid = True".
The Write procedure sets the value of the buffer then sets
Data_Valid to True, releasing all tasks queued on the blocked
Read entry.


Jim Rogers
Colorado Springs, Colorado






^ permalink raw reply	[flat|nested] 45+ messages in thread

end of thread, other threads:[~2000-01-02  0:00 UTC | newest]

Thread overview: 45+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-12-15  0:00 Ada Protected Object Tutorial #1 James S. Rogers
1999-12-16  0:00 ` Kaz Kylheku
1999-12-16  0:00   ` John English
1999-12-16  0:00     ` Ed Falis
1999-12-16  0:00       ` Usenet Poster Boy
1999-12-17  0:00     ` Karel Th�nissen
1999-12-17  0:00       ` Mike Silva
1999-12-17  0:00       ` Laurent Guerby
1999-12-18  0:00         ` Kaz Kylheku
1999-12-18  0:00           ` Laurent Guerby
1999-12-18  0:00             ` Kaz Kylheku
1999-12-19  0:00               ` Laurent Guerby
1999-12-20  0:00                 ` Stanley R. Allen
1999-12-21  0:00               ` Robert I. Eachus
     [not found]             ` <33qr5scnbs04v391ev4541p5bv48hklg3q@4ax.com>
1999-12-20  0:00               ` Robert A Duff
1999-12-18  0:00           ` Robert A Duff
1999-12-18  0:00             ` Kaz Kylheku
1999-12-18  0:00         ` Karel Th�nissen
1999-12-18  0:00           ` Laurent Guerby
1999-12-24  0:00       ` Kenneth Almquist
1999-12-16  0:00   ` James S. Rogers
1999-12-17  0:00     ` Laurent Guerby
1999-12-17  0:00   ` Tucker Taft
1999-12-18  0:00     ` Kaz Kylheku
1999-12-18  0:00       ` Robert A Duff
1999-12-18  0:00         ` Kaz Kylheku
1999-12-19  0:00           ` swhalen
1999-12-19  0:00             ` Kaz Kylheku
1999-12-19  0:00               ` Laurent Guerby
1999-12-19  0:00               ` Robert Dewar
1999-12-20  0:00       ` Vladimir Olensky
1999-12-26  0:00         ` Ehud Lamm
1999-12-26  0:00           ` Robert Dewar
1999-12-26  0:00             ` Kaz Kylheku
1999-12-27  0:00               ` Robert Dewar
1999-12-27  0:00                 ` Jean-Pierre Rosen
1999-12-27  0:00                 ` Richard D Riehle
1999-12-27  0:00                   ` Robert Dewar
1999-12-31  0:00                     ` Richard D Riehle
1999-12-27  0:00               ` Robert Dewar
2000-01-02  0:00             ` Tucker Taft
1999-12-17  0:00   ` Robert A Duff
1999-12-17  0:00     ` Vladimir Olensky
1999-12-17  0:00 ` Robert A Duff
1999-12-18  0:00   ` Kaz Kylheku

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