+#include "silcschedule_i.h"
+
+/* Forward declarations */
+typedef struct SilcTaskQueueStruct *SilcTaskQueue;
+
+/* System specific routines. Implemented under unix/ and win32/. */
+
+/* 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