In Smalltalk images that include a user interface (Common Widgets), the startUp class (System startUpClass) is responsible for providing a polling loop for the user interface process, an instance of UIProcess. The UIProcess sends the message messageLoop to the startUp class to start the polling loop. The startUp class, EsWindowSystemStartUp, implements a simple polling loop similar to the one shown below:
messageLoop "Run the dispatch loop." [true] whileTrue: [ CwAppContext default readAndDispatch ifFalse: [ CwAppContext default sleep ]]
In general, application programmers never need to modify this code because it provides full functionality for all but the most exceptional circumstances. However, as mentioned above, application programmers can replace this loop with their own.
The message loop makes use of two methods defined in class CwAppContext:
As long as there is any activity in the user interface, the UIProcess will continue to poll for events as quickly as possible. As soon as the activity stops, the UIProcess becomes inactive and suspends. This enables any other Smalltalk processes that are running at the the same or lower priority than the UIProcess to execute.
Because sending CwAppContext messageLoop can deactivate the UIProcess, there must be a mechanism for reactivating it. To support this, sleep enables an operating-system-specific mechanism that causes the private message CwAppContext default wake to be sent when new events become available. This wake method is also sent by all other methods that generate user interface activity, causing the UIProcess to respond immediately.
If the underlying operating system does not provide any mechanism for triggering user-written code when events become available, the CwAppContext can still function by generating a Smalltalk process that wakes the UIProcess at regular intervals. By default, this is called the "CwAsyncIOProcess".
As previously mentioned, if there is no user interface activity the UIProcess is deactivated, enabling other Smalltalk processes at the same or lower priority to run. However, if there are no other active processes to run, a system-provided "idle" process is run, which repeatedly sends the suspendSmalltalk message to the default CwAppContext:
As soon as input is available, both the IBM Smalltalk system and the UIProcess are reactivated, because they are higher priority than the idle process.
If the operating system does not provide a facility for suspending execution of an application until input is available, the suspendSmalltalk method simply returns to its sender. In this case, Common Widgets continues to run normally, but IBM Smalltalk competes for resources with any other applications being run by the operating system.
Note that there is an interaction between the suspendSmalltalk method and the Smalltalk Delay class. If the idle process runs because a higher priority process has suspended on a Delay, the system must be reactivated when the Delay expires. This situation is handled in one of three ways depending on the capabilities of the operating system: