29.11.2014 Views

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

31. Concurrency in <strong>Smalltalk</strong><br />

31.1 <strong>Introduction</strong><br />

This chapter presents <strong><strong>an</strong>d</strong> explains a short example of how concurrency c<strong>an</strong> be accomplished wit hin<br />

<strong>Smalltalk</strong>. It also considers lazy evaluations (i.e. you only perform the evaluation if you absolutely have<br />

to), futures (you start <strong>an</strong> evaluation now which you will need later) <strong><strong>an</strong>d</strong> persistence.<br />

The remainder of the document is structured in the followin g m<strong>an</strong>ner: Section two introduces the<br />

concept of concurrency, Section three briefly discusses processes within <strong>Smalltalk</strong>. Section four<br />

introduces the concepts used in the time slicing scheduler <strong><strong>an</strong>d</strong> Section five explains how future <strong><strong>an</strong>d</strong> lazy<br />

evaluators may be implemented in <strong>Smalltalk</strong>.<br />

31.2 Concurrent processes<br />

The concepts behind object oriented programming lend themselves particularly well to the concepts<br />

associated with concurrency. For example, a system c<strong>an</strong> be described as a set of discrete obj ects<br />

communicating with one <strong>an</strong>other when necessary. In most <strong>Smalltalk</strong> implementations, only one object<br />

may be executing at <strong>an</strong>y one moment in time. However, conceptually at least, there is no reason why<br />

this restriction should be enforced. For example, the basic concepts behind object orientation still hold,<br />

even if each object executes within a separate independent process.<br />

Traditionally, a message send is treated just like a procedural call, in which the calling object’s<br />

execution is blocked until a respo nse is returned. However, we could extend this model quite simply to<br />

view each object as a concurrently executable program module, with activity starting when the object is<br />

created <strong><strong>an</strong>d</strong> continuing even when a message is sent to <strong>an</strong>other object (unless the re sponse is required<br />

for further processing). In this model, there may be very m<strong>an</strong>y (concurrent) objects all executing at the<br />

same time. Of course this introduces issues associated with resource allocation etc. but no more so th<strong>an</strong><br />

in <strong>an</strong>y concurrent system.<br />

One implication of this concurrent object model is that objects are necessarily larger th<strong>an</strong> in the<br />

traditional single execution thread approach. This is because the overhead of having each object as a<br />

process. A process scheduler for h<strong><strong>an</strong>d</strong>ling these process es <strong><strong>an</strong>d</strong> resource allocation mech<strong>an</strong>isms me<strong>an</strong><br />

that it is not feasible to have integers or characters etc. as separate processes. (Note in <strong>Smalltalk</strong> integers<br />

etc. are objects. In such a system there might be a separate type of object which is a concurrent obje ct<br />

which is assumed to be a large grained object).<br />

<strong>Smalltalk</strong> has limited built -in support for concurrency. However, it does support Processes <strong><strong>an</strong>d</strong><br />

Semaphores as primitive types. Other conventional inter -process communication <strong><strong>an</strong>d</strong> protection<br />

mech<strong>an</strong>isms such as SharedQueues <strong><strong>an</strong>d</strong> critical regions, are implemented in terms of these primitives.<br />

However, the processor scheduler (part of the virtual machine) implements a naive non -preemptive<br />

scheduling policy, with limited support for re-scheduling within a particular priority level. Further, once<br />

a high -priority process is running, no lower priority process will run until the high priority process<br />

suspends or terminates. For these reasons, the basic <strong>Smalltalk</strong> system contains only a few processes <strong><strong>an</strong>d</strong><br />

typical applications using concurrency do not create more th<strong>an</strong> a few tens of processes.<br />

However, as the source code is available to the developer it is possible to extend the scheduler such<br />

that a pre-emptive policy is introduced. This is described in Section 3 following on from a discussion of<br />

the facilities provided in <strong>Smalltalk</strong> which enable the construction of such a scheduler.<br />

261

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!