AtomEclipse |
Semantics | Disclamer | Download & Setup | Lecture Slides | Contact
Teaching shared-memory concurrency to computer science undergrads can be a frustrating task. Nondeterministic thread scheduling is understandably jarring after several courses' worth of sequential programming; the added complications of locks, race conditions, and deadlock make this new concurrent world all the more disorienting. At the University of Washington, we have found it helpful to begin instruction of shared-memory concurrency control with atomic transactions. Atomic transactions allow programmers to execute parts of code as if the program were single-threaded.
Atomic transactions have relatively straightforward semantics compared to locks: code enclosed within an atomic block runs as if no other threads were interleaved with the computation, and other threads behave as if the transactional code happened "all at once." Within the runtime system, other threads may be interleaved in a way such that their memory accesses do not conflict with the current transaction's, and the transaction may abort or roll back if a conflict occurs, but these details are largely invisible to the programmer.
Unfortunately, atomic transactions are a new enough concept in programming languages that implementations (though they certainly exist) have not yet made their way into the popular distributions of mainstream languages like Java, C and C++. Therefore, instructors cannot demonstrate atomic transactions in class or use atomic transactions in homeworks. We have developed an educational tool called AtomEclipse to fill this void.
AtomEclipse is an Eclipse plugin that allows Java threads to run atomic transactions in Eclipse's debug mode. The plugin uses various hooks in the Java Debug Interface (JDI) to catch when threads enter and exit atomic blocks and prevent other threads from running accordingly. The project was developed by Laura Effinger-Dean with support from an Intel grant, and is intended for use in the University of Washington's CSE 303 course. If you would like to use AtomEclipse at your institution, please try it out and let us know how it works for you.
With AtomEclipse, you can mark code points to enter and exit atomic sections by calling AtomicDebug.enterAtomic() and AtomicDebug.exitAtomic(), respectively. When a thread calls AtomicDebug.enterAtomic(), the plugin suspends any other running threads, so the calling thread is the only thread running. Calling AtomicDebug.exitDebug() resumes the suspended threads. For example, to create a thread-safe withdraw() method for a bank account class:
/* Withdraw money from the account. The method returns trueAtomic sections may also be nested. Threads currently in an atomic section maintain a counter; calls to AtomicDebug.enterAtomic() and AtomicDebug.exitAtomic() increment and decrement the counter, respectively. When the counter hits zero, the suspended threads are resumed. So if we wanted to write a transfer() method that called the deposit method, the atomic sections compose:
/* Transfer money from this account to another. The methodIf a thread terminates during an atomic section without issuing the proper calls to AtomicDebug.enterAtomic(), the waiting threads resume automatically. If a thread tries to run another thread during an atomic section via Thread.start(), the new thread is suspended until the atomic section completes.
AtomEclipse is intended for demonstration purposes. It works well for the examples that we have tested, but we do not recommend using it in class without testing your code a few times. The plugin does not work unless you are using Eclipse's debug mode to run your program. The implementation does not provide any fairness guarantees in thread scheduling, so if a thread executing an atomic section goes into an infinite loop, the other threads will remain suspended forever. There is no abort/retry feature: a thread's actions within an atomic section cannot be undone, and once a thread enters an atomic section, it must complete the section before any other threads may run.
Downloads:
AtomEclipseExamples.zip
AtomicDebug.jar
In order to use AtomEclipse, follow these steps:
I used AtomEclipse in a guest lecture for CSE 303, Concepts and Tools for Software Development, in Fall 2007. Here are the slides: full version handouts version LaTeX source
Some of the slides are based on Dan Grossman's lecture from Spring '07. The code examples are included in the examples package linked to above.