diff options
author | Timo Teras <timo.teras@iki.fi> | 2009-11-25 16:53:02 +0200 |
---|---|---|
committer | Timo Teras <timo.teras@iki.fi> | 2009-11-25 16:53:02 +0200 |
commit | 4db830052d941d9c6de281bc9a2f6ac212c59ad8 (patch) | |
tree | 3a9b5e7c78a812fc582f8844703069e5692a0c94 /src | |
parent | 2b19cc385163a43b1d559074a795a8aaab751185 (diff) | |
download | libtf-4db830052d941d9c6de281bc9a2f6ac212c59ad8.tar.bz2 libtf-4db830052d941d9c6de281bc9a2f6ac212c59ad8.tar.xz |
libtf: minor changes and new test case for network i/o
fixup the internals a bit.
Diffstat (limited to 'src')
-rw-r--r-- | src/fiber.c | 11 | ||||
-rw-r--r-- | src/io-unix.c | 54 |
2 files changed, 60 insertions, 5 deletions
diff --git a/src/fiber.c b/src/fiber.c index aa83fa8..b678842 100644 --- a/src/fiber.c +++ b/src/fiber.c @@ -123,20 +123,27 @@ static void process_runq(struct tf_scheduler *sched) } } -int tf_main(tf_fiber_proc main_fiber) +int tf_main_args(tf_fiber_proc main_fiber, int argc, char **argv) { struct tf_uctx *ctx = alloca(sizeof(struct tf_uctx) + sizeof(struct tf_scheduler)); struct tf_scheduler *sched = (struct tf_scheduler*) ctx->fiber.data; + struct tf_main_ctx *mainctx; int stack_guard = STACK_GUARD; ctx->stack_guard = &stack_guard; *sched = (struct tf_scheduler){ .run_q = TF_LIST_HEAD_INITIALIZER(sched->run_q), }; + __tf_scheduler = sched; tf_poll_init(); update_time(sched); - tf_fiber_put(tf_fiber_create(main_fiber, 0)); + + mainctx = tf_fiber_create(main_fiber, sizeof(struct tf_main_ctx)); + mainctx->argc = argc; + mainctx->argv = argv; + tf_fiber_put(mainctx); + do { tf_mtime_diff_t timeout; diff --git a/src/io-unix.c b/src/io-unix.c index cc1bd1b..8ed8e42 100644 --- a/src/io-unix.c +++ b/src/io-unix.c @@ -90,7 +90,8 @@ int tf_close(struct tf_fd *fd) return 0; } -int tf_read(struct tf_fd *fd, void *buf, size_t count, tf_mtime_diff_t timeout) +int tf_read_fully(struct tf_fd *fd, void *buf, size_t count, + tf_mtime_diff_t timeout) { ssize_t n; int r; @@ -127,8 +128,8 @@ int tf_read(struct tf_fd *fd, void *buf, size_t count, tf_mtime_diff_t timeout) return -r; } -int tf_write(struct tf_fd *fd, const void *buf, size_t count, - tf_mtime_diff_t timeout) +int tf_write_fully(struct tf_fd *fd, const void *buf, size_t count, + tf_mtime_diff_t timeout) { ssize_t n; int r; @@ -162,6 +163,53 @@ int tf_write(struct tf_fd *fd, const void *buf, size_t count, return -r; } +ssize_t tf_read(struct tf_fd *fd, void *buf, size_t count, tf_mtime_diff_t timeout) +{ + ssize_t n; + + tf_fd_monitor(fd, EPOLLIN); + do { + n = read(fd->fd, buf, count); + if (n >= 0) + break; + if (errno == EINTR) + continue; + if (errno != EAGAIN) { + n = -errno; + break; + } + n = tf_schedule(timeout); + timeout = TF_NO_TIMEOUT_CHANGE; + } while (n == TF_WAKEUP_FD); + tf_fd_unmonitor(fd); + + return n; +} + +ssize_t tf_write(struct tf_fd *fd, const void *buf, size_t count, + tf_mtime_diff_t timeout) +{ + ssize_t n; + + tf_fd_monitor(fd, EPOLLOUT); + do { + n = write(fd->fd, buf, count); + if (n >= 0) + break; + if (errno == EINTR) + continue; + if (errno != EAGAIN) { + n = -errno; + break; + } + n = tf_schedule(timeout); + timeout = TF_NO_TIMEOUT_CHANGE; + } while (n == TF_WAKEUP_FD); + tf_fd_unmonitor(fd); + + return n; +} + int tf_socket(struct tf_fd *fd, int domain, int type, int protocol) { int kfd, tfdf, on = 1; |