Skip to content

Commit 8cc06db

Browse files
committed
CAtomicReference#get_and_set compares read value on update.
1 parent d6a3fa1 commit 8cc06db

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

ext/concurrent/atomic_reference.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,13 @@ VALUE ir_set(VALUE self, VALUE new_value) {
5050
}
5151

5252
VALUE ir_get_and_set(VALUE self, VALUE new_value) {
53-
VALUE old_value = ir_get(self);
54-
ir_set(self, new_value);
55-
return old_value;
53+
VALUE old_value;
54+
for (;;) {
55+
old_value = ir_get(self);
56+
if (ir_compare_and_set(self, old_value, new_value) == Qtrue) {
57+
return old_value;
58+
}
59+
}
5660
}
5761

5862
VALUE ir_compare_and_set(volatile VALUE self, VALUE expect_value, VALUE new_value) {

0 commit comments

Comments
 (0)