musl/src/stdio/__stdio_read.c
Rich Felker 5816592389 make stdio open, read, and write operations cancellation points
it should be noted that only the actual underlying buffer flush and
fill operations are cancellable, not reads from or writes to the
buffer. this behavior is compatible with POSIX, which makes all
cancellation points in stdio optional, and it achieves the goal of
allowing cancellation of a thread that's "stuck" on IO (due to a
non-responsive socket/pipe peer, slow/stuck hardware, etc.) without
imposing any measurable performance cost.
2012-02-02 00:11:29 -05:00

33 lines
717 B
C

#include "stdio_impl.h"
#include <pthread.h>
static void cleanup(void *p)
{
FILE *f = p;
if (!f->lockcount) __unlockfile(f);
}
size_t __stdio_read(FILE *f, unsigned char *buf, size_t len)
{
struct iovec iov[2] = {
{ .iov_base = buf, .iov_len = len - !!f->buf_size },
{ .iov_base = f->buf, .iov_len = f->buf_size }
};
ssize_t cnt;
pthread_cleanup_push(cleanup, f);
cnt = syscall_cp(SYS_readv, f->fd, iov, 2);
pthread_cleanup_pop(0);
if (cnt <= 0) {
f->flags |= F_EOF ^ ((F_ERR^F_EOF) & cnt);
f->rpos = f->rend = 0;
return cnt;
}
if (cnt <= iov[0].iov_len) return cnt;
cnt -= iov[0].iov_len;
f->rpos = f->buf;
f->rend = f->buf + cnt;
if (f->buf_size) buf[len-1] = *f->rpos++;
return len;
}