2010-03-19

A Lock Free Concurrent Circular Buffer

The high level classes in java.util.concurrent.atomic makes it quite easy to create data structures that are thread safe and low on contention.  This is a lock free concurrent circular buffer that I once found the need for:

import java.util.List;
import java.util.Vector;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReferenceArray;

/**
 * A lock-free thread safe circular fixed length buffer.
 * 
 * Uses an AtomicInteger as index counter and an AtomicReferenceArray
 * to hold the references to the values.
 * 
 * When the buffer is full, the oldest item is overwritten.
 *
 */
public class CircularBuffer<T> {
    
    private final AtomicInteger index = new AtomicInteger(-1);
    private final AtomicReferenceArray<T> buffer;
    private final int size;

    public CircularBuffer(int size) {
        this.size = size;
        buffer = new AtomicReferenceArray<T>(this.size);
    }
    
    public void add(T item) {
        if (index.compareAndSet(size-1, 0)) {
            buffer.set(0, item);
        } else {
            buffer.set(index.incrementAndGet(), item);
        }
    }

    /**
     * Get contents of buffer, as a list.
     * 
     * Note that the list always has the size of the buffer,
     * no matter how many elements there actually are.
     * 
     * The ordering is also unpredictable.
     * 
     * @return contents
     */
    public List<T> getAll() {
        List<T> list = new Vector<T>(size);
        for (int i = 0; i < size; i++) 
            list.add(buffer.get(i));
        return list;
    }
   
    public T get(int i) {
        return buffer.get(i);
    }
}

2010-03-15

Eleven Reasons Your Home Server Should Be a Netbook

1. It's Cheap

It doesn't have to cost more than $250.

2. It Has a Solid-State Disk Drive

Hard Drives are mechanical devices that come in two forms: the ones that have failed, and those that haven't failed yet. Many netbooks come with solid state drives that potentially last as long as the rest of the computer electronics. If it doesn't come with an SSD originally one can be added later on, often in the form of a Compact Flash adapter.

The newly released Intel X25-V is an SSD especially targeted for netbooks, and sells for around $120.

3. It Has Built In UPS and Power Management

Since it's a netbook, it obviously has a battery. This saves you the need for a UPS system to keep the system running in case of power failure.

4. It Has a Built In Console

Servers are typically "headless", i.e. used without a monitor and keyboard. These can however come in handy for installation or troubleshooting. Having a server with a built-in console can be very useful and saves the cost of expensive remote Keyboard, Video, Mouse (KVM) solutions.

5. It Has Built In Thermal Management

Because of their small and compact form factor, portable computers come with built in thermal management features that often are more elaborate than a typical server. They are also built to be used in a "normal" living room environment, and not a server room with ample cooling.

6. It Has Backup Connectivity

Many netbooks come with a bundled mobile internet device in addition to wireless and wired ethernet. This provides a useful backup connectivity solution in case the primary connection fails or is congested. Combined with the built in battery this will keep the server running and accessible even if the building has lost all electricity, a feature that would be very costly for a full fledged server.

7. It's Small And Quiet

Servers are not build to be quiet. A 1U rack-mounted server typically has around ten 1-inch fans, and boy are they noisy! They typically will be kept running at full speed or a minimum speed that is still very noisy. Noise level ratings around 50-60 dB are common, or as noisy as a TV set or an air conditioning unit. Laptops are much quieter.

8. You Don't Need More Performance (At Home)

A home server is typically either a simple web server or a server to make files available. It thus does not need to be very powerful. The $250 price point above gives you a 1 GHz Atom processor.

9. You Don't Need More Space

It's for sharing documents or services that depend on being at your home. With a 16 GB or 32 GB SSD you easily outperform all but the most expensive hosting or sharing sites. Add in an SD card reader and you can easily expand this storage space at almost no cost.

10. Spare Parts Are Cheap(er) And Easier To Obtain

Server-grade replacement parts are typically pricey and not on stock in your everyday computer store. Laptop spares on the other hand are stocked everywhere. For most popular models replacement batteries are manufactured by other vendors long after the original battery has gone out of distribution. I just bought one for my nine-year-old model.

11. It Doesn't Have to Be New

You can get good deals for used laptops on eBay. My home server was actually purchased there for $120 in 2006, then five years old. It's still running fine.

2010-03-13

C++0x Might Be C++11, Maybe

Trip Report: March 2010 ISO C++ Standards Meeting « Sutter’s Mill
The biggest news is that this afternoon we voted in the final remaining feature changes to C++0x, and to much applause then unanimously approved the text for international ballot as a Final Committee Draft (FCD). FCD means that, assuming no surprises, we intend to do only bug fixes and editorial corrections for the next year or so, and then ballot a final standard. If we can do that, assuming all goes well, C++0x could officially be published as soon as next year as ISO C++ 2011, and we can stop with the “x-is-hex” jokes and just start calling it C++11.

Best wishes to the ISO Technical Committee for pulling this one off – now let’s hope the wheels of the ISO organization are able to grind it through on schedule..

2010-03-10

C++0x: A const * const Odyssey

This is a presentation I and my colleague Anders Schau Knatten held at an internal software seminar this February.

It was meant to cover both some introductory material, best practices we have acquired, new features to look forward to and things that might (or should) scare you.

However not one member of our audience had less experience with C++ than Anders and myself have together, so we quickly jumped down into details.

You should probably view the presentation full screen and open the speaker notes to get the most out of it.

You might find this additional note on slide 43 interesting:  A non-virtual destructor once made it to the board of directors..

2010-03-08

Why C++ is Male

Through recent frustrations, and needless to say, core dumps, I have realized that C++ as a language have some distinct features often associated with men: It can only do one thing at a time, and it cannot communicate. In this post I'll elaborate on these.

It Can Only Do One Thing at a Time

In C++ threads or the notion of multiprocessing simply does not exist. The language has neither a thread concept nor any thread synchronization primitives like mutexes, semaphores and locks.

Even relatively simple constructs like static members simply do not work in a multi-threaded context. As described by Scott Meyers and Andrei Alexandrescu in C++ and the Perils of Double-Checked Locking, no matter how many volatile keywords or extra locking you add in the mix, it still won't work consistently. Some compilers try to help you by fixing this for you, but since that is compiler implementation dependent it does more harm than help.

C++ has no thread-safe memory model, and does not provide any means for a memory barrier. Which means that any "clever" compiler or instruction dispatcher is free to reorder your statements in whichever way provides the same result in a single thread of execution.

C++ Cannot Communicate

What is the de facto standard for communicating with the outside world, and has been for the last two decades? The Internet Protocol, or more specifically, the TCP and UDP protocols on top of it. But the C++ language does not support this protocol. The only means of communication provided by the language are streams for input and output, and they can only be used for talking to files or to the console.

You Can Teach C++ New Tricks

In the recent years I've learned the art of walking and chewing gum at the same time. I can even pay attention to a conversation while doing other stuff, or at least I think I can.

Being a married man I have also learned to cope with my lack of communication skills, to some extent. Neither of these feats came easy. They were achieved after failing a lot and suffering many crashes.

I have never tried teaching a man to do these things, but having been taught these capabilities has provided some insight into the requisite effort and amount of frustration involved.

Trying to teach C++ to do several things at the same time while communicating with others can also be accomplished. It is however probably equally frustrating as teaching your significant other something he obviously wasn't designed to do. Both capabilities require that you deal with the more primitive foundation the individual has evolved from (Neanderthals and C), and relying on cultural constructs (operating system interfaces) that have been added on top of evolution and behave differently from individual to individual.

In many circumstances the resulting behavior is undefined, and will certainly cause some surprises. You also have to meticulously and in great detail instruct your friend on every step required to perform the task, in a language he understands.

The Modern Man: C++0x

Men have evolved, and so has C++ as well. In C++0x the language has grown the capability of doing several things at once, but it still prefers to do so alone in a dark corner.