Atomic Operation Library
Categories:
4 minute read
Memory Model
Like the standard library, XWOS atomic operations are divided into 6 memory ordering models:
xwaop_mo_relaxed
Relaxed ordering, no memory barrier operations.
xwaop_mo_consume
Consume ordering, adds a Data dependency barrier between read operations. This barrier is uncommon; for common x86/ARM architectures, Data dependency barrier is handled automatically. To the author’s knowledge, currently only Alpha architecture CPUs require programmer attention to Data dependency barrier. The Rust language does not even provide this memory ordering.
xwaop_mo_acquire
Acquire ordering, often used in conjunction with reads to form load-acquire.
xwaop_mo_release
Release ordering, often used in conjunction with writes to form store-release.
xwaop_mo_acq_rel
Acquire-release ordering: acquire on load and release on store. Typically used for read-modify-write atomic operations.
xwaop_mo_seq_cst
Sequentially consistent ordering, indicating that the CPU’s access order to memory strictly follows the order of the program code (after compilation). This is referred to in journal literature as Sequential Consistency.
Atomic Operations
XWOS provides an atomic operation template library. Various basic types defined in Basic Types can use atomic operations.
XWOS’s atomic operation library can be mixed with atomic operations from the standard library <stdatomic.h>.
Atomic operation function templates include:
- Read
xwaop_load(): Load- Can specify one of 6 memory orderings
xwaop_read(): Read- Memory order:
xwaop_mo_acquire
- Memory order:
- Write
xwaop_store(): Store- Can specify one of 6 memory orderings
xwaop_write(): Write- Memory order:
xwaop_mo_release
- Memory order:
- Read-Modify-Write
- Basic arithmetic
xwaop_add(): Addxwaop_sub(): Subtractxwaop_rsb(): Reverse subtract- Memory order:
xwaop_mo_acq_rel
- Bitwise operations
xwaop_and(): ANDxwaop_or(): ORxwaop_xor(): XOR- Memory order:
xwaop_mo_acq_rel
- Bit operations
xwaop_s1m(): Set all bits in the data mask portion to 1xwaop_c0m(): Clear all bits in the data mask portion to 0xwaop_x1m(): Toggle all bits in the data mask portion- Memory order:
xwaop_mo_acq_rel
- Basic arithmetic
- Read-Test-Modify-Write
- All functions whose names match the regular expression
xwaop_t.+_then_.+- Divided into two cases
- Test succeeds, continue with “modify-write” operation, memory order is
xwaop_mo_acq_relat this point - Test fails, equivalent to a read operation, memory order is
xwaop_mo_consumeat this point
- Test succeeds, continue with “modify-write” operation, memory order is
- Divided into two cases
- All functions whose names match the regular expression
The first parameter of all atomic operation functions is the Basic Type, and the second parameter is the pointer to the atomic data, for example:
xwer_t rc;
xwsq_a refcnt = v;
xwsq_t nv, ov;
/*
* Test whether refcnt is 0:
* + true: refcnt increases by 1, nv returns the new value after increment, ov returns the old value before increment,
* memory order: acq_rel, rc is the return value XWOK;
* + false: both nv and ov return the value of refcnt, memory order: consume, rc is the return value -EACCES;
*/
rc = xwaop_teq_then_add(xwsq_t, &refcnt, 0, 1, &nv, &ov);
Bitmap Atomic Operations
Bitmaps (xwbmp_t arrays) can undergo atomic operations. XWOS provides a basic function library:
- Declaration
xwbmpaop_define(): Declare an atomic bitmap
- Read
xwbmpaop_t1i(): Test whether a certain bit in the bitmap is set to 1- Memory order:
consume
- Write
xwbmpaop_s1all(): Set all bits in the bitmap to 1xwbmpaop_c0all(): Clear all bits in the bitmap to 0- Memory order:
acq_rel
- Read-Modify-Write
xwbmpaop_s1i(): Set a certain bit in the bitmap to 1xwbmpaop_c1i(): Clear a certain bit in the bitmap to 0xwbmpaop_x1i(): Toggle a certain bit in the bitmap- Memory order:
acq_rel
- Read-Test-Modify-Write
- All functions whose names match the regular expression
xwbmpaop_.+_then_.+xwbmpaop_t0i_then_s1i(): Test whether a certain bit in the bitmap is 0, and if so, set it to 1xwbmpaop_t1i_then_c0i(): Test whether a certain bit in the bitmap is 1, and if so, clear it to 0xwbmpaop_fls_then_c0i(): Find the first bit set to 1 from the most significant bit and clear it to 0xwbmpaop_flz_then_s1i(): Find the first bit set to 0 from the most significant bit and set it to 1xwbmpaop_ffs_then_c0i(): Find the first bit set to 1 from the least significant bit and clear it to 0xwbmpaop_ffz_then_s1i(): Find the first bit set to 0 from the least significant bit and set it to 1- Divided into two cases
- Test succeeds, continue with “modify-write” operation, memory order is
xwaop_mo_acq_relat this point - Test fails, equivalent to a read operation, memory order is
xwaop_mo_consumeat this point
- Test succeeds, continue with “modify-write” operation, memory order is
- All functions whose names match the regular expression
API Reference
-
Atomic Operations
-
Bitmap Atomic Operations