Save a redundant load in zipos read/seek (#1037)

When h->pos has changed to something other than SIZE_MAX, we don't need
the extra atomic load.
This commit is contained in:
Jōshin 2023-12-24 19:40:18 -05:00 committed by GitHub
parent bb2602a524
commit 25266b037b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 18 deletions

View file

@ -38,17 +38,15 @@ static ssize_t __zipos_read_impl(struct ZiposHandle *h, const struct iovec *iov,
return eisdir();
}
if (opt_offset == -1) {
while (true) {
Restart:
start_pos = atomic_load_explicit(&h->pos, memory_order_relaxed);
do {
if (UNLIKELY(start_pos == SIZE_MAX)) {
continue;
goto Restart;
}
if (LIKELY(atomic_compare_exchange_weak_explicit(
} while (!LIKELY(atomic_compare_exchange_weak_explicit(
&h->pos, &start_pos, SIZE_MAX, memory_order_acquire,
memory_order_relaxed))) {
break;
}
}
memory_order_relaxed)));
x = y = start_pos;
} else {
x = y = opt_offset;

View file

@ -47,10 +47,11 @@ static int64_t Seek(int64_t pos, int64_t offset) {
*/
int64_t __zipos_seek(struct ZiposHandle *h, int64_t offset, unsigned whence) {
int64_t pos, new_pos;
while (true) {
Restart:
pos = atomic_load_explicit(&h->pos, memory_order_relaxed);
do {
if (UNLIKELY(pos == SIZE_MAX)) {
continue;
goto Restart;
}
switch (whence) {
case SEEK_SET:
@ -65,11 +66,8 @@ int64_t __zipos_seek(struct ZiposHandle *h, int64_t offset, unsigned whence) {
default:
new_pos = einval();
}
if (LIKELY(atomic_compare_exchange_weak_explicit(
} while (!LIKELY(atomic_compare_exchange_weak_explicit(
&h->pos, &pos, new_pos < 0 ? pos : new_pos, memory_order_release,
memory_order_relaxed))) {
break;
}
}
memory_order_relaxed)));
return new_pos;
}