On Dec 21, 2007 10:41 AM, Igor Stasenko <siguctua@gmail.com> wrote:
On 20/12/2007, Michael van der Gulik <mikevdg@gmail.com> wrote:
>
>
> On Dec 21, 2007 8:09 AM, Igor Stasenko < siguctua@gmail.com> wrote:
> >
> > On 20/12/2007, Tom Phoenix <rootbeer@redcat.com> wrote:
> > > On 12/20/07, Igor Stasenko < siguctua@gmail.com> wrote:
> > >
> > > > This is essentially useful when you need to guarantee that process
> > > > will stay suspended even if it's currently suspended waiting for
> > > > semaphore signal.
> > >
> > > Do you mean to say that your processes resume running before their
> > > semaphores are signaled?
> > >
> >
> > Just try to run given code:
> >
> > | sema proc |
> > sema := Semaphore new.
> > proc := [ sema critical: [ Transcript show: 'Oopsie' ] ] fork.
> > Processor yield.
> > proc suspend.
> > proc resume.
> >
<snip>
No, i'm specially doing Processor yield to make proc start waiting for
semaphore.

Er... oh, I see. From my point of view (which consists of little format training wrt Process management), you're mis-treating these poor objects. The code would work... but I'd never write code like that.

If you want to use #critical:, you should be making the Semaphore with "sema := Semaphore forMutualExclusion".

"Processor yield" shouldn't ever be used IMHO. It relies on particular behaviour of the scheduler. In a future version of Squeak when we properly support pthreads or we "improve" the behaviour of the scheduler, your code might not work as expected.


> I can't try this code at the moment - I don't have Squeak nearby.
>
> Generally, I'd rarely use Process>>suspend in my code. Semaphores provide
> the behaviour that you want; a Semaphore is a linked list of waiting
> processes. When you call #wait on a semaphore, your process gets added to
> the list. When you call #signal, the process at one of the ends of the list
> (FIFO? LIFO? Can't remember) gets resumed. The effect of #suspend and
> #resume should have no effect on the signalled/waiting state of a Process.
>
> In the Launcher example that you give, it would be okay to use #suspend and
> #resume, but there shouldn't be anything else in the image which is trying
> to suspend or resume those processes.
>
Yes, i was using it like:

suspendedList := OrderedCollection new.
(Process allInstances select:[:proc | proc isActiveProcess not and: [
proc isSuspended not ]]) do: [:proc | suspendedList add: proc. proc
suspend].

<... do my code...>

suspendedList do: [:proc | proc resume ].

But because of semaphores bug, after i sending #resume to processes
which waiting on semaphore signal causing them think that signal is
received.
Btw you can try this code yourself and see how low space watcher pops
up scaring you to death :)


I'll try this when I get home. I do a lot of multithreaded programming in Squeak so its important to me that this stuff works right.

Gulik.

--
http://people.squeakfoundation.org/person/mikevdg
http://gulik.pbwiki.com/