void bdrv_write_threshold_check_write(BlockDriverState *bs, int64_t offset,
int64_t bytes)
{
int64_t end = offset + bytes;
- uint64_t wtr = bs->write_threshold_offset;
+ uint64_t wtr;
- if (wtr > 0 && end > wtr) {
- qapi_event_send_block_write_threshold(bs->node_name, end - wtr, wtr);
+retry:
+ wtr = bdrv_write_threshold_get(bs);
+ if (wtr == 0 || wtr >= end) {
+ return;
+ }
- /* autodisable to avoid flooding the monitor */
- bdrv_write_threshold_set(bs, 0);
+ /* autodisable to avoid flooding the monitor */
+ if (qatomic_cmpxchg(&bs->write_threshold_offset, wtr, 0) != wtr) {
+ /* bs->write_threshold_offset changed in parallel */
+ goto retry;
}
+
+ /* We have cleared bs->write_threshold_offset, so let's send event */
+ qapi_event_send_block_write_threshold(bs->node_name, end - wtr, wtr);
}