Skip to content

Commit e196870

Browse files
authored
CPP-988 Fix race condition in monotonic_timestamp() (#558)
1 parent f15ee23 commit e196870

File tree

1 file changed

+3
-14
lines changed

1 file changed

+3
-14
lines changed

src/uuids.cpp

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -262,20 +262,9 @@ uint64_t UuidGen::monotonic_timestamp() {
262262
while (true) {
263263
uint64_t now = from_unix_timestamp(get_time_since_epoch_ms());
264264
uint64_t last = last_timestamp_.load();
265-
if (now > last) {
266-
if (last_timestamp_.compare_exchange_strong(last, now)) {
267-
return now;
268-
}
269-
} else {
270-
uint64_t last_ms = to_milliseconds(last);
271-
if (to_milliseconds(now) < last_ms) {
272-
return last_timestamp_.fetch_add(1);
273-
}
274-
uint64_t candidate = last + 1;
275-
if (to_milliseconds(candidate) == last_ms &&
276-
last_timestamp_.compare_exchange_strong(last, candidate)) {
277-
return candidate;
278-
}
265+
uint64_t candidate = (now > last) ? now : last + 1;
266+
if (last_timestamp_.compare_exchange_strong(last, candidate)) {
267+
return candidate;
279268
}
280269
}
281270
}

0 commit comments

Comments
 (0)