comp.lang.ada
 help / color / mirror / Atom feed
From: "Björn Lundin" <b.f.lundin@gmail.com>
Subject: Re: can someone help me with this code (explanation)
Date: Fri, 26 Sep 2014 09:04:52 +0200
Date: 2014-09-26T09:04:52+02:00	[thread overview]
Message-ID: <m0335c$mto$1@dont-email.me> (raw)
In-Reply-To: <m024t3$j1p$1@dont-email.me>

On 2014-09-26 00:27, Jeffrey Carter wrote:
> On 09/25/2014 01:10 PM, Niklas Holsti wrote:
>>
>> To make task-safe use of Ada.Text_IO, it should be wrapped in some way
>> to ensure that only one task calls it at a time. For example, you could
>> write a task with an entry like Put_Line (Message : in String) and an
>> accept statement that does Ada.Text_IO.Put_Line (Message). But a
>> protected-object wrapper is probably better for uses where I/O
>> performance (latency) is not critical.
> 
> Except that I/O is potentially blocking, and so cannot appear in a protected
> operation, meaning one needs a task one way or another.
> 
Or by using a protected object as a semaphore



   protected type Semaphore_Type is
      procedure Release;
      entry Seize;
   private
      In_Use : Boolean := False;
   end Semaphore_Type;

   protected body Semaphore_Type is
      procedure Release is
      begin
         In_Use := False;
      end;
      entry Seize when not In_Use is
      begin
         In_Use := True;
      end;
   end Semaphore_Type;

  Semaphore : Semaphore_Type;


  procedure Do_Safe_Put_Line(What : String) is
  begin
    Semaphore.Seize;
    Ada.Text_Io.Put_Line(What);
    Semaphore.Release;
  end Do_Safe_Put_Line;

  procedure Do_Safe_Put(What : String) is
  begin
    Semaphore.Seize;
    Ada.Text_Io.Put(What);
    Semaphore.Release;
  end Do_Safe_Put;



Using Do_Safe_Put or Do_Safe_Put_Line
can be used by any of the tasks.

If several tries to use them at the same time,
they will line up in the Seize statement,
and go ahead whe the task holding the semaphor calls Release.


--
Björn


  reply	other threads:[~2014-09-26  7:04 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-09-25 13:44 can someone help me with this code (explanation) Stribor40
2014-09-25 14:56 ` Björn Lundin
2014-09-25 15:01   ` Björn Lundin
2014-09-25 15:54   ` Stribor40
2014-09-25 17:11     ` Niklas Holsti
2014-09-25 19:41       ` Stribor40
2014-09-25 19:53         ` Jeffrey Carter
2014-09-25 20:10         ` Niklas Holsti
2014-09-25 22:27           ` Jeffrey Carter
2014-09-26  7:04             ` Björn Lundin [this message]
2014-09-26  7:58               ` J-P. Rosen
2014-09-26 10:24                 ` G.B.
2014-09-26 11:48                 ` Björn Lundin
2014-09-26 12:49                   ` J-P. Rosen
2014-09-26 14:15                     ` Björn Lundin
2014-09-26 14:49                       ` J-P. Rosen
2014-09-26 15:31                         ` Björn Lundin
2014-09-26 16:08                           ` G.B.
2014-09-30  4:22                             ` Brad Moore
2014-09-30 21:59                               ` Georg Bauhaus
2014-10-01 15:38                                 ` Brad Moore
2014-09-26 16:23                           ` Dmitry A. Kazakov
2014-09-26 20:38                           ` J-P. Rosen
2014-09-27 18:16                             ` Björn Lundin
2014-09-26 16:43                       ` Niklas Holsti
2014-09-26 16:45                 ` Niklas Holsti
2014-10-09  2:45                   ` Randy Brukardt
2014-09-26 16:44             ` Niklas Holsti
2014-09-26  5:06           ` J-P. Rosen
2014-09-25 16:53 ` Jeffrey 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