commit 5816592389 added these optional
cancellation points on the basis that cancellable stdio could be
useful, to unblock threads stuck on stdio operations that will never
complete. however, the only way to ensure that cancellation can
achieve this is to violate the rules for side effects when
cancellation is acted upon, discarding knowledge of any partial data
transfer already completed. our implementation exhibited this behavior
and was thus non-conforming.
in addition to improving correctness, removing these cancellation
points moderately reduces code size, and should significantly improve
performance on i386, where sysenter/syscall instructions can be used
instead of "int $128" for non-cancellable syscalls.
35 lines
824 B
C
35 lines
824 B
C
#include "stdio_impl.h"
|
|
#include <sys/uio.h>
|
|
|
|
size_t __stdio_write(FILE *f, const unsigned char *buf, size_t len)
|
|
{
|
|
struct iovec iovs[2] = {
|
|
{ .iov_base = f->wbase, .iov_len = f->wpos-f->wbase },
|
|
{ .iov_base = (void *)buf, .iov_len = len }
|
|
};
|
|
struct iovec *iov = iovs;
|
|
size_t rem = iov[0].iov_len + iov[1].iov_len;
|
|
int iovcnt = 2;
|
|
ssize_t cnt;
|
|
for (;;) {
|
|
cnt = syscall(SYS_writev, f->fd, iov, iovcnt);
|
|
if (cnt == rem) {
|
|
f->wend = f->buf + f->buf_size;
|
|
f->wpos = f->wbase = f->buf;
|
|
return len;
|
|
}
|
|
if (cnt < 0) {
|
|
f->wpos = f->wbase = f->wend = 0;
|
|
f->flags |= F_ERR;
|
|
return iovcnt == 2 ? 0 : len-iov[0].iov_len;
|
|
}
|
|
rem -= cnt;
|
|
if (cnt > iov[0].iov_len) {
|
|
cnt -= iov[0].iov_len;
|
|
iov++; iovcnt--;
|
|
}
|
|
iov[0].iov_base = (char *)iov[0].iov_base + cnt;
|
|
iov[0].iov_len -= cnt;
|
|
}
|
|
}
|