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,FREEMAIL_FROM autolearn=unavailable autolearn_force=no version=3.4.4 X-Received: by 10.236.23.230 with SMTP id v66mr16823724yhv.53.1405613534102; Thu, 17 Jul 2014 09:12:14 -0700 (PDT) X-Received: by 10.50.20.168 with SMTP id o8mr512157ige.12.1405613533990; Thu, 17 Jul 2014 09:12:13 -0700 (PDT) Path: border1.nntp.dca1.giganews.com!nntp.giganews.com!j15no1237978qaq.0!news-out.google.com!bp9ni939igb.0!nntp.google.com!h18no3150864igc.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Thu, 17 Jul 2014 09:12:13 -0700 (PDT) In-Reply-To: <50bdb713-7ce1-411b-810b-9bdee1d26b7a@googlegroups.com> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=66.126.103.122; posting-account=KSa2aQoAAACOxnC0usBJYX8NE3x3a1Xq NNTP-Posting-Host: 66.126.103.122 References: <50bdb713-7ce1-411b-810b-9bdee1d26b7a@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: Subject: Re: Timeouts in Ada From: Adam Beneschan Injection-Date: Thu, 17 Jul 2014 16:12:14 +0000 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Xref: number.nntp.dca.giganews.com comp.lang.ada:187653 Date: 2014-07-17T09:12:13-07:00 List-Id: On Wednesday, July 16, 2014 10:18:18 PM UTC-7, NiGHTS wrote: > First of all I just want to say how much I appreciate the help you have g= iven me so far with the questions I have had. >=20 > For this next question I am interested in creating a "wrapper" around cer= tain tasks that should have a timeout period. I feel this feature is part o= f what makes Ada so good at keeping a software running stable. There are pl= enty of network and I/O tasks that I would love to have a "fallback" plan i= n the event that they take too long to execute.=20 >=20 > For study on this subject I am using this as a reference: >=20 > http://en.wikibooks.org/wiki/Ada_Programming/Tasking#Timeout >=20 > I am looking at Ex. 10.=20 >=20 > task Password_Server is > entry Check (User, Pass : in String; Valid : out Boolean); > entry Set (User, Pass : in String); > end Password_Server; > ... > User_Name, Password : String (1 .. 8); > ... > Put ("Please give your new password:"); > Get_Line (Password); > select > Password_Server.Set (User_Name, Password); > Put_Line ("Done"); > or > delay 10.0; > Put_Line ("The system is busy now, please try again later."); > end select; >=20 > Now as an Ada beginner with a strong background in C/Asm I interpret this= example in the following way... >=20 > 1. "select" creates a thread which runs the body of "select" or the body = of "or". Or maybe it creates two threads for each and blocks its own thread= until one finishes. I think the mistake you're making is treating the two sequences of statemen= ts=20 Password_Server.Set (User_Name, Password); Put_Line ("Done"); and=20 delay 10.0; Put_Line ("The system is busy now, please try again later."); as independent "bodies". That's not how SELECT works. The first statement following SELECT is treat= ed specially. It is restricted; it can only be an entry call, an ACCEPT, o= r a DELAY (the DELAY is possible only if there's a "THEN ABORT" part). The= first statement following OR is also restricted, and is treated specially;= if you're selecting on an entry call, it *must* be a DELAY. So instead of looking at this as two separate blocks of code and trying to = figure out how the task executes those two blocks, you need to look at thos= e first statements to see what the SELECT's purpose is. And when you have = SELECT ... OR DELAY 10.0, it means that the task waits until t= he entry is available, but it only waits 10 seconds and then gives up. And= then, depending on whether the entry call timed out or not, it executes on= e or the other of the statements that *follow* the first statement. This d= oesn't require separate threads trying to execute both blocks. =20 Look at it more like this [this is not real Ada code, of course]: if Try_Entry_Call (Password_Server.Set (User_Name, Password), Timeout = =3D> 10.0) =3D ACCEPTED then Wait_For_Rendezvous_To_Complete; Put_Line("Done"); else Put_Line("The system is busy now, please try again later.");=20 end if; If the first statement after SELECT is an ACCEPT or DELAY, or if there's a = THEN ABORT clause, the statement will have different semantics, and it won'= t look like the IF statement I wrote above. There are four different kinds= of SELECT. -- Adam >=20 > 2. In either case the variables in scope where "select" was run will be a= ccessible to both the thread created to deal with the body of "select" and = the body of "or" >=20 >=20 >=20 > 3. The "Password_Server" task is run in its very own thread so it has its= own internal scope of variables. So this would mean that the "select" body= is dealing with two nested threads. >=20 >=20 >=20 > 4. If the "select" body completes before the "or" body is completed, the = "or" thread is terminated. If the "or" body completes before the "select" b= ody completes, the "select" body and the Password_Server (if applicable) is= terminated. >=20 >=20 >=20 > 5.a. If both threads happen to reach the "Put_Line" command of each body,= both threads will terminate and there would be a conflict of information -= - It will confuse the user with printing both "Done" AND "The system is bus= y". If this was a more mission-critical operation, both the main operation = and its fallback plan may execute at the same time. In other words I don't = see how this is atomic, and if its not atomic how could this possibly be us= eful to anyone? >=20 >=20 >=20 > 5.b. Terminating threads while its doing things (while writing to shared = memory? performing network operations mid-way?) sounds disastrous, but it s= eems like the only way this could possibly function in Ada. >=20 >=20 >=20 > Now keep in mind these are all assumptions I made in order to make sense = of what I am reading. So far in my self-study of Ada 80% of everything had = to be assumed with the help of trial and error because no literature that I= have read explained WHY things happen, only superficial usage syntax. Its = rather annoying but that is why I am so thrilled to be able to ask this que= stion here. >=20 >=20 >=20 > Thank you again for all your help!