diff options
author | Timo Teras <timo.teras@iki.fi> | 2009-11-25 10:52:15 +0200 |
---|---|---|
committer | Timo Teras <timo.teras@iki.fi> | 2009-11-25 10:52:15 +0200 |
commit | fc1044daf51f32b9d85f8497e4e0bd5a3c1e7fe9 (patch) | |
tree | 52e11c88f17c47c0d086761e50b266f5c5ccd061 /include | |
parent | cec85dedb7fd66cf2c23cafadd7c53eb7afed78f (diff) | |
download | libtf-fc1044daf51f32b9d85f8497e4e0bd5a3c1e7fe9.tar.bz2 libtf-fc1044daf51f32b9d85f8497e4e0bd5a3c1e7fe9.tar.xz |
libtf: implement basic file i/o with epoll
some scetching of i/o api, and implement basic read and write
functionality. integrate polling to scheduler and an epoll based
polling mechanism.
Diffstat (limited to 'include')
-rw-r--r-- | include/libtf/defines.h | 5 | ||||
-rw-r--r-- | include/libtf/fiber.h | 45 | ||||
-rw-r--r-- | include/libtf/io.h | 70 | ||||
-rw-r--r-- | include/libtf/tf.h | 3 |
4 files changed, 107 insertions, 16 deletions
diff --git a/include/libtf/defines.h b/include/libtf/defines.h index b1d2aa9..144ad63 100644 --- a/include/libtf/defines.h +++ b/include/libtf/defines.h @@ -53,6 +53,9 @@ #define attribute_never_inline __attribute__((noinline)) #define attribute_weak_function __attribute__((weak)) +#define attribute_noreturn __attribute__((noreturn)) +#define attribute_warn_unused_result __attribute__((warn_unused_result)) +#define attribute_deprecated __attribute__((deprecated)) #define TF_BUG_ON(cond) if (unlikely(cond)) { \ fprintf(stderr, "BUG: failure at %s:%d/%s(): %s!\n", \ @@ -69,6 +72,8 @@ #endif /* Monotonic time */ +#define TF_INFINITE -1 + typedef uint32_t tf_mtime_t; typedef int32_t tf_mtime_diff_t; diff --git a/include/libtf/fiber.h b/include/libtf/fiber.h index 09d5ef1..91c0b3b 100644 --- a/include/libtf/fiber.h +++ b/include/libtf/fiber.h @@ -1,4 +1,4 @@ -/* tf.h - libtf main include +/* fiber.h - libtf fiber scheduler header * * Copyright (C) 2009 Timo Teräs <timo.teras@iki.fi> * All rights reserved. @@ -19,16 +19,31 @@ #include <libtf/list.h> #include <libtf/heap.h> +#define TF_UCTX_H "uctx-setjmp.h" + +/* Fiber wakeup reasons */ +#define TF_WAKEUP_NONE 0 +#define TF_WAKEUP_IMMEDIATE EAGAIN +#define TF_WAKEUP_KILL EINTR +#define TF_WAKEUP_TIMEOUT ETIMEDOUT +#define TF_WAKEUP_FD EIO + /* Scheduler */ +struct tf_fiber; + +struct tf_poll_data { + int epoll_fd; + int num_waiters; +}; + struct tf_scheduler { struct tf_list_head run_q; - struct tf_list_head sleep_q; struct tf_heap_head heap; - struct tf_fiber * active_fiber; int num_fibers; - tf_mtime_t scheduler_time; + struct tf_poll_data poll_data; + }; static inline @@ -39,6 +54,12 @@ struct tf_scheduler *tf_get_scheduler(void) } static inline +struct tf_fiber *tf_get_fiber(void) +{ + return tf_get_scheduler()->active_fiber; +} + +static inline tf_mtime_t tf_mtime(void) { return tf_get_scheduler()->scheduler_time; @@ -53,18 +74,14 @@ void *tf_fiber_get(void *data); void tf_fiber_put(void *data); /* Scheduling and fiber management */ -int tf_schedule(int err); -int tf_msleep(int milliseconds); +void tf_exit(void) attribute_noreturn; void tf_kill(void *fiber); -static inline int tf_yield(void) -{ - return tf_schedule(EAGAIN); -} +int tf_schedule(int wakeup_type); +int tf_schedule_timeout(int milliseconds); +void tf_wakeup(struct tf_fiber *fiber, int wakeup_type); -static inline int tf_exit(void) -{ - return tf_schedule(EFAULT); -} +int tf_yield(void); +int tf_msleep(int milliseconds); #endif diff --git a/include/libtf/io.h b/include/libtf/io.h new file mode 100644 index 0000000..87a6c90 --- /dev/null +++ b/include/libtf/io.h @@ -0,0 +1,70 @@ +/* tf.h - libtf io header + * + * Copyright (C) 2009 Timo Teräs <timo.teras@iki.fi> + * All rights reserved. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 or later as + * published by the Free Software Foundation. + * + * See http://www.gnu.org/ for details. + */ + +#ifndef TF_IO_H +#define TF_IO_H + +#include <sys/types.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include <netinet/in.h> + +#include <libtf/defines.h> + +struct tf_fiber; + +struct tf_sockaddr { + union { + struct sockaddr addr; + struct sockaddr_in in; + struct sockaddr_in6 in6; + }; +}; + +struct tf_fd { + int fd; + unsigned int flags; + /* Single waiter -- would be relatively trivial to modify to allow + * multiple waiters, if someone actually needs it */ + unsigned int events; + struct tf_fiber *waiting_fiber; +}; + +void tf_poll_init(void); +int tf_poll(tf_mtime_diff_t timeout); +void tf_poll_close(void); + +int tf_open(struct tf_fd *fd, const char *pathname, int flags); +int tf_open_fd(struct tf_fd *fd, int kfd); +int tf_close(struct tf_fd *fd); +int tf_read(struct tf_fd *fd, void *buf, size_t count, int timeout); +int tf_write(struct tf_fd *fd, const void *buf, size_t count, int timeout); + +int tf_socket(struct tf_fd *fd, int domain, int type, int protocol); +int tf_bind(struct tf_fd *fd, const struct tf_sockaddr *addr); +int tf_listen(struct tf_fd *fd, int backlog); +int tf_accept(struct tf_fd *fd); +int tf_connect(struct tf_fd *fd, const struct tf_sockaddr *addr, int timeout); + +ssize_t tf_recv(struct tf_fd *fd, void *buf, size_t count, int timeout); +ssize_t tf_send(struct tf_fd *fd, const void *buf, size_t count, int timeout); +ssize_t tf_recvmsg(struct tf_fd *fd, + struct tf_sockaddr *from, struct tf_sockaddr *to, + void *buf, size_t count, int timeout); +ssize_t tf_sendmsg(struct tf_fd *fd, + struct tf_sockaddr *from, const struct tf_sockaddr *to, + const void *buf, size_t count, int timeout); + +int tf_query_dns(const char *name, int num_res, struct tf_sockaddr *res, + int timeout); + +#endif diff --git a/include/libtf/tf.h b/include/libtf/tf.h index 7ff7b25..7a089ff 100644 --- a/include/libtf/tf.h +++ b/include/libtf/tf.h @@ -13,8 +13,7 @@ #ifndef TF_H #define TF_H -#define TF_UCTX_H "uctx-setjmp.h" - #include <libtf/fiber.h> +#include <libtf/io.h> #endif |