/* fiber.h - libtf fiber scheduler header * * Copyright (C) 2009 Timo Teräs * 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_FIBER_H #define TF_FIBER_H #include #include #include #include #include #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_heap_head heap; struct tf_fiber * active_fiber; int num_fibers; tf_mtime_t scheduler_time; struct tf_poll_data poll_data; }; static inline struct tf_scheduler *tf_get_scheduler(void) { extern struct tf_scheduler *__tf_scheduler; return __tf_scheduler; } 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; } /* Fiber creation */ typedef void (*tf_fiber_proc)(void *fiber); int tf_main(tf_fiber_proc fiber_main); void *tf_fiber_create(tf_fiber_proc fiber_main, int private_size); void *tf_fiber_get(void *data); void tf_fiber_put(void *data); /* Scheduling and fiber management */ void tf_exit(void) attribute_noreturn; void tf_kill(void *fiber); int tf_schedule(int wakeup_type); int tf_schedule_timeout(int milliseconds); void tf_wakeup(struct tf_fiber *fiber, int wakeup_type); int tf_yield(void); int tf_msleep(int milliseconds); #endif