-/*
- * Copyright (C) 2006 Andre Noll <maan@tuebingen.mpg.de>
- *
- * Licensed under the GPL v2. For licencing details see COPYING.
- */
+/* Copyright (C) 2006 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
/** \file sched.h Sched and task structures and exported symbols from sched.c. */
* Paraslash's scheduler.
*
* Designed with KISS in mind. It maintains a list of task structures which is
- * extended when a new task is registered. Each task may define a pre_select
+ * extended when a new task is registered. Each task may define a pre_monitor
* function which is called from the scheduler main loop before it calls
- * select(). Similarly, each task must define a post_select function which is
- * called after the select call.
+ * poll(2). Similarly, each task must define a post_monitor function which is
+ * called after poll(2) returns.
+ *
+ * \sa select(2), poll(2).
*/
struct sched {
- /** Initial value before any pre_select call. */
- struct timeval default_timeout;
- /** The current timeout for the upcoming select call. */
- struct timeval select_timeout;
- /** fds that should be watched for readability. */
- fd_set rfds;
- /** fds that should be watched for writability. */
- fd_set wfds;
- /** Highest numbered file descriptor in any of the above fd sets. */
- int max_fileno;
- /** If non-NULL, use this function instead of para_select. */
- int (*select_function)(int, fd_set *, fd_set *, struct timeval *);
+ /** Initial value (in milliseconds) before any pre_monitor call. */
+ int default_timeout;
+ /** The timeout (also in milliseconds) for the next iteration. */
+ int timeout;
+ /** Passed to poll(2). */
+ struct pollfd *pfd;
+ /** Number of elements in the above array, passed to poll(2). */
+ unsigned pfd_array_len;
+ /** Number of fds registered for montitoring so far. */
+ unsigned num_pfds;
+ /** Maps fds to indices of the pfd array. */
+ unsigned *pidx;
+ /** Mumber of elements in the above pidx array. */
+ unsigned pidx_array_len;
+ /** If non-NULL, use this function instead of \ref xpoll(). */
+ int (*poll_function)(struct pollfd *fds, nfds_t nfds, int timeout);
/** Tasks which have been registered to the scheduler. */
struct list_head task_list;
};
/** Used for log messages and by \ref get_task_list(). */
const char *name;
/**
- * The optional pre select method.
+ * Configure watch fds and impose an upper bound on the I/O timeout.
*
- * Its purpose is to add file descriptors to the fd sets of the
- * scheduler and to decrease the select timeout if necessary.
+ * If this is not NULL, the function is called at each iteration of the
+ * scheduler's main loop. Its purpose is to tell the scheduler that
+ * certain file descriptors should be monitored for readiness for I/O.
+ * The function may also lower the scheduler's timeout value (but shall
+ * never increase it) to impose an upper bound on the waiting time in
+ * case no file descriptors happen to be ready.
+ *
+ * \sa \ref time.c.
*/
- void (*pre_select)(struct sched *s, void *context);
+ void (*pre_monitor)(struct sched *s, void *context);
/**
- * The mandatory post select method.
+ * Perform I/O on file descriptors which are ready for I/O.
+ *
+ * This mandatory hook is called after the system call which monitors
+ * file descriptors returns. The function should perform non-blocking
+ * I/O on those file descriptors which are reported as being ready.
*
- * Its purpose is to evaluate and act upon the results of the previous
- * select call. If this function returns a negative value, the
- * scheduler unregisters the task.
+ * If this function returns a negative value, the scheduler unregisters
+ * the task.
*/
- int (*post_select)(struct sched *s, void *context);
+ int (*post_monitor)(struct sched *s, void *context);
/**
* This pointer is saved when the task is registered. It is passed to
- * ->pre_select() and ->post_select(). Usually this is a pointer to the
+ * ->pre_monitor() and ->post_monitor(). Usually this is a pointer to the
* struct owned by the caller which contains the task pointer.
*/
void *context;
void sched_request_timeout_ms(long unsigned ms, struct sched *s);
int sched_request_barrier(struct timeval *barrier, struct sched *s);
int sched_request_barrier_or_min_delay(struct timeval *barrier, struct sched *s);
+void sched_monitor_readfd(int fd, struct sched *s);
+void sched_monitor_writefd(int fd, struct sched *s);
+bool sched_read_ok(int fd, const struct sched *s);
+bool sched_write_ok(int fd, const struct sched *s);