Serialize ZipOS handle IO across threads

This commit is contained in:
Justine Tunney 2023-02-23 06:24:49 -08:00
parent 0f1ead8943
commit a808b3e738
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
7 changed files with 18 additions and 6 deletions

View file

@ -17,9 +17,10 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/assert.h"
#include "libc/intrin/safemacros.internal.h"
#include "libc/calls/struct/iovec.h"
#include "libc/intrin/safemacros.internal.h"
#include "libc/str/str.h"
#include "libc/thread/thread.h"
#include "libc/zip.h"
#include "libc/zipos/zipos.internal.h"
@ -39,11 +40,13 @@ static size_t GetIovSize(const struct iovec *iov, size_t iovlen) {
ssize_t __zipos_read(struct ZiposHandle *h, const struct iovec *iov,
size_t iovlen, ssize_t opt_offset) {
size_t i, b, x, y;
pthread_mutex_lock(&h->lock);
x = y = opt_offset != -1 ? opt_offset : h->pos;
for (i = 0; i < iovlen && y < h->size; ++i, y += b) {
b = min(iov[i].iov_len, h->size - y);
if (b) memcpy(iov[i].iov_base, h->mem + y, b);
}
if (opt_offset == -1) h->pos = y;
pthread_mutex_unlock(&h->lock);
return y - x;
}