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.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,a0224dc3d1e52f3d X-Google-Attributes: gid103376,public From: "James S. Rogers" Subject: Re: Streams and Concurrency Date: 1998/12/30 Message-ID: <76dn7b$a35@bgtnsc03.worldnet.att.net>#1/1 X-Deja-AN: 427164648 References: <76c3tv$acs@bgtnsc02.worldnet.att.net> <76cat4$2ldc$1@news.gate.net> X-MimeOLE: Produced By Microsoft MimeOLE V4.72.2106.4 Organization: AT&T WorldNet Services Newsgroups: comp.lang.ada Date: 1998-12-30T00:00:00+00:00 List-Id: Following is a modest example of what I mean about encapsulating a stream in a protected object. The example uses Ada.Streams.Stream_Io for simplicity. You can reasonably substitute your own stream implementation as needed. Note that the file name for the log file is passed as a discriminant to the protected type. I believe this solution solves the interleaving problem. The read and write operations will be atomic. The write operation will not complete until all 'write operations for the tagged type complete. The key is that the task calling the protected write and read operations is ignorant of the use of a stream. No stream operations occur outside the protection of the protected type. -------------------------------------------------------------------------- -- Simple Example of using streams in a protected object for access by -- multiple tasks. -------------------------------------------------------------------------- package Type_Hierarchy is type Base_Type is tagged private; -- Add dispatching operations to the Base_type type First_Child is new Base_Type with private; -- Add or override operations for First_Child private type Base_Type is tagged record Date : String(1..10); Time : String(1..8); end record; type First_Child is new Base_type with record Voltage : Float; end record; end Type_Hierarchy; with Type_Hierarchy; use Type_Hierarchy; with Ada.Steams.Stream_Io; package Multi_Task_Logging is protected type Log_Stream (Log_Name : String) is Procedure Write(Item : in Base_Type'Class); Entry Read(Item : out Base_Type'Class); private File : Ada.Streams.Stream_Io.File_Type; Log_Access : Ada.Streams.Stream_Io.Stream_Access := Ada.Streams.Stream_Io.Stream(File => File); end Log_Stream; end Multi_Tasking_Logging; package body Multi_Tasking_Logging is protected body Log_Stream is Procedure Write(Item : in Base_Type'Class) is begin if not Ada.Streams.Stream_Io.Is_Open(File) then Ada.Streams.Stream_Io.Open(File => File, Name => Log_Name, Mode => Ada.Streams.Stream_Io.Append_File); else Ada.Streams.Stream_Io.Reset(File => File, Mode => Ada.Streams.Stream_Io.Append_File); end if; Base_Type'Class'Write(Stream => Log_Access, Item => Item); end Write; Entry Read(Item : out Base_Type'Class) when not Ada.Streams.Stream_Io.End_of_File(File) is begin if not Ada.Streams.Stream_Io.is_Open(File) then Ada.Streams.Stream_Io.Open(File => File, Name => Log_Name, Mode => Ada.Streams.Stream_Io.In_File); else Ada.Streams.Stream_Io.Reset(File => File, Mode => Ada.Streams.Stream_Io.In_File); end if; Base_Type'Class'Read( Stream => Log_Access, Item => Item); end Read; end Log_Stream; End Multi_Tasking_Logging; ----------------------------------- Jim Rogers Colorado Springs, Colorado USA