The Usages of Intel RTM

The emerging HTM (Hardware Transactional Memory) technology can reduce the extra overhead (i.e., Lock) during doing transactions. Recently years, Intel make its hardware solutions, RTM (Restricted Transactional Memory), is available as a mature commercial products. There are three main instructions: XBEGIN, XEND, and XABORT for supporting HTM. Intel Intrinsics library natively support such three software interfaces in C/C++.

To start/end the transactional code region, programmer should use XBEGIN/XEND. The XABORT instruction allows programmers to abort the execution of an RTM region explicitly. The XABORT instruction takes an 8-bit immediate argument that is loaded into the EAX register becoming available to software following an RTM abort.

A simple example is just like following codes.

1
2
3
4
5
6
7
8
9
10
while (1) { // keep trying
int status = _xbegin(); // set status = -1 and start transaction
if (status == _XBEGIN_STARTED) { // status == XBEGIN_STARTED == -1
(*g) ++; // non atomic increment of shared global variable
_xend(); // end transaction
break; // break on success
} else { //
x_abort(0xff);
} //
}

Firstly, you should check whether your CPU platform is support RTM or not. Just run cat /proc/cpuinfo, the answer is yes if rtm is shown in the flags item. To compile such RTM codes, you should include the C header named immintrin.h add an extra compiler flags -mrtm

A full descriptions of RTM’s APIs is available here