help-smalltalk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-smalltalk] Timeouts for BlockClosures


From: Paolo Bonzini
Subject: Re: [Help-smalltalk] Timeouts for BlockClosures
Date: Wed, 06 Apr 2011 08:45:48 +0200
User-agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) Gecko/20101209 Fedora/3.1.7-0.35.b3pre.fc14 Lightning/1.0b3pre Mnenhy/0.8.3 Thunderbird/3.1.7

Almost there indeed, thanks for clarifying!

First comment: please move the TimeoutNotification under the Kernel namespace.

+BlockClosure extend [
+    timeout: seconds do: aBlock [

So what do you think about passing a delay here? Or even making this method Delay>>#value:onTimeout:? Do you know what other Smalltalks do?

+<category: '*timeout-private'>
+       "I will execute myself for up to seconds and if a timeout
+       occurs I will invoke the aBlock. If the timeout occurs early
+       not much of the block is executed yet. I also have some issues
+       with Delays and not breaking these properly.

Is the comment still accurate?

+        [[
+
+            "Start a process to wait in and then signal"
+            [| delay |
+                delay := Delay forSeconds: seconds.
+
+                "Wait and see if it is timed out. If so send a signal."
+                (delay timedWaitOn: sem) ifTrue: [
+                   proc signalInterrupt: (TimeoutNotification on: self).
+                ].
+            ] fork.
+
+            value := self value.
+        ] ensure: [sem signal]

This can be written in a lighter way:

        [

            "Start a process to wait in and then signal"
            [| delay |
                delay := Delay forSeconds: seconds.

                "Wait and see if it is timed out. If so send a signal."
                (delay timedWaitOn: sem) ifTrue: [
                   proc signalInterrupt: (TimeoutNotification on: self)]
            ] fork.

            value := self ensure: [sem signal]

+        ] on: TimeoutNotification do: [:e |
+            e block = self
+                ifTrue:  [timeout := true]
+                ifFalse: [e pass].
+        ].
+
+        "Make sure we call the ensure's first."
+        ^ timeout
+            ifTrue:  [^aBlock value]
+            ifFalse: [^value].

No returns within ifTrue/ifFalse blocks.

+    ]
+]

+            ] timeout: 1 do: [events add: 'timeout'].

Larger timeout, please (1s for example).

Thanks!

Paolo



reply via email to

[Prev in Thread] Current Thread [Next in Thread]