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-Thread: a07f3367d7,a4db0fc323f0b09e X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!news1.google.com!npeer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!post01.iad.highwinds-media.com!newsfe23.iad.POSTED!7564ea0f!not-for-mail From: Brad Moore User-Agent: Thunderbird 2.0.0.23 (X11/20090817) MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Barrier re-evaluation issue with GNAT 4.3.2 References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: NNTP-Posting-Host: 70.72.159.148 X-Complaints-To: internet.abuse@sjrb.ca X-Trace: newsfe23.iad 1253868632 70.72.159.148 (Fri, 25 Sep 2009 08:50:32 UTC) NNTP-Posting-Date: Fri, 25 Sep 2009 08:50:32 UTC Date: Fri, 25 Sep 2009 02:50:32 -0600 Xref: g2news2.google.com comp.lang.ada:8461 Date: 2009-09-25T02:50:32-06:00 List-Id: Dmitry A. Kazakov wrote: > I was always wondering the rationale behind introducing > Ada.Real_Time.Timing_Events. The delay statement does the very same thing: > > with Ada.Real_Time; use Ada.Real_Time; > ... > Event : Time; > begin > ... > Put_Line ("Starting alarm ..."); > Event := Clock + To_Time_Span (3.0); > > Put_Line ("Waiting for alarm ..."); > delay until Event; > Put_Line ("ALARM!!"); > In this trivial example, a simple delay is probably all you need. However, consider a slightly different twist on the alarm example. Suppose you have a burglar alarm where if you trip the alarm you have 1 minute to correctly enter the security code, or the police are called. If you correctly enter the code in time the alarm is canceled. This gets you into the realm where the timing events package is more useful. You could accomplish this also using a task to monitor the timeout, but that could be considered a "heavier" approach depending on the implementation of the timing events package. Timing events provide a simpler abstraction, if all you want is an event to fire at sometime in the future. Plus if you have many such sort of events in a program, you could potentially save yourself from having to add tasks for each event you want to monitor, resulting in less system resources being used. Brad -------------------------------------------- eg. with Ada.Text_IO; use Ada; with Burglar_Alarm; procedure Test_Timers is My_Alarm : Burglar_Alarm.Alarm_Type; Code : Burglar_Alarm.Security_Code_Type; begin Text_IO.Put_Line ("Tripping alarm ..."); Burglar_Alarm.Trip (My_Alarm); while Burglar_Alarm.Is_Tripped (My_Alarm) loop begin Text_IO.Put_Line ("Enter Security Code: "); Code := Burglar_Alarm.Security_Code_Type'Value (Text_IO.Get_Line); Burglar_Alarm.Disarm (Alarm => My_Alarm, Key => Code); exception when Constraint_Error => null; -- Silently ignore bad codes end; end loop; Text_IO.Put_Line ("Entered Correct Code!"); end Test_Timers; ------------------------------------------------------------ with Ada.Real_Time.Timing_Events; use Ada.Real_Time; package Burglar_Alarm is type Alarm_Type is limited private; procedure Trip (Alarm : in out Alarm_Type); type Security_Code_Type is range 0 .. 9999; procedure Disarm (Alarm : in out Alarm_Type; Key : Security_Code_Type); -- Once an Alarm has been tripped, you have -- 1 minute to enter the security code to -- disable the alarm, or else the police are called function Is_Tripped (Alarm : Alarm_Type) return Boolean; private protected type Alarm_Type is function Is_Tripped return Boolean; procedure Start_Timer; procedure Disarm (Key : Security_Code_Type); procedure Call_Police (Event : in out Timing_Events.Timing_Event); private Timer : Timing_Events.Timing_Event; Tripped : Boolean := False; Sound_Alarm : Boolean := False; Security_Code : Security_Code_Type := 1234; -- set to some unique value; end Alarm_Type; end Burglar_Alarm; ---------------------------------------------------------- with Ada.Text_IO; use Ada; package body Burglar_Alarm is protected body Alarm_Type is procedure Call_Police (Event : in out Timing_Events.Timing_Event) is pragma Unreferenced (Event); begin Text_IO.Put_Line ("Come out with your hands up!"); end Call_Police; procedure Disarm (Key : Security_Code_Type) is Cancelled : Boolean; begin if Key = Security_Code then Timer.Cancel_Handler (Cancelled); Tripped := False; end if; end Disarm; function Is_Tripped return Boolean is begin return Tripped; end Is_Tripped; procedure Start_Timer is begin Tripped := True; Timer.Set_Handler (In_Time => Minutes (1), Handler => Call_Police'Access); end Start_Timer; end Alarm_Type; procedure Disarm (Alarm : in out Alarm_Type; Key : Security_Code_Type) is begin Alarm.Disarm (Key); end Disarm; function Is_Tripped (Alarm : Alarm_Type) return Boolean is begin return Alarm.Is_Tripped; end Is_Tripped; procedure Trip (Alarm : in out Alarm_Type) is begin Alarm.Start_Timer; end Trip; end Burglar_Alarm;