A synchronized block join point for AspectJ

Chenchen Xi Bruno Harbulot John R. Gurd The University of Manchester, Oxford Road, Manchester M13 9PL, United Kingdom xic AT cs.man.ac.uk bruno.harbulot AT manchester.ac.uk jgurd AT cs.man.ac.uk

Abstract However, it is still problematic to avoid code tangling while Designing and implement model for a synchronized block effecting this conversation. join point to encapsulate crosscutting synchronization con- Aspect-Oriented Programming (AOP) has the potential cerns into single unit in AspectJ. to modularise such synchronizations so that user code can become oblivious to the distributed environment. Indeed, a Categories and Subject Descriptors D.3.3 [Programming join point based on the synchronized method has been Languages]: Language Constructs and Features proposed. However, the synchronized block has not yet General Terms Design, Language. been treated in AspectJ. This paper 1 shows how to separate the synchronization Keywords Synchronized Block Join Point, AspectJ. concern by designing and implementing models for a syn- chronized block join point and a synchronized block body 1. Introduction join point to encapsulate crosscutting synchronization con- Synchronization is a concern that developers of distributed cerns into logical units using the concepts of AOP. It is also systems must deal with whenever guarded access to a shared shown how the two new join points can help such modular- resource is required. Access to devices, files and shared ization to plug into the JMM so as to maintain its semantics, memory are all situations that typically require synchro- along with the semantics defined in the Java Language Spec- nization, and they also often require careful management of ification. The models achieve reusability of synchronized multiple threads and synchronization devices such as locks. code and thread control management in Java to such an ex- Avoiding tangling of the code responsible for these various tent that concurrency can be fully handled by a single aspect. concerns is difficult, as is encapsulating them for reuse in The models augment the capabilities of join points for syn- diverse situations. chronized methods in intercepting and modifying synchro- In the context of distributed Java programs, three distinct nization actions in distributed, Java-based, aspect oriented synchronization situations are encountered, namely, shar- software. The work is applicable in any aspect oriented en- ing data among multiple threads, sharing among clusters of vironment, but emphasis is placed on compatibility with the JVMs and sharing among clusters of physical computers. most commonly used language AspectJ. The former is the logical concern of sharing data between The proposed join point model is enhanced with a mech- concurrent threads; the latter two are practical concerns anism for removal of unnecessary synchronization, which is about the particular ‘distributed environment’ in which the vital for reducing overheads associated with the lock. There code is executed. The problem is how to distribute the logi- is also a facility for re-introducing necessary synchroniza- cally necessary Java threads transparently across the physi- tion that has previously been removed. cal vagiaries of the distributed environment. Moreover, parallel programming is often difficult sim- ply due to the complexity of dealing with lock-based syn- chronization. As a result, there have been proposals to sim- plify parallel programming by using various forms of trans- actional memory to replace lock-based synchronization in Consider the above simple example, which is captured existing parallel Java programs. Once Java Memory Model by the synchronize() . With transactional exe- (JMM) issues have been addressed, conversion of lock-based cution, there is no need to use anything other than a non- synchronization into transactions is largely straightforward. locking HashMap since the caller specifies its atomicity re- quirements. The rm proceed() method provides the ability to remove the synchronization on HashMap map and atomic Permission to make digital or hard copies of all or part of this work for personal or warp it as a transactional object. Extensions for the abc com- classroom use is granted without fee provided that copies are not made or distributed piler which implement the two new join points are briefly for profit or commercial advantage and that copies bear this notice and the full citation on the first page. To copy otherwise, to republish, to post on servers or to redistribute presented and are shown to meet the requirements of various to lists, requires prior specific permission and/or a fee. applications. Seventh International Workshop on Foundations of Aspect-Oriented Languages (FOAL 2008), April 1, 2008, Brussels, Belgium. Copyright c 2008 ACM ISBN 978-1-60558-110-1/08/0004. . . $5.00 1 Full text can be found at http://www.cs.man.ac.uk/˜xic/SBJP AspectJ.pdf