mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-03-03 15:38:22 +00:00
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:
parent
bb2602a524
commit
25266b037b
2 changed files with 14 additions and 18 deletions
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue