Timeouts for SharedQueue and Semaphore

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Timeouts for SharedQueue and Semaphore

Holger Freyther
Hi all,

would it be possible to add a waitTimeSeconds/MSecs to Semaphore? Maybe even
to SharedQueue? I would like to write code like the one below but I am a bit
at a loss of how to realize it without spawning many processes.


[
  self sendSomeData
  c := queue nextTimeout: 3.
] on: TimeOut do: [:e | ... ]

thanks

_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Timeouts for SharedQueue and Semaphore

Paolo Bonzini-2
On 11/10/2010 12:51 AM, Holger Hans Peter Freyther wrote:
> would it be possible to add a waitTimeSeconds/MSecs to Semaphore?
> Maybe even to SharedQueue? I would like to write code like the one
> below but I am a bit at a loss of how to realize it without spawning
> many processes.
>
> [ self sendSomeData c := queue nextTimeout: 3. ] on: TimeOut do: [:e
> | ... ]

The right primitive to implement would be "wait for any of these
semaphores".  Then what you need would be easily written as

     nextTimeout: secs
         | delay |
         delay := (Delay forSeconds: secs).
         (Processor activeProcess waitOnAll: {delay. self}) = delay
             ifTrue: [ TimeOut signal ]

Right now the code however is dependent on each process having a single
process, so this is quite complex to do.

So it may indeed make sense to special case waiting on a Semaphore and a
Delay.  The first idea was to do something like this, forcing use of an
external semaphore in Delay:

     Semaphore>>waitTimeout: aDelay
         | sema p |
         sema := Semaphore new.
         p := [self wait. sema signal] fork.
         aDelay waitOn: sema onExternalSignal: [ ^self ].
         p terminate.
         TimeOut signal

... which still needs to fork.  Oh well. :(

However, most of the changes needed for something like
#waitOn:onExternalSignal: were a nice cleanup anyway so I went ahead
with them.  After doing this, I checked what Squeak does and the idea is
to do "process suspend; resume" to take a process out of the semaphore
wait.  This fits in the new delay code pretty well, even though the
timed-wait implementation is completely different from Squeak's.  It is
accessible via Delay>>#timedWaitOn:, patches are welcome to implement
Squeak's Semaphore-side methods instead.

On top of this, letting the caller know whether there was a timeout from
#timedWaitOn: requires a small change to the VM.  Luckily, the change is
in general a good idea.

Paolo

_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Timeouts for SharedQueue and Semaphore

Holger Freyther
On 11/10/2010 10:00 AM, Paolo Bonzini wrote:

> On top of this, letting the caller know whether there was a timeout from
> #timedWaitOn: requires a small change to the VM.  Luckily, the change is in
> general a good idea.

thanks, I will need to read it a couple of more times until I figure out what
I need to do. :)


_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Timeouts for SharedQueue and Semaphore

Paolo Bonzini-2
On 11/10/2010 10:49 AM, Holger Hans Peter Freyther wrote:
> thanks, I will need to read it a couple of more times until I figure out what
> I need to do.:)

Nothing? :)

    (Delay forSeconds: 1) timedWaitOn: sema

will wait on a semaphore for 1 second, and return true if the timeout
occurred, false if the semaphore was grabbed.

Paolo

_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Timeouts for SharedQueue and Semaphore

Holger Freyther
On 11/10/2010 10:54 AM, Paolo Bonzini wrote:
> On 11/10/2010 10:49 AM, Holger Hans Peter Freyther wrote:
>> thanks, I will need to read it a couple of more times until I figure out what
>> I need to do.:)
>
> Nothing? :)

What about the VM change for a cleanup? That is just to wait for multiple
things to happen?


>
>    (Delay forSeconds: 1) timedWaitOn: sema
>
> will wait on a semaphore for 1 second, and return true if the timeout
> occurred, false if the semaphore was grabbed.


    nextTimed: timeout [
        "Wait for an object up to timeout seconds"
        <category: 'accessing'>
        | delay expired result |

        delay := Delay forSeconds: timeout.
        expired := delay timedWaitOn: valueReady.

        expired ifTrue: [
            "Raise some kind of exception"
        ]

        queueSem critical: [result := queue removeFirst].
        ^result
    ]


so something like the above for SharedQueue would be acceptable upstream? Or
should I keep it as an extension?

_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Timeouts for SharedQueue and Semaphore

Paolo Bonzini-2
On Wed, Nov 10, 2010 at 14:47, Holger Hans Peter Freyther
<[hidden email]> wrote:
> On 11/10/2010 10:54 AM, Paolo Bonzini wrote:
>> On 11/10/2010 10:49 AM, Holger Hans Peter Freyther wrote:
>>> thanks, I will need to read it a couple of more times until I figure out what
>>> I need to do.:)
>>
>> Nothing? :)
>
> What about the VM change for a cleanup? That is just to wait for multiple
> things to happen?

That would be quite huge.  Everything else is already pushed.

>>    (Delay forSeconds: 1) timedWaitOn: sema
>>
>> will wait on a semaphore for 1 second, and return true if the timeout
>> occurred, false if the semaphore was grabbed.
>
>    nextTimed: timeout [
>        "Wait for an object up to timeout seconds"
>        <category: 'accessing'>
>        | delay expired result |
>
>        delay := Delay forSeconds: timeout.
>        expired := delay timedWaitOn: valueReady.
>
>        expired ifTrue: [
>            "Raise some kind of exception"
>        ]
>
>        queueSem critical: [result := queue removeFirst].
>        ^result
>    ]
>
>
> so something like the above for SharedQueue would be acceptable upstream?

Sure.  Just make sure that: 1) the exception is not resumable; 2) if
there's anything like that in Squeak, it's compatible.

Paolo

_______________________________________________
help-smalltalk mailing list
[hidden email]
http://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Timeouts for SharedQueue and Semaphore

Stephen-71
In reply to this post by Paolo Bonzini-2
Thanks - this is great for server side. For example the LDAP Library I
ported from Squeak used timed wait on semaphore, in the worker thread. I
can put that back in now.

>
> (Delay forSeconds: 1) timedWaitOn: sema
>
> will wait on a semaphore for 1 second, and return true if the timeout
> occurred, false if the semaphore was grabbed.


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