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

_______________________________________________
help-smalltalk mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/help-smalltalk

Reply via email to