Skip to main content

Posts

Showing posts from May, 2007

Double-Checked Locking and the Problem with Wikipedia

I love Wikipedia, I really do. I use it for everything. Well, not everything. For example, if I want a really good piece of chocolate cake, I tend to use flour, sugar, butter, eggs, chocolate, and not Wikipedia at all . For many things, however, Wikipedia cannot be surpassed. Imagine, then, my surprise when I found out that their page on Double-Checked Locking gave an example of a version of double-checked locking that does not work . // Broken -- Do Not Use! class Foo {   private Helper helper = null;   private boolean initialized = false;   public Helper getHelper() {     if (!initialized) {       synchronized(this) {         if (!initialized) {           helper = new Helper();           initialized = true;         }       }     }   return helper; } (For background on Double-Checked Locking, see Bill Pugh's "Double-Checked Locking is Broken" Declaration .) The problem with this code is exactly the same as the problem with all of the other varia

Profiling with JVMTI/JVMPI, SIGPROF and AsyncGetCallTrace

This post is not about the memory model, but, instead, is about being able to get, asynchronously, the stack trace of a currently executing thread out of Sun's Java VM. Getting one synchronously is easy, of course. I was trying to profile some Java applications under Linux, and I wanted to use SIGPROF and JVMTI to find out what code was running at any given second. This is just a posting to document how I did it, as I ended up using some undocumented JVM features, and I can't find a proper reference for them anywhere else. More information as to why you might want to do this is here . JVMTI is the Java Virtual Machine Toolkit Interface. It's a C/C++ interface to the virtual machine that allows you to hook in debuggers and profilers. More information can be found here . SIGPROF is a POSIX signal. The way it is set up under Linux is that you can set a timer that goes off at intervals, and, whenever the timer expires, the SIGPROF signal is sent to the application. The

Google Talks

By the way, gentle reader, in case you haven't noticed, I am running a series of talks at Google. All of them can be found here . Updated July 2015 : They seem to be available on YouTube, now that Google Video is a long forgotten service: They can now be found here .

Roach Motels and The Java Memory Model

I've been asked by a couple of people lately about what a compiler can do with code motion around synchronized blocks. The first person asked me about the following example, so let's run with it: x = 1; synchronized(o) { z = z + 1; } y = 1; I may have answered this already (at some point), but I feel free to repeat myself. The JMM has what we call "roach motel" ordering. "Roach Motel" is Black Flag's name for their cockroach trap; in grad school, my apartment had lots and lots and lots of cockroaches, so I find it particularly salient (although I think it was probably Bill Pugh who came up with this association). In the 1980s, the slogan for Black Flag Roach Motels was "Roaches check in, but they don't check out". The memory model is the same: accesses can be moved into synchronized blocks, but they can't be moved out. So, in this case, you can get: x = 1; synchronized(o) { z = z + 1; } y = 1; if we want to move the write to y ea

Software Transactional Memory Talk at Google

Adam Welc, from Intel's Programming Systems Lab, came to Google to give a talk about what they are doing in the realm of Software Transactional Memory. He also talks briefly, at the end, about Intel's hardware plans. For those who are interested in such things, this is a talk worth seeing. Click Here.