diff options
| author | Laurent Bercot <ska-skaware@skarnet.org> | 2025-03-22 01:01:26 +0000 |
|---|---|---|
| committer | Laurent Bercot <ska@appnovation.com> | 2025-03-22 01:01:26 +0000 |
| commit | 138a22f24ac6ddf21c2420e9ec39dc611372b52c (patch) | |
| tree | 1a7294df51c031a64f99a5d4af7ee955fd8f205f /src | |
| parent | 715b0466cda481f240bafdad7793d59eaaea26b1 (diff) | |
| download | skalibs-138a22f24ac6ddf21c2420e9ec39dc611372b52c.tar.gz | |
Add timed_write and timed_writev
Signed-off-by: Laurent Bercot <ska@appnovation.com>
Diffstat (limited to 'src')
| -rw-r--r-- | src/include/skalibs/unix-timed.h | 5 | ||||
| -rw-r--r-- | src/libunixonacid/timed_write.c | 39 | ||||
| -rw-r--r-- | src/libunixonacid/timed_writev.c | 43 |
3 files changed, 87 insertions, 0 deletions
diff --git a/src/include/skalibs/unix-timed.h b/src/include/skalibs/unix-timed.h index 7243370..1ddd206 100644 --- a/src/include/skalibs/unix-timed.h +++ b/src/include/skalibs/unix-timed.h @@ -25,6 +25,11 @@ extern size_t timed_read (int, char *, size_t, tain const *, tain *) ; extern size_t timed_readv (int, struct iovec *, unsigned int, tain const *, tain *) ; #define timed_readv_g(fd, v, vlen, deadline) timed_readv(fd, v, vlen, (deadline), &STAMP) +extern size_t timed_write (int, char const *, size_t, tain const *, tain *) ; +#define timed_write_g(fd, s, len, deadline) timed_write(fd, s, len, (deadline), &STAMP) +extern size_t timed_writev (int, struct iovec const *, unsigned int, tain const *, tain *) ; +#define timed_writev_g(fd, v, vlen, deadline) timed_writev(fd, v, vlen, (deadline), &STAMP) + extern ssize_t buffer_timed_fill (buffer *, tain const *, tain *) ; #define buffer_timed_fill_g(b, deadline) buffer_timed_fill(b, (deadline), &STAMP) extern int bufalloc_timed_flush (bufalloc *, tain const *, tain *) ; diff --git a/src/libunixonacid/timed_write.c b/src/libunixonacid/timed_write.c new file mode 100644 index 0000000..dc86d8f --- /dev/null +++ b/src/libunixonacid/timed_write.c @@ -0,0 +1,39 @@ +/* ISC license. */ + +#include <unistd.h> + +#include <skalibs/functypes.h> +#include <skalibs/allreadwrite.h> +#include <skalibs/unix-timed.h> + +struct blah_s +{ + int fd ; + char const *s ; + size_t len ; + size_t w ; +} ; + +static int getfd (struct blah_s *blah) +{ + return blah->fd ; +} + +static int isnonempty (struct blah_s *blah) +{ + return blah->w < blah->len ; +} + +static int flush (struct blah_s *blah) +{ + ssize_t r = fd_write(blah->fd, blah->s + blah->w, blah->len - blah->w) ; + if (r > 0) blah->w += r ; + return r > 0 ; +} + +size_t timed_write (int fd, char const *s, size_t len, tain const *deadline, tain *stamp) +{ + struct blah_s blah = { .fd = fd, .s = s, .len = len, .w = 0 } ; + timed_flush(&blah, (init_func_ref)&getfd, (init_func_ref)&isnonempty, (init_func_ref)&flush, deadline, stamp) ; + return blah.w ; +} diff --git a/src/libunixonacid/timed_writev.c b/src/libunixonacid/timed_writev.c new file mode 100644 index 0000000..7542d27 --- /dev/null +++ b/src/libunixonacid/timed_writev.c @@ -0,0 +1,43 @@ +/* ISC license. */ + +#include <sys/uio.h> + +#include <skalibs/functypes.h> +#include <skalibs/allreadwrite.h> +#include <skalibs/siovec.h> +#include <skalibs/unix-timed.h> + +struct blah_s +{ + int fd ; + struct iovec *v ; + unsigned int vlen ; +} ; + +static int getfd (struct blah_s *blah) +{ + return blah->fd ; +} + +static int isnonempty (struct blah_s *blah) +{ + return !!siovec_len(blah->v, blah->vlen) ; +} + +static int flush (struct blah_s *blah) +{ + ssize_t r = fd_writev(blah->fd, blah->v, blah->vlen) ; + if (r > 0) siovec_seek(blah->v, blah->vlen, r) ; + return r > 0 ; +} + +size_t timed_writev (int fd, struct iovec const *srcv, unsigned int vlen, tain const *deadline, tain *stamp) +{ + size_t len = siovec_len(srcv, vlen) ; + if (!len) return 0 ; + struct iovec v[vlen] ; + struct blah_s blah = { .fd = fd, .v = v, .vlen = vlen } ; + for (unsigned int i = 0 ; i < vlen ; i++) v[i] = srcv[i] ; + timed_flush(&blah, (init_func_ref)&getfd, (init_func_ref)&isnonempty, (init_func_ref)&flush, deadline, stamp) ; + return len - siovec_len(v, vlen) ; +} |
