Atomic Instructions in Java Print E-mail
Article Index
Atomic Instructions in Java
2
3
4
5
All Pages



Dept. of Computer Science, University of Maryland, College Park, MD 20742 USA
{daveho,pugh,jspacco}@cs.umd.edu
Abstract. Atomic instructions atomically access and update one or
more memory locations. Because they do not incur the overhead of lock
acquisition or suspend the executing thread during contention, they may
allow higher levels of concurrency on multiprocessors than lock-based
synchronization. Wait-free data structures are an important application
of atomic instructions, and extend these performance benefits to higher
level abstractions such as queues. In type-unsafe languages such as C,
atomic instructions can be expressed in terms of operations on mem-
ory addresses. However, type-safe languages such as Java do not allow
manipulation of arbitrary memory locations. Adding support for atomic
instructions to Java is an interesting but important challenge.
In this paper we consider several ways to support atomic instructions
in Java. Each technique has advantages and disadvantages. We propose
idiom recognition as the technique we feel has the best combination of
expressiveness and simplicity. We describe techniques for recognizing in-
stances of atomic operation idioms in the compiler of a Java Virtual
Machine, and converting such instances into code utilizing atomic ma-
chine instructions. In addition, we describe a runtime technique which
ensures that the semantics of multithreaded Java[11] are preserved when
atomic instructions and blocking synchronization are used in the same
program. Finally, we present benchmark results showing that for concur-
rent queues, a wait-free algorithm implemented using atomic compare-
and-swap instructions yields better scalability on a large multiprocessor
than a queue implemented with lock-based synchronization.

1 Introduction
Wait-free data structures and algorithms have been an active area of research
in recent years[3, 6, 12, 18], and have spawned a variety of applications[4, 8, 9].
They have the desirable property that when multiple threads access a wait-
free data structure, stalled threads cannot prevent other threads from making
progress. This avoids a variety of problems encountered with lock-based (block-
ing) synchronization, such as priority inversion and formation of convoys. Wait-
free synchronization is especially useful when a data structure must be accessed
in a context from which blocking is impossible or undesirable.
Wait-free data structures generally work by making small changes atom-
ically, such that before and after the atomic operation the data structure is
in a consistent state. These atomic operations are usually implemented using
Page 2
atomic machine instructions, in which one or more memory locations are ac-
cessed and updated atomically. For example, many architectures support an
atomic compare-and-swap, or ‘CAS’, instruction. CAS atomically compares the
contents of a single memory location against an input value, and if they are
equal stores a second value in the memory location.
Atomic memory operations are easy to express in languages such as C and
C++, in which it is possible to determine the address of any variable, array
element, or field. (It is necessary to resort to assembly language to implement
these operations, but most compilers support some form of inline assembly.)
However, type-safe languages such as Java do not permit manipulation of ar-
bitrary memory locations, nor do they permit the direct execution of arbitrary
machine instructions. The problem of how to express atomic memory operations
in such languages in a way that respects the language’s safety guarantees is thus
more challenging.
In this paper we consider several techniques for supporting atomic memory
operations in the Java programming language[7]. We propose idiom recognition
as a lightweight technique for expressing atomic instructions in a way that is
simple to implement and is fully compatible with the semantics of the language.
While much of our discussion is specific to Java, the same basic techniques
could also be used in other type-safe virtual machines, such as Microsoft’s CLR
(Common Language Runtime).
The structure of the paper is as follows. In Sect. 2, we give a general overview
of atomic instructions and how they are used in wait-free algorithms. In Sect. 3,
we describe several techniques for supporting atomic instructions in the Java
virtual machine. In Sect. 4 we discuss several questions of strategy that arise in
supporting atomic instructions. In Sect. 5, we describe an algorithm for recogniz-
ing instances of idioms which can be translated into atomic machine instructions
within the Java virtual machine. In Sect. 6 we show performance results that
show that support for atomic instructions allow the implementation of a con-
current queue that is more scalable than a comparable queue implemented with
blocking synchronization. In Sect. 7 we describe related work. Finally, in Sect. 8
we summarize our findings and describe possibilities for future work.
2 Atomic Instructions
Atomic machine instructions atomically access and modify one or more memory
locations. They have two primary advantages over using a lock to guarantee
atomicity:
1. an atomic instruction is generally faster than equivalent lock-based code,
and
2. atomic instructions execute in a finite amount of time, whereas acquiring a
lock may block the executing thread for an unbounded period of time
For these reasons, atomic instructions are valuable in situations where a small
update is made to a shared data structure, especially when the use of a lock
Page 3
might significantly reduce potential concurrency. Table 1 lists examples of atomic
instructions and common commercial architectures in which they are supported
in hardware