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.

The Semaphore class provides facilities for achieving simple synch ronization, it is simple because it<br />

only allows for two forms of communication signal <strong><strong>an</strong>d</strong> wait. Essentially, a signal puts a 1 in the<br />

queue representing the semaphore <strong><strong>an</strong>d</strong> wait pops a 1 off the semaphore. The adv<strong>an</strong>tage of this approach<br />

is simplicity <strong><strong>an</strong>d</strong> eff iciency. the disadv<strong>an</strong>tage of this approach is that more complex synchronization is<br />

not possible. Of course, more complex synchronization c<strong>an</strong> be achieved using semaphores <strong><strong>an</strong>d</strong> shared<br />

queues, but the key thing is that it is not directly supported.<br />

When a process sends a wait message to a semaphore, that process will only be allowed to proceed<br />

if a corresponding signal has been sent to the semaphore. If there is no corresponding signal, then the<br />

process will be suspended. It will only be resumed when such a signal is sent.<br />

Semaphores are ordered queues. If five waits have been sent to a semaphore then the fifth wait will<br />

only be serviced once five signals have been sent to the same semaphore. This me<strong>an</strong>s that they pay no<br />

attention to the priority of the process ( unlike the scheduler). A high -priority process must wait in line<br />

for a signal in just the same way as a low priority process.<br />

There are two inst<strong>an</strong>ce creation methods for the Semaphore class. These are:<br />

Semaphore new.<br />

Semaphore<br />

forMutalExclusion.<br />

This creates a new empty semaphore.<br />

This creates a new semaphore with a single signal in it.<br />

This is a special type of semaphore which is used in<br />

association with a special message critical <strong><strong>an</strong>d</strong> provides<br />

for mutual exclusion.<br />

As stated above there are two synchronization messages; signal <strong><strong>an</strong>d</strong> wait:<br />

aSemaphore signal. This increments the semaphore signal count.<br />

aSemaphore wait. This increments the semaphore wait count <strong><strong>an</strong>d</strong> causes the<br />

sender to suspend if fewer signals were previously sent.<br />

An additional messag e to a semaphore is critical:. This should only be sent to a semaphore<br />

which was created using the forMutalExclusion inst<strong>an</strong>ce creation message. The format of this<br />

message expression is:<br />

aSemaphore critical: aBlock<br />

This is useful if the block is accessing shared information. This message only allows the block to<br />

execute if no other block controlled by the same semaphore is executing; otherwise, it causes the active<br />

process to suspend until the block c<strong>an</strong> be executed. In effect it is performing the following process:<br />

• Send a wait message to the semaphore.<br />

• Executes the block.<br />

• Send a signal message to the semaphore.<br />

31.3.2 Shared queues<br />

When distinct processes access shared objects, then the access to those objects must be carefully<br />

controlled. For example, if two objects were to access a common set of data <strong><strong>an</strong>d</strong> one was in the process<br />

of adding some data, then when <strong>an</strong>other (higher priority) process pre -empts it <strong><strong>an</strong>d</strong> attempts to perform a<br />

different addition operation, the original ch<strong>an</strong>ges might well be lost. Since the ori ginal addition was not<br />

completed the set could be in a partially modified state, this could have extremely unpredictable results.<br />

<strong>Smalltalk</strong> does not provide protected collection classes (although again these could be implemented<br />

using semaphores etc.). Ins tead, <strong>Smalltalk</strong> provides a SharedQueue class which guar<strong>an</strong>tees that only<br />

one process will be able to access it at a time <strong><strong>an</strong>d</strong> that the expression being performed will execute to<br />

completion without being pre -empted. The design of a shared qu eue is essentially that presented in the<br />

Purple Book [Goldberg <strong><strong>an</strong>d</strong> Robson 1989], in Chapter 15 starting on page 258-265.<br />

Elements c<strong>an</strong> be placed in a shared queue using the nextPut: message, while elements c<strong>an</strong> be read<br />

from a shared queue using the next message. If no elements are currently in the shared queue when a<br />

next message is sent, then the sending process is suspended until a nextPut: message is sent to the<br />

queue. If three successive next messages are sent to <strong>an</strong> empty shared queue by three separate<br />

263

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

Saved successfully!

Ooh no, something went wrong!