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=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: ffc1e,fb45e48e8dddeabd,start X-Google-Attributes: gidffc1e,public X-Google-Thread: 103376,fb45e48e8dddeabd,start X-Google-Attributes: gid103376,public From: "James S. Rogers" Subject: Ada Protected Object Tutorial #1 Date: 1999/12/15 Message-ID: <839toq$pu$1@bgtnsc03.worldnet.att.net> X-Deja-AN: 561290390 X-MimeOLE: Produced By Microsoft MimeOLE V4.72.2106.4 X-Complaints-To: abuse@worldnet.att.net X-Trace: bgtnsc03.worldnet.att.net 945322586 830 12.74.129.100 (16 Dec 1999 05:36:26 GMT) Organization: AT&T WorldNet Services NNTP-Posting-Date: 16 Dec 1999 05:36:26 GMT Newsgroups: comp.programming.threads,comp.lang.ada Date: 1999-12-16T05:36:26+00:00 List-Id: 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