concept read - write lock pattern in category aspectj

appears as: read-write lock pattern, The read-write lock pattern, read-write lock pattern
AspectJ in Action, Second Edition

This is an excerpt from Manning's book AspectJ in Action, Second Edition.

Consider a situation where you need to apply the read-write lock pattern, which needs a lock per object. Ideally, you’d like to create a reusable aspect given that the pattern itself is reusable. The per-object association feature provides a mechanism to associate a new aspect instance with each execution or target object. In the following snippet, a new aspect instance is associated with each new execution object using perthis(), which matches the abstract access() pointcut (see chapter 13 for a complete example of this pattern implemented using AspectJ):

Many concurrency-control patterns exhibit symptoms of crosscutting concerns, and AOP can help simplify their implementation. In this chapter, we’ll examine how AOP can provide a level of consistency that is seldom achieved in conventional implementations. Specifically, we’ll discuss modularizing the implementation of two patterns: a Swing thread-safety pattern and the read-write lock pattern.

Consider an implementation of the InventoryService interface from appendix A that uses a grid-based cache (implemented as the Map interface) to avoid round trips to the database. Because inventory is queried more frequently than it’s modified, and because access to the grid-based map is expensive, using the read-write lock pattern for such an implementation is appropriate.

The read-write lock pattern uses a pair of locks: the read lock and the write lock. Multiple threads can simultaneously acquire the read lock as long as the write lock hasn’t been acquired. Only one thread, on the other hand, can acquire the write lock at any moment as long as no read locks have been acquired. Each operation in the class that needs this pattern must acquire and release the read lock for readonly operations and the write lock for state-altering operations. When the operation is over, it must release the lock that it acquired. Listing 13.17 shows the InventoryServiceCachedImpl class with the necessary modifications.

Listing 13.17. InventoryServiceCachedImpl with the read-write lock pattern

@ReadOnly is an annotation that describes an objective characteristic and as such says nothing about the locking mechanism. Therefore, to indicate the use of read-write lock pattern, you create the @ReadWriteLockManaged annotation, as shown in listing 13.20. Note the use of @Inherited to apply the annotation to subclasses.

Listing 13.20. Annotation to mark a class suitable for the read-write lock pattern
package ajia.concurrent; import ... @Target(ElementType.TYPE) @Inherited public @interface ReadWriteLockManaged { }

Finally, you’re ready to write the subaspect based on these annotations. Listing 13.21 shows an annotation-driven subaspect to apply the read-write lock pattern.

Listing 13.21. Annotation-driven subaspect for the read-write lock pattern

13.3. Modularizing the read-write lock pattern

The read-write lock pattern offers maximal liveliness while ensuring the integrity of objects. It’s an appropriate pattern in situations where you expect the reader threads to access the object more frequently than the writer threads, and the operations are expensive (to justify the use of acquiring and releasing locks). The fundamental idea is that any number of readers can simultaneously read the state of the object as long as no thread is modifying the state at the same time.