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 SilcTaskCallback callback;
34 unsigned int type : 1; /* 0 = fd, 1 = timeout */
35 unsigned int valid : 1; /* Set if task is valid */
39 typedef struct SilcTaskTimeoutStruct {
40 struct SilcTaskStruct header;
41 struct SilcTaskTimeoutStruct *next;
42 struct timeval timeout;
47 struct SilcTaskStruct header;
48 unsigned int events : 15;
49 unsigned int revents : 15;
53 /* Scheduler context */
54 struct SilcScheduleStruct {
56 void *app_context; /* Application specific context */
57 SilcHashTable fd_queue; /* FD task queue */
58 SilcList timeout_queue; /* Timeout queue */
59 SilcList free_tasks; /* Timeout task freelist */
60 SilcMutex lock; /* Scheduler lock */
61 struct timeval timeout; /* Current timeout */
62 unsigned int max_tasks : 28; /* Max FD tasks */
63 unsigned int has_timeout : 1; /* Set if timeout is set */
64 unsigned int valid : 1; /* Set if scheduler is valid */
65 unsigned int signal_tasks : 1; /* Set if to dispatch signals */
68 /* Locks. These also blocks signals that we care about and thus guarantee
69 that while we are in scheduler no signals can happen. This way we can
70 synchronise signals with SILC Scheduler. */
71 #define SILC_SCHEDULE_LOCK(schedule) \
73 schedule_ops.signals_block(schedule, schedule->internal); \
74 silc_mutex_lock(schedule->lock); \
76 #define SILC_SCHEDULE_UNLOCK(schedule) \
78 silc_mutex_unlock(schedule->lock); \
79 schedule_ops.signals_unblock(schedule, schedule->internal); \
82 /* Platform specific scheduler operations */
84 /* Initializes the platform specific scheduler. This for example initializes
85 the wakeup mechanism of the scheduler. In multi-threaded environment
86 the scheduler needs to be wakenup when tasks are added or removed from
87 the task queues. Returns context to the platform specific scheduler. */
88 void *(*init)(SilcSchedule schedule, void *app_context);
90 /* Uninitializes the platform specific scheduler context. */
91 void (*uninit)(SilcSchedule schedule, void *context);
93 /* System specific select(). Returns same values as normal select(). */
94 int (*select)(SilcSchedule schedule, void *context);
96 /* Wakes up the scheduler. This is platform specific routine */
97 void (*wakeup)(SilcSchedule schedule, void *context);
100 void (*signal_register)(SilcSchedule schedule, void *context,
101 SilcUInt32 signal, SilcTaskCallback callback,
102 void *callback_context);
104 /* Unregister signal */
105 void (*signal_unregister)(SilcSchedule schedule, void *context,
108 /* Call all signals */
109 void (*signals_call)(SilcSchedule schedule, void *context);
111 /* Block registered signals in scheduler. */
112 void (*signals_block)(SilcSchedule schedule, void *context);
114 /* Unblock registered signals in schedule. */
115 void (*signals_unblock)(SilcSchedule schedule, void *context);
118 /* The generic function to add any type of task to the scheduler. This
119 used to be exported as is to application, but now they should use the
120 macro wrappers defined in silcschedule.h. For Fd task the timeout must
121 be zero, for timeout task the timeout must not be zero, for signal task
122 the fd argument is the signal. */
123 SilcTask silc_schedule_task_add(SilcSchedule schedule, SilcUInt32 fd,
124 SilcTaskCallback callback, void *context,
125 long seconds, long useconds,
128 #ifdef SILC_DIST_INPLACE
129 /* Print scheduler statistics to stdout. */
130 void silc_schedule_stats(SilcSchedule schedule);
131 #endif /* SILC_DIST_INPLACE */
133 #endif /* SILCSCHEDULE_I_H */