P. 1
Wiley.pattern.oriented.software.architecture.volume.4.May.2007

Wiley.pattern.oriented.software.architecture.volume.4.May.2007

|Views: 170|Likes:
Published by Arun Nair

More info:

Published by: Arun Nair on Jun 09, 2012
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

11/24/2015

pdf

text

original

When realizing a REQUESTOR (242), CLIENT REQUEST HANDLER (246),
SHAREDREPOSITORY(202),PAGECONTROLLER(337),FRONTCONTROLLER(339),
APPLICATION CONTROLLER (341), COMMAND PROCESSOR (343), THREAD-SAFE
INTERFACE (384), or DOUBLE-CHECKED LOCKING (386) . . .

. . . a key issue in multi-threaded programming is the selection of the
appropriate locking strategy for a particular environment.

Components that are shared across threads in multi-threaded
environments must protect their critical sections from con-
current access. However, different software configurations may
require different locking strategies, such as mutexes, readers–
writer locks, or semaphores.

Hard-coding the locking strategy into the components and adapting it
to specific environments is a straightforward solution to this problem,
but infeasible for most applications. Components would depend on
their environment, and whenever that environment changes and sug-
gests a different locking strategy, all components must be updated,
with corresponding maintenance overhead. Providing different com-
ponent versions for each environment is also infeasible—it causes
similar maintenance overhead. Ideally, it should be possible to
customize a component’s locking strategy without making its imple-
mentation dependent on a specific environment.

Therefore:

Define locks in terms of ‘pluggable’ types, with each type objecti-
fyingaparticular synchronization strategy. Provideall typeswith
a common interface, so that a component can use all lock types
uniformly without being dependent on their implementation.

begin## Enter the critical section.
## Acquire the lock.

lock.acquire ();

## Execute the critical section.

do_something ();

end## Leave the critical section.

## Release the lock.

lock.release ();

acquire

release

Lock

configure

service

Component

Configure component with lock

Call method

1

2

3

4

Strategized Locking **

389

Configure the component with an instance of the appropriate lock
type to use at its creation or declaration time, for example by passing
a lock object to its constructor or parameterizing the component’s
class with a particular lock type. Use this lock instance to protect all
critical sections within the component.

ASTRATEGIZEDLOCKINGdesign offers several benefits. Ratherthana sep-
arate implementation for each concurrency model, there is only one
core implementation. Enhancements and bug fixes for a component
therefore do not have to be duplicated. Configuring and customizing
a component for specific concurrency models is simple and non-
intrusive for the component, because the synchronization aspects of
components are strategized. Conversely, STRATEGIZED LOCKING exposes
a parameterization decision to the component user, which may be
considered intrusive for common cases. Nevertheless, such an open
and orthogonal approach allows the component to be used beyond
its original context.

To make locks ‘pluggable,’ define a lock acquisition and release
STRATEGY (455) interface that is implemented by all concrete lock
types. Use the same lock type to configure related components within
the same application. Where a variation in lock implementation is
with respect to the platform rather than the locking policy, make con-
crete lock types WRAPPER FACADES (459) that encapsulate the details of
aparticularplatform-specificlockingmechanism.Tooptimizecompo-
nents for single-threaded environments where no locking is needed,
provide a null lock type, which is a NULL OBJECT (457) whose lock
acquisition and release methods are empty ‘no-ops.’

SCOPED LOCKING (390) helps to simplify and automate safe acquisition
and release of the lock within the component’s implementation.

390

Synchronization

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->