dcas with llvm

Trying to implement a lock free linked list/stack and other lock free structures sometimes requires the use of a "double compare and swap", essentially comparing two pointers with two other pointers and replacing the first with a new pair, only if both match, atomically. Reading the LangRef it's not obvious how to do this, the cmpxchg instruction is what you'd use if you had just a pair of integers. The trick is to use an int sized (2*pointersize) for example int(64) on a 32bits cpu or int(128) on a 64bits cpu and casting the pointer/values to it. For example:

%struct.dcas = type { i8*, i8* }

%struct.dcas void @dcas(%struct.dcas* %ptr, %struct.dcas %_cmp, 
    %struct.dcas %_newval) {
  %cmp = alloca %struct.dcas
  %newval = alloca %struct.dcas
  store %struct.dcas %_cmp, %cmp
  store %struct.dcas %_newval, %newval

  %res = alloca i64
  %ptri64 = bitcast %struct.test* %ptr to i64*

  %ptrcmpi64 = bitcast %struct.dcas* %cmp to i64*
  %cmpi64 = load i64* %ptrcmpi64

  %ptrnewvali64 = bitcast %struct.dcas* %newval to i64*
  %newvali64 = load i64* %ptrnewvali64

  %resi64 = cmpxchg i64* %ptri64, i64 %cmpi64, i64 %newvali64 seq_cst

  store i64 %resi64, i64* %res
  %resdcasptr = bitcast i64* %res to %struct.dcas*
  %resdcas = load %struct.dcas* %resdcasptr

  ret %resdcas

Replace i64 with i128 on 64bits platforms. Tested on x86 (creates cmpxchg8b) and x86_64 (cmpxchg16b), seems to work for arm too but I'm not entirely familiar with arm asm yet to say 100%.

Windows RT Limitations

While working on Oxygene august and the august release I came across some real oddities in the WinRT runtime. Microsoft dropped like 50% of the apis and must have been working on the basis of "what's the bare minimum". There's no TypedReference (which is used for classic with support). There's no way to determine if a type is an Enum or not. Now I get the idea of dropping non-generic list types and other things that are not needed anymore, but the way reflection was stripped down makes it a lot less powerful.

Finding a good board for media player

My latest project is finding a good base board for a media player. I my requirements are:

  • Has to run Linux
  • Hardware accelerated video in XBMC
  • 500gb or above harddisk
  • < 200 euro

My options seem to be limited, the < 200 euro price tag discards anything Intel, that leaves the ARM boards like Cubieboard, Raspberry PI, BeagleBone Black and Via APC.

Beaglebone Black

The BBB lacks a good graphics processor so isn't an option.


The via apc seem to lack support or a user community completely, they dump a model on the market and then let people hang, doesn't seem like a good idea.

Raspberry PI

The RPI could work, it would need a powered hub and usb harddisk. The powered hub would have to be able to feed both the RPI and the hd at the same time (I want 1 power cord).


The CubieBoard has everything I need, except apparently drivers for the hw acceleration, it has a sata connector (with power) and twice the cpu & memory the rpi has. A 2amp usb power adapter could make it do everything I need except for a cover.

Since this project isn't "due" for December (present for a family member) I can wait a while to see how the cubie ends up, else go for the rpi (or who knows there might be a new model of any of these by then)

New blog

I thought it was time again to put something up here. It's about my 10th attempt at a maintained website, so I'll have to see where this one leads me.