+/* System specific select(). Returns same values as normal select(). */
+int silc_select(SilcScheduleFd fds, SilcUInt32 fds_count, struct timeval *timeout);
+
+/* Initializes the wakeup of the scheduler. In multi-threaded environment
+ the scheduler needs to be wakenup when tasks are added or removed from
+ the task queues. This will initialize the wakeup for the scheduler.
+ Any tasks that needs to be registered must be registered to the `queue'.
+ It is guaranteed that the scheduler will automatically free any
+ registered tasks in this queue. This is system specific routine. */
+void *silc_schedule_wakeup_init(SilcSchedule schedule);
+
+/* Uninitializes the system specific wakeup. */
+void silc_schedule_wakeup_uninit(void *context);
+
+/* Wakes up the scheduler. This is platform specific routine */
+void silc_schedule_wakeup_internal(void *context);
+
+
+/* Internal task management routines. */
+
+static void silc_task_queue_alloc(SilcTaskQueue *queue);
+static void silc_task_queue_free(SilcTaskQueue queue);
+static SilcTask silc_task_find(SilcTaskQueue queue, SilcUInt32 fd);
+static SilcTask silc_task_add(SilcTaskQueue queue, SilcTask newtask,
+ SilcTaskPriority priority);
+static SilcTask silc_task_get_first(SilcTaskQueue queue, SilcTask first);
+static SilcTask silc_task_add_timeout(SilcTaskQueue queue, SilcTask newtask,
+ SilcTaskPriority priority);
+static int silc_schedule_task_remove(SilcTaskQueue queue, SilcTask task);
+static int silc_schedule_task_timeout_compare(struct timeval *smaller,
+ struct timeval *bigger);
+static void silc_task_del_by_context(SilcTaskQueue queue, void *context);
+static void silc_task_del_by_callback(SilcTaskQueue queue,
+ SilcTaskCallback callback);
+static void silc_task_del_by_fd(SilcTaskQueue queue, SilcUInt32 fd);
+
+/* Returns the task queue by task type */
+#define SILC_SCHEDULE_GET_QUEUE(type) \
+ (type == SILC_TASK_FD ? schedule->fd_queue : \
+ type == SILC_TASK_TIMEOUT ? schedule->timeout_queue : \
+ schedule->generic_queue)
+
+/* SILC Task object. Represents one task in the scheduler. */
+struct SilcTaskStruct {
+ SilcUInt32 fd;
+ struct timeval timeout;
+ SilcTaskCallback callback;
+ void *context;
+ bool valid;
+ SilcTaskPriority priority;
+ SilcTaskType type;
+
+ /* Pointers forming doubly linked circular list */
+ struct SilcTaskStruct *next;
+ struct SilcTaskStruct *prev;
+};
+
+/* SILC Task Queue object. The queue holds all the tasks in the scheduler.
+ There are always three task queues in the scheduler. One for non-timeout
+ tasks (fd tasks performing tasks over specified file descriptor),
+ one for timeout tasks and one for generic tasks. */
+struct SilcTaskQueueStruct {
+ SilcTask task; /* Pointer to all tasks */
+ struct timeval timeout; /* Current timeout */
+ SILC_MUTEX_DEFINE(lock); /* Queue's lock */
+};
+
+/*
+ SILC Scheduler structure.
+
+ This is the actual schedule object in SILC. Both SILC client and server
+ uses this same scheduler. Actually, this scheduler could be used by any
+ program needing scheduling.
+
+ Following short description of the fields:
+
+ SilcTaskQueue fd_queue
+
+ Task queue hook for non-timeout tasks. Usually this means that these
+ tasks perform different kind of I/O on file descriptors. File
+ descriptors are usually network sockets but they actually can be
+ any file descriptors. This hook is initialized in silc_schedule_init
+ function. Timeout tasks should not be added to this queue because
+ they will never expire.
+
+ SilcTaskQueue timeout_queue
+
+ Task queue hook for timeout tasks. This hook is reserved specificly
+ for tasks with timeout. Non-timeout tasks should not be added to this
+ queue because they will never get scheduled. This hook is also
+ initialized in silc_schedule_init function.
+
+ SilcTaskQueue generic_queue
+
+ Task queue hook for generic tasks. This hook is reserved specificly
+ for generic tasks, tasks that apply to all file descriptors, except
+ to those that have specificly registered a non-timeout task. This hook
+ is also initialized in silc_schedule_init function.
+
+ SilcScheduleFd fd_list
+
+ List of file descriptors the scheduler is supposed to be listenning.
+ This is updated internally.
+
+ SilcUInt32 max_fd
+ SilcUInt32 last_fd
+
+ Size of the fd_list list. There can be `max_fd' many tasks in
+ the scheduler at once. The `last_fd' is the last valid entry
+ in the fd_list.
+
+ struct timeval *timeout;
+
+ Pointer to the schedules next timeout. Value of this timeout is
+ automatically updated in the silc_schedule function.
+
+ bool valid
+
+ Marks validity of the scheduler. This is a boolean value. When this
+ is false the scheduler is terminated and the program will end. This
+ set to true when the scheduler is initialized with silc_schedule_init
+ function.
+
+ fd_set in
+ fd_set out
+
+ File descriptor sets for select(). These are automatically managed
+ by the scheduler and should not be touched otherwise.
+
+ void *wakeup
+
+ System specific wakeup context. On multi-threaded environments the
+ scheduler needs to be wakenup (in the thread) when tasks are added
+ or removed. This is initialized by silc_schedule_wakeup_init.
+
+ SILC_MUTEX_DEFINE(lock)
+
+ Scheduler lock.
+
+*/
+struct SilcScheduleStruct {
+ SilcTaskQueue fd_queue;
+ SilcTaskQueue timeout_queue;
+ SilcTaskQueue generic_queue;
+ SilcScheduleFd fd_list;
+ SilcUInt32 max_fd;
+ SilcUInt32 last_fd;
+ struct timeval *timeout;
+ bool valid;
+ void *wakeup;
+ SILC_MUTEX_DEFINE(lock);
+ bool is_locked;
+};
+
+/* Initializes the scheduler. This returns the scheduler context that
+ is given as arugment usually to all silc_schedule_* functions.
+ The `max_tasks' indicates the number of maximum tasks that the
+ scheduler can handle. */
+
+SilcSchedule silc_schedule_init(int max_tasks)