[Vm-dev] Termination semaphore for Process
Max Leske
maxleske at gmail.com
Fri Feb 12 14:59:37 UTC 2016
Hi all,
I have a proposal and I’d like to hear your opinion.
Right now, when I want to wait for a process to finish I use something like #forkAndWait or I write a block with semaphores myself. This works fine for processes that I spawn and then immediately want to wait for. But what if I spawn a process somewhere else and only later know that I want to wait for its termination? Currently I would construct a polling loop to do that.
Here’s a different idea (which I’ve tested):
1. add instance variable “terminationSemaphore” to Process
2. at the end of Process>>terminate #ensure: that all waiting processes are signaled
3. add #waitForTermination message to Process:
waitForTermination
terminationSemaphore ifNil: [ terminationSemaphore := Semaphore new ].
self resume.
terminationSemaphore wait
This enables code like the following:
result := 0.
p := [ result := 2. 2 seconds asDelay wait ] newProcess
priority: 20;
yourself.
“don’t run the new process yet"
[ p waitForTermination. result2 := 3. ] forkAt: 30.
“p is now running, the second process is waiting"
p waitForTermination.
“p is still running, two processes are waiting”
“because processes waiting on a semaphore are signaled in the order
they sent #wait we even achieve ordering (at least with equal process
priorities) and so ‘result2’ is not nil but 3”
{result. result2} “#(2 3)
I wrote this in 10 minutes, so there’s room for polishing. I just want to know if this is something other people might want and if there are potential problems with process termination and semaphores which I am not aware of.
Cheers,
Max
More information about the Vm-dev
mailing list