[squeak-dev] Changeset: Eliminating global state from Morphic

Marcel Taeumel marcel.taeumel at hpi.de
Mon Sep 28 11:45:49 UTC 2020


Hi Christoph.

Of course. That's what Shout does. Yet, you have to ensure that you only work on your own objects in #doExpensiveComputation. You can make a #copy like Shout does.

Best,
Marcel
Am 28.09.2020 13:40:54 schrieb Thiede, Christoph <christoph.thiede at student.hpi.uni-potsdam.de>:
Hi Marcel,

probably the example I have given was oversimplified. Please give me another chance to give a better example. :-)

Imagine a CalculatorMorph, which has the purpose to run an expensive computation (#doExpensiveComputation). When the morph is clicked, it should start the computation, and when it is clicked again, it should abort the computation. During the computation, unfortunately, an error could be raised which's handling logic might communicate to UI elements (such as a FileDoesNotExistException (#inform:) or a SyntaxError (#openAsTool)).

So I would implement #mouseDown: in the following way:

mouseDown: anEvent
    status caseOf: {
        [#ready] -> [
            computation := [
                result := self doExpensiveComputation.
                self world addDeferredUIMessage: [
                    self showResult: result]] forkAt: Processor userBackgroundPriority.
            self status: #computing].
        [#computing] -> [
            computation terminate.
            self status: #ready] }

Isn't this a justified reason to use forks in UI code? :-)

Best,
Christoph

Von: Taeumel, Marcel
Gesendet: Montag, 28. September 2020 13:14:26
An: Thiede, Christoph; squeak-dev
Betreff: Re: AW: AW: [squeak-dev] Changeset: Eliminating global state from Morphic
 
Hi, all!

> Don't do it. Never. Please. Avoid #fork for such things. Use #deferredUIMessages.

Let me clarify this a little bit more. :-) Given that the OpenSmalltalk VM offers cooperative process scheduling with priority-based interrupting and round-robin for each priority level, a simple #fork from the UI process will do no harm. That forked process will get the same priority as the UI process and thus safely run (and likely finish) the next time the UI process yields after each cycle. However, Squeak is in the lucky position to enable experimentation with in-image process scheduling algorithms ... if you fancy. :-) Writing code that relys on the current scheduling behavior may break for those experiments. Given that we already have those deferred-ui-messages, there is no reason not to use them.

Best,
Marcel
Am 28.09.2020 13:00:06 schrieb Marcel Taeumel <marcel.taeumel at hpi.de>:
Hi Christoph.

> In this trivial example, a deferred UI message for attaching the morph would obviously be the better choice

Not only better but the only correct way without risking debuggers. ;-)  You should not just #fork UI code or any code for that matter that makes shared use of an object. In your example, the problems come through the use of "self" as well as the access to hand morph.

Don't do it. Never. Please. Avoid #fork for such things. Use #deferredUIMessages.

Best,
Marcel
Am 28.09.2020 12:55:46 schrieb Thiede, Christoph <christoph.thiede at student.hpi.uni-potsdam.de>:
Hi all,

Dave, I double-checked it. When loading the second changeset, can you confirm that you used the new option in the drop handler dialog?



Marcel, I absolutely see that you should not run two UI processes side by side in the sense that both of them handle events at the same time. (As a consequence of some attempts in my image, I accidentally ran into this situation a few times (2 worlds active at the same time), and in practice, about every second event is just ignored.) I was only talking about deferred event-handling actions, such as:

mouseDown: anEvent
    [|result|
    result := self doLongComputation.
    self currentHand attachMorph: result asMorph] fork.

In this trivial example, a deferred UI message for attaching the morph would obviously be the better choice, but in other examples, this is not so easy, for example when some application logic raises a FileDoesNotExistException which opens some UI stuff again ... We could, of course, try to use more deferred UI messages in such cases and maybe use Promises or some other mechanism to resume the background process after that, but I am not sure we can completely get rid of any sends to activeHand, activeWorld, and activeEvent in every non-UI process.
What do you think? :-)

Best,
Christoph
Von: Taeumel, Marcel
Gesendet: Montag, 28. September 2020 11:01:05
An: Thiede, Christoph; squeak-dev
Betreff: Re: AW: [squeak-dev] Changeset: Eliminating global state from Morphic
 
Hi Christoph.

>> It's true that in Morphic there is one distinct Process associated
>> with the user interface for each Morphic world.
> I'm not so sure about this. You can always do forks within UI code, 
> so sometimes there are also multiple Processes within one Morphic 
> world. Just think about Shout's background process or debugging
> processes, and please keep in mind that Jakob justifiably proposed to
> renew the process handling for non-modal dialogs, which maybe could
> also result in multiple processes being active at the same time.


There is (and should) only be a single UI process running in Morphic at any time. All other processes that can be scheduled must synchronize with that single UI process when doing UI stuff. Shout, for example, does that via #addDeferredUIMessage:, which is effectively a SharedQueue (using a Mutex or similar). Having two UI processes running (or be scheduled) at the same time will produce strange side effects or even lock up the image. I don't know. At least, things will slow down.

You could change that by re-desiging the ownership partitioning of UI objects. Even then, there will be some process responsible for a set of UI objects. All other processes must synchronize into that one when talking to those (foreign) UI objects.

I forgot. When does process switching happen through higher-priority processes having a semaphore ready (i.e. Delay >> #wait)? At byte-code level? Oder message-send level? Well, you might not see the effects of omitting such process synchronization immediately. However, occasionally, debuggers with "nil does not understand" may pop up. Causing you headaches. :-)

Best,
Marcel
Am 24.09.2020 14:08:56 schrieb Thiede, Christoph <christoph.thiede at student.hpi.uni-potsdam.de>:
Hi all,

sorry for the long delay on this issue. I'm attaching a new version of the changeset including the following changes:

* Use DynamicVariables for storing the active variable states
* Refactor and reduce the visibility of active variable accessors on Object
* Clean up implementations of #activeHand and #primaryHand

@Dave I think associating these objects to Process is an implementation detail of DynamicVariables which I would not like to denigrate in general.

[http://www.hpi.de/]

> It's true that in Morphic there is one distinct Process associated with the user interface for each Morphic world.

I'm not so sure about this. You can always do forks within UI code, so sometimes there are also multiple Processes within one Morphic world. Just think about Shout's background process or debugging processes, and please keep in mind that Jakob justifiably proposed to renew the process handling for non-modal dialogs, which maybe could also result in multiple processes being active at the same time.
A world can have multiple hands, for example, RemoteHandMorphs (Nebraska), event-simulating hands (EventRecorder), or just multiple plain HandMorphs as demonstrated by Tony recently in his PostmarketOS implementation. There is no reason for these hands to run on the same process. Analogously, I don't see why we should restrict a hand not to be thread-safe, so the hand-event relation is 1:n again.

PS: Thanks for the hint to the new wiki page!

Best,
Christoph

Von: Squeak-dev <squeak-dev-bounces at lists.squeakfoundation.org> im Auftrag von David T. Lewis <lewis at mail.msen.com>
Gesendet: Montag, 14. September 2020 17:49:34
An: The general-purpose Squeak developers list
Betreff: Re: [squeak-dev] Changeset: Eliminating global state from Morphic
 
On Mon, Sep 14, 2020 at 11:17:53AM +0000, Thiede, Christoph wrote:
> Hi Dave,
>
>
> I agree that could try to place the active variables at better places than the most generic Object class. Still, I am not sure whether you are arguing against the thread-local storage of their values - which I do find pretty important for enabling concurrency.
>
>
> Of course, we could move my proposed implementation #activeWorld down to Project, and on Object, forward the request to "Project current activeWorld". Still, I do not think that an instance variable would be the right way to store the world, because it is not thread-local. If you would like to do this, we should implement some kind of PluggableThreadLocalVariable as proposed below, and store an instance of this class in Project.
>
>
> What do you think?
>

Hi Christoph,

The thing that I like (a lot) about your changes is that they make it
much easier to see and understand the accesses to the global variables.
The references now all go through a small number of methods in Object,
so that you can see what they do now.

Clearly we would want to leave this logic in Object, since we don't
want to add more clutter to its protocol. But you have collected the logic
in one place now, which makes it easier to think about where it ultimately
should go, and that's great.

But where do these things actually belong?  Thinking in terms of the
responsiblities of objects in the system [1], I don't think that associating
them with a Process seems to be a good fit. It's true that in Morphic there
is one distinct Process associated with the user interface for each Morphic
world. So you can make things work by associating these variables with
a Process, but it seems like an unnatural fit to me. A Morphic world has
a process, but a Process is not responsible for knowing the state of
the Morphic world.

Dave

[1] https://en.wikipedia.org/wiki/Responsibility-driven_design#:~:text=Responsibility%2Ddriven%20design%20is%20a,information%20that%20the%20object%20shares [https://en.wikipedia.org/wiki/Responsibility-driven_design#:~:text=Responsibility%2Ddriven%20design%20is%20a,information%20that%20the%20object%20shares].


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20200928/e7f4681e/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: pastedImage.png
Type: image/png
Size: 32042 bytes
Desc: not available
URL: <http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20200928/e7f4681e/attachment.png>


More information about the Squeak-dev mailing list