Cumulative U256
Concurrent Delta Friendly uint256
U256Cumulative is a concurrent variable that supports concurrent addition and subtraction. It is conceptionally similar to an atomic integer but with one major constraint: the value can be only updated. Attempting to invoke timing-dependent operations like read()
on the variable while being updated by other threads will result in a transaction rollback.
TheU256Cum contract
is designed for handling cumulative operations in full parallel. It has both minimum and maximum bounds and allows concurrent delta changes. Out-of-limit delta changes will fail to ensure that the variable stays within its prescribed bounds.
Note: It is recommended to check the returned value after adding a delta update.
Constructor
constructor(uint256 minv, uint256 maxv)
Constructor to initialize the U256Cumulative variable with specified minimum and maximum bounds.
Parameters:
minv
(type:uint256
): The minimum bound of the cumulative value.maxv
(type:uint256
): The maximum bound of the cumulative value.
Public Functions
peek
Get the latest committed value of the cumulative variable.
Returns:
uint256
: The latest committed value of the cumulative variable.
get
Get the current value of the cumulative variable.
Returns:
uint256
: The current value of the cumulative variable.
add
Add the given value to the cumulative variable.
Parameters:
delta
(type:uint256
): The value to be added to the cumulative variable.
Returns:
bool
: A boolean indicating the success of the operation.
sub
Subtract the given value from the variable.
Parameters:
delta
(type:uint256
): The value to be subtracted from the cumulative variable.
Returns:
bool
: A boolean indicating the success of the operation.
min
Get the minimum bound of the variable.
Returns:
uint256
: The minimum bound of the cumulative variable.
max
Get the maximum bound of the variable.
Returns:
uint256
: The maximum bound of the variable.
Last updated