5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2001 - 2006 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
20 #ifndef SILCSCHEDULE_I_H
21 #define SILCSCHEDULE_I_H
23 #ifndef SILCSCHEDULE_H
24 #error "Do not include this header directly"
27 #include "silchashtable.h"
31 struct SilcTaskStruct {
32 struct SilcTaskStruct *next;
33 SilcTaskCallback callback;
35 unsigned int type : 1; /* 0 = fd, 1 = timeout */
36 unsigned int valid : 1; /* Set if task is valid */
40 typedef struct SilcTaskTimeoutStruct {
41 struct SilcTaskStruct header;
42 struct timeval timeout;
46 typedef struct SilcTaskFdStruct {
47 struct SilcTaskStruct header;
48 unsigned int scheduled : 1;
49 unsigned int events : 14;
50 unsigned int revents : 15;
54 /* Scheduler context */
55 struct SilcScheduleStruct {
57 void *app_context; /* Application specific context */
58 SilcHashTable fd_queue; /* FD task queue */
59 SilcList fd_dispatch; /* Dispatched FDs */
60 SilcList timeout_queue; /* Timeout queue */
61 SilcList free_tasks; /* Timeout task freelist */
62 SilcMutex lock; /* Scheduler lock */
63 struct timeval timeout; /* Current timeout */
64 unsigned int max_tasks : 29; /* Max FD tasks */
65 unsigned int has_timeout : 1; /* Set if timeout is set */
66 unsigned int valid : 1; /* Set if scheduler is valid */
67 unsigned int signal_tasks : 1; /* Set if to dispatch signals */
70 /* Locks. These also blocks signals that we care about and thus guarantee
71 that while we are in scheduler no signals can happen. This way we can
72 synchronise signals with SILC Scheduler. */
73 #define SILC_SCHEDULE_LOCK(schedule) \
75 silc_mutex_lock(schedule->lock); \
76 schedule_ops.signals_block(schedule, schedule->internal); \
78 #define SILC_SCHEDULE_UNLOCK(schedule) \
80 schedule_ops.signals_unblock(schedule, schedule->internal); \
81 silc_mutex_unlock(schedule->lock); \
84 /* Platform specific scheduler operations */
86 /* Initializes the platform specific scheduler. This for example initializes
87 the wakeup mechanism of the scheduler. In multi-threaded environment
88 the scheduler needs to be wakenup when tasks are added or removed from
89 the task queues. Returns context to the platform specific scheduler. */
90 void *(*init)(SilcSchedule schedule, void *app_context);
92 /* Uninitializes the platform specific scheduler context. */
93 void (*uninit)(SilcSchedule schedule, void *context);
95 /* System specific waiter. This must fill the schedule->fd_dispatch queue
96 with valid tasks that has something to dispatch, when this returns. */
97 int (*schedule)(SilcSchedule schedule, void *context);
99 /* Schedule `task' with events `event_mask'. Zero `event_mask'
100 unschedules the task. */
101 SilcBool (*schedule_fd)(SilcSchedule schedule, void *context,
102 SilcTaskFd task, SilcTaskEvent event_mask);
104 /* Wakes up the scheduler. This is platform specific routine */
105 void (*wakeup)(SilcSchedule schedule, void *context);
107 /* Register signal */
108 void (*signal_register)(SilcSchedule schedule, void *context,
109 SilcUInt32 signal, SilcTaskCallback callback,
110 void *callback_context);
112 /* Unregister signal */
113 void (*signal_unregister)(SilcSchedule schedule, void *context,
116 /* Call all signals */
117 void (*signals_call)(SilcSchedule schedule, void *context);
119 /* Block registered signals in scheduler. */
120 void (*signals_block)(SilcSchedule schedule, void *context);
122 /* Unblock registered signals in schedule. */
123 void (*signals_unblock)(SilcSchedule schedule, void *context);
126 /* The generic function to add any type of task to the scheduler. This
127 used to be exported as is to application, but now they should use the
128 macro wrappers defined in silcschedule.h. For Fd task the timeout must
129 be zero, for timeout task the timeout must not be zero, for signal task
130 the fd argument is the signal. */
131 SilcTask silc_schedule_task_add(SilcSchedule schedule, SilcUInt32 fd,
132 SilcTaskCallback callback, void *context,
133 long seconds, long useconds,
136 #ifdef SILC_DIST_INPLACE
137 /* Print scheduler statistics to stdout. */
138 void silc_schedule_stats(SilcSchedule schedule);
139 #endif /* SILC_DIST_INPLACE */
141 #endif /* SILCSCHEDULE_I_H */