JCTools
JCTools copied to clipboard
[Channels] NOTE: Channels unaligned memory access considerations
The SPSC fixed sized ring buffer is using an int marker for publication:
private boolean isMessageReady(long offset) {
return UNSAFE.getIntVolatile(null, offset) == READY_MESSAGE_INDICATOR;
}
private void busyIndicator(long offset) {
UNSAFE.putOrderedInt(null, offset, BUSY_MESSAGE_INDICATOR);
}
This is potentially an issue if offset is not integer aligned. We use int because there's no putOrderedByte. The int access may not be atomic, but this is not really an issue because we only set the value to 0 or 1 so there's only one actual byte in use. I am aware that for JDK9 Unsafe gains new methods for unaligned access which we may need to use here and elsewhere in the channel code. I am also aware that current packing/layout of data in each message is NOT aligned, which is something we may want to address in future.
Can't you just use a long
indicator and align the int
write into it?
in theory, yes. In practice I think I'll go with rounding the 'frame' size to int.
@nitsanw Both have advantages: having a long indicator and frame size rounded to long will give you frame start aligned to long for free...