1 /****h* silcutil/silcschedule.h
9 * Author: Pekka Riikonen <priikone@silcnet.org>
11 * Copyright (C) 1998 - 2001 Pekka Riikonen
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
25 * The SILC Scheduler is the heart of any application. The scheduler provides
26 * the application's main loop that can handle incoming data, outgoing data,
27 * timeouts and dispatch different kind of tasks.
29 * The SILC Scheduler supports file descriptor based tasks, timeout tasks
30 * and generic tasks. File descriptor tasks are tasks that perform some
31 * operation over the specified file descriptor. These include network
32 * connections, for example. The timeout tasks are timeouts that are executed
33 * after the specified timeout has elapsed. The generic tasks are tasks that
34 * apply to all registered file descriptors thus providing one task that
35 * applies to many independent connections.
37 * The SILC Scheduler is designed to be the sole main loop of the application
38 * so that the application does not need any other main loop. However,
39 * SILC Scheduler does support running the scheduler only once, so that the
40 * scheduler does not block, and thus providing a possiblity that some
41 * external main loop is run over the SILC Scheduler. However, these
42 * applications are considered to be special cases.
44 * Typical application first initializes the scheduler and then registers
45 * the very first tasks to the scheduler and then run the scheduler. After
46 * the scheduler's run function returns the application is considered to be
49 * On WIN32 systems the SILC Scheduler is also designed to work as the main
50 * loop of the GUI application. It can handle all Windows messages and
51 * it dispatches them from the scheduler and thus makes it possible to
52 * create GUI applications. The scheduler can also handle all kinds of
53 * WIN32 handles, this includes sockets created by the SILC Net API routines,
54 * WSAEVENT handle objects and arbitrary WIN32 HANDLE objects.
56 * The SILC Scheduler supports multi-threads as well. The actual scheduler
57 * must be run in single-thread but other threads may register new tasks
58 * and unregister old tasks. However, it is enforced that the actual
59 * task is always run in the main thread. The scheduler is context based
60 * which makes it possible to allocate several schedulers for one application.
61 * Since the scheduler must be run in single-thread, a multi-threaded
62 * application could be created by allocating own scheduler for each of the
67 #ifndef SILCSCHEDULE_H
68 #define SILCSCHEDULE_H
70 /****s* silcutil/SilcScheduleAPI/SilcSchedule
74 * typedef struct SilcScheduleStruct *SilcSchedule;
78 * This context is the actual Scheduler and is allocated by
79 * the silc_schedule_init funtion. The context is given as argument
80 * to all silc_schedule_* functions. It must be freed by the
81 * silc_schedule_uninit function.
84 typedef struct SilcScheduleStruct *SilcSchedule;
86 /****s* silcutil/SilcScheduleAPI/SilcTask
90 * typedef struct SilcTaskStruct *SilcTask;
94 * This object represents one task in the scheduler. It is allocated
95 * by the silc_schedule_task_add function and freed by one of the
96 * silc_schedule_task_del* functions.
99 typedef struct SilcTaskStruct *SilcTask;
101 /****d* silcutil/SilcScheduleAPI/SilcTaskType
105 * typedef enum { ... } SilcTaskType;
109 * SILC has three types of tasks, non-timeout tasks (tasks that perform
110 * over file descriptors), timeout tasks and generic tasks (tasks that
111 * apply to every file descriptor). This type is sent as argument for the
112 * task registering function, silc_schedule_task_add.
117 /* File descriptor task that performs some event over file descriptors.
118 These tasks are for example network connections. */
121 /* Timeout tasks are tasks that are executed after the specified
122 time has elapsed. After the task is executed the task is removed
123 automatically from the scheduler. It is safe to re-register the
124 task in task callback. It is also safe to unregister a task in
125 the task callback. */
128 /* Generic tasks are non-timeout tasks and they apply to all file
129 descriptors, except to those that have explicitly registered a
130 non-timeout task. These tasks are there to make it simpler and faster
131 to execute common code that applies to all connections. These are,
132 for example, receiving packets from network and sending packets to
133 network. It doesn't make much sense to register a task that receives
134 a packet from network to every connection when you can have one task
135 that applies to all connections. This is what generic tasks are for-
136 Generic tasks are not bound to any specific file descriptor, however,
137 the correct file descriptor must be passed as argument to task
138 registering function. */
143 /****d* silcutil/SilcScheduleAPI/SilcTaskEvent
147 * typedef enum { ... } SilcTaskEvent;
151 * SILC Task event types. The event type indicates the occurred
152 * event of the task. This type will be given as argument to the
153 * SilcTaskCallback function to indicate the event for the caller.
154 * The SILC_TASK_READ and SILC_TASK_WRITE may be set by the caller
155 * of the silc_schedule_set_listen_fd if the caller needs to control
156 * the events for the task. The SILC_TASK_TIMEOUT is set always only
157 * by the scheduler when timeout for timeout task occurs.
162 SILC_TASK_READ = 0x0001, /* Reading */
163 SILC_TASK_WRITE = 0x0002, /* Writing */
164 SILC_TASK_EXPIRE = 0x0004, /* Timeout */
168 /****d* silcutil/SilcScheduleAPI/SilcTaskPriority
172 * typedef enum { ... } SilcTaskPriority
176 * Task priorities. Tasks may be registered with different priorities.
177 * This type defines the different task priorities. The priorities
178 * behaves same for all type of tasks, fd tasks, timeout tasks and
184 /* Lowest priority. The task is scheduled to run after its timeout
185 has expired only and only when every other task with higher priority
186 has already been run. For non-timeout tasks this priority behaves
187 same way. Life is not fair for tasks with this priority. */
190 /* Normal priority that is used mostly in Silc. This is priority that
191 should always be used unless you specificly need some other priority.
192 The scheduler will run this task as soon as its timeout has expired.
193 For non-timeout tasks this priority behaves same way. Tasks are run
194 in FIFO (First-In-First-Out) order. */
195 SILC_TASK_PRI_NORMAL,
199 /****f* silcutil/SilcScheduleAPI/silc_schedule_init
203 * typedef void (*SilcTaskCallback)(SilcSchedule schedule,
204 * SilcTaskEvent type, uint32 fd,
209 * The task callback function. This function will be called by the
210 * scheduler when some event of the task is performed. For example,
211 * when data is available from the connection this will be called.
213 * The `schedule' is the scheduler context, the `type' is the indicated
214 * event, the `fd' is the file descriptor of the task and the `context'
215 * is a caller specified context. If multiple events occurred this
216 * call is called separately for all events.
218 * To specify task callback function in the application using the
219 * SILC_TASK_CALLBACK and SILC_TASK_CALLBACK_GLOBAL macros is
223 typedef void (*SilcTaskCallback)(SilcSchedule schedule, SilcTaskEvent type,
224 uint32 fd, void *context);
228 /****d* silcutil/SilcScheduleAPI/SILC_ALL_TASKS
232 * #define SILC_ALL_TASKS ...
236 * Marks for all tasks in the scheduler. This can be passed to
237 * silc_schedule_task_del function to delete all tasks at once.
241 #define SILC_ALL_TASKS ((SilcTask)1)
244 /****d* silcutil/SilcScheduleAPI/SILC_TASK_CALLBACK
248 * #define SILC_TASK_CALLBACK ...
252 * Generic macro to define task callback functions. This defines a
253 * static function with name `func' as a task callback function.
257 #define SILC_TASK_CALLBACK(func) \
258 static void func(SilcSchedule schedule, SilcTaskEvent type, \
259 uint32 fd, void *context)
262 /****d* silcutil/SilcScheduleAPI/SILC_TASK_CALLBACK
266 * #define SILC_TASK_CALLBACK_GLOBAL ...
270 * Generic macro to define task callback functions. This defines a
271 * function with name `func' as a task callback function. This
272 * differs from SILC_TASK_CALLBACK in that the defined function is
273 * not static function.
277 #define SILC_TASK_CALLBACK_GLOBAL(func) \
278 void func(SilcSchedule schedule, SilcTaskEvent type, \
279 uint32 fd, void *context)
284 /****f* silcutil/SilcScheduleAPI/silc_schedule_init
288 * SilcSchedule silc_schedule_init(int max_tasks);
292 * Initializes the scheduler. This returns the scheduler context that
293 * is given as arugment usually to all silc_schedule_* functions.
294 * The `max_tasks' indicates the number of maximum tasks that the
295 * scheduler can handle.
298 SilcSchedule silc_schedule_init(int max_tasks);
300 /****f* silcutil/SilcScheduleAPI/silc_schedule_uninit
304 * bool silc_schedule_uninit(SilcSchedule schedule);
308 * Uninitializes the schedule. This is called when the program is ready
309 * to end. This removes all tasks from the scheduler. Returns FALSE if the
310 * scheduler could not be uninitialized. This happens when the scheduler
311 * is still valid and silc_schedule_stop has not been called.
314 bool silc_schedule_uninit(SilcSchedule schedule);
316 /****f* silcutil/SilcScheduleAPI/silc_schedule_stop
320 * void silc_schedule_stop(SilcSchedule schedule);
324 * Stops the scheduler even if it is not supposed to be stopped yet.
325 * After calling this, one must call silc_schedule_uninit (after the
326 * silc_schedule has returned).
329 void silc_schedule_stop(SilcSchedule schedule);
331 /****f* silcutil/SilcScheduleAPI/silc_schedule
335 * void silc_schedule(SilcSchedule schedule);
339 * The SILC scheduler. This is actually the main routine in SILC programs.
340 * When this returns the program is to be ended. Before this function can
341 * be called, one must call silc_schedule_init function.
344 void silc_schedule(SilcSchedule schedule);
346 /****f* silcutil/SilcScheduleAPI/silc_schedule
350 * bool silc_schedule_one(SilcSchedule schedule, int block);
354 * Same as the silc_schedule but runs the scheduler only one round
355 * and then returns. This function is handy when the SILC scheduler
356 * is used inside some other external scheduler, for example. If
357 * the `timeout_usecs' is non-negative a timeout will be added to the
358 * scheduler. The function will not return in this timeout unless
359 * some other event occurs.
362 bool silc_schedule_one(SilcSchedule schedule, int timeout_usecs);
364 /****f* silcutil/SilcScheduleAPI/silc_schedule_wakeup
368 * void silc_schedule_wakeup(SilcSchedule schedule);
372 * Wakes up the scheduler. This is used only in multi-threaded
373 * environments where threads may add new tasks or remove old tasks
374 * from the scheduler. This is called to wake up the scheduler in the
375 * main thread so that it detects the changes in the scheduler.
376 * If threads support is not compiled in this function has no effect.
377 * Implementation of this function may be platform specific.
380 void silc_schedule_wakeup(SilcSchedule schedule);
382 /****f* silcutil/SilcScheduleAPI/silc_schedule_task_add
386 * SilcTask silc_schedule_task_add(SilcSchedule schedule, uint32 fd,
387 * SilcTaskCallback callback,
389 * long seconds, long useconds,
391 * SilcTaskPriority priority);
395 * Registers a new task to the scheduler. This same function is used
396 * to register all types of tasks. The `type' argument tells what type
397 * of the task is. Note that when registering non-timeout tasks one
398 * should also pass 0 as timeout, as the timeout will be ignored anyway.
399 * Also, note, that one cannot register timeout task with 0 timeout.
400 * There cannot be zero timeouts, passing zero means no timeout is used
401 * for the task and SILC_TASK_FD_TASK is used as default task type in
404 * The `schedule' is the scheduler context. The `fd' is the file
405 * descriptor of the task. On WIN32 systems the `fd' is not actual
406 * file descriptor but some WIN32 event handle. On WIN32 system the `fd'
407 * may be a socket created by the SILC Net API routines, WSAEVENT object
408 * created by Winsock2 network routines or arbitrary WIN32 HANDLE object.
409 * On Unix systems the `fd' is always the real file descriptor.
411 * The `callback' is the task callback that will be called when some
412 * event occurs for this task. The `context' is sent as argument to
413 * the task `callback' function. For timeout tasks the callback is
414 * called after the specified timeout has elapsed.
416 * If the `type' is SILC_TASK_TIMEOUT then `seconds' and `useconds'
417 * may be non-zero. Otherwise they should be zero. The `priority'
418 * indicates the priority of the task.
420 * It is always safe to call this function in any place. New tasks
421 * may be added also in task callbacks, and in multi-threaded environment
422 * in other threads as well.
425 SilcTask silc_schedule_task_add(SilcSchedule schedule, uint32 fd,
426 SilcTaskCallback callback, void *context,
427 long seconds, long useconds,
429 SilcTaskPriority priority);
431 /****f* silcutil/SilcScheduleAPI/silc_schedule_task_del
435 * void silc_schedule_task_del(SilcSchedule schedule, SilcTask task);
439 * Deletes the `task' from the scheduler indicated by the `schedule'.
440 * After deleting the task it is guaranteed that the task callback
441 * will not be called. If the `task' is SILC_ALL_TASKS then all
442 * tasks is removed from the scheduler.
444 * It is safe to call this function in any place. Tasks may be removed
445 * in task callbacks (including in the task's own task callback) and
446 * in multi-threaded environment in other threads as well.
449 void silc_schedule_task_del(SilcSchedule schedule, SilcTask task);
451 /****f* silcutil/SilcScheduleAPI/silc_schedule_task_del_by_fd
455 * void silc_schedule_task_del_by_fd(SilcSchedule schedule, uint32 fd);
459 * Deletes a task from the scheduler by the specified `fd'.
461 * It is safe to call this function in any place. Tasks may be removed
462 * in task callbacks (including in the task's own task callback) and
463 * in multi-threaded environment in other threads as well.
466 void silc_schedule_task_del_by_fd(SilcSchedule schedule, uint32 fd);
468 /****f* silcutil/SilcScheduleAPI/silc_schedule_task_del_by_callback
472 * void silc_schedule_task_del_by_callback(SilcSchedule schedule,
473 * SilcTaskCallback callback);
477 * Deletes a task from the scheduler by the specified `callback' task
480 * It is safe to call this function in any place. Tasks may be removed
481 * in task callbacks (including in the task's own task callback) and
482 * in multi-threaded environment in other threads as well.
485 void silc_schedule_task_del_by_callback(SilcSchedule schedule,
486 SilcTaskCallback callback);
488 /****f* silcutil/SilcScheduleAPI/silc_schedule_task_del_by_context
492 * void silc_schedule_task_del_by_context(SilcSchedule schedule,
497 * Deletes a task from the scheduler by the specified `context'.
499 * It is safe to call this function in any place. Tasks may be removed
500 * in task callbacks (including in the task's own task callback) and
501 * in multi-threaded environment in other threads as well.
504 void silc_schedule_task_del_by_context(SilcSchedule schedule, void *context);
506 /****f* silcutil/SilcScheduleAPI/silc_schedule_set_listen_fd
510 * void silc_schedule_set_listen_fd(SilcSchedule schedule, uint32 fd,
511 * SilcTaskEvent mask);
515 * Sets a file descriptor `fd' to be listened by the scheduler for
516 * `mask' events. To tell scheduler not to listen anymore for this
517 * file descriptor call the silc_schedule_unset_listen_fd function.
518 * When new task is created with silc_schedule_task_add the event
519 * for the task's fd is initially set to SILC_TASK_READ. If you need
520 * to control the task's fd's events you must call this function
521 * whenever you need to change the events. This can be called multiple
522 * times to change the events.
525 void silc_schedule_set_listen_fd(SilcSchedule schedule, uint32 fd,
528 /****f* silcutil/SilcScheduleAPI/silc_schedule_unset_listen_fd
532 * void silc_schedule_unset_listen_fd(SilcSchedule schedule, uint32 fd);
536 * Tells the scheduler not to listen anymore for the specified
537 * file descriptor `fd'. No events will be detected for the `fd'
538 * after calling this function.
541 void silc_schedule_unset_listen_fd(SilcSchedule schedule, uint32 fd);