af2c8cf3d336877212e1a940736ee5333c1c4fb9
[crypto.git] / lib / silcutil / silcschedule.h
1 /*
2
3   silcschedule.h
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1998 - 2007 Pekka Riikonen
8
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.
12
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.
17
18 */
19
20 /****h* silcutil/SILC Schedule Interface
21  *
22  * DESCRIPTION
23  *
24  * The SILC Scheduler is the heart of any application. The scheduler provides
25  * the application's main loop that can handle incoming data, outgoing data,
26  * timeouts and dispatch different kind of tasks.
27  *
28  * The SILC Scheduler supports file descriptor based tasks and timeout tasks.
29  * File descriptor tasks are tasks that perform some operation over the
30  * specified file descriptor. These include network connections, for example.
31  * The timeout tasks are timeouts that are executed after the specified
32  * timeout has elapsed.
33  *
34  * The SILC Scheduler is designed to be the sole main loop of the application
35  * so that the application does not need any other main loop.  However,
36  * SILC Scheduler does support running the scheduler only once, so that the
37  * scheduler does not block, and thus providing a possiblity that some
38  * external main loop is run over the SILC Scheduler.
39  *
40  * Typical application first initializes the scheduler and then registers
41  * the very first tasks to the scheduler and then run the scheduler.  After
42  * the scheduler's run function returns the application is considered to be
43  * ended.
44  *
45  * On WIN32 systems the SILC Scheduler is too designed to work as the main
46  * loop of the GUI application. It can handle all Windows messages and
47  * it dispatches them from the scheduler, and thus makes it possible to
48  * create GUI applications. The scheduler can also handle all kinds of
49  * WIN32 handles, this includes sockets created by the SILC Net API routines,
50  * WSAEVENT handle objects created by Winsock2 routines and arbitrary
51  * WIN32 HANDLE objects.
52  *
53  * The SILC Scheduler supports multi-threads as well. The actual scheduler
54  * must be run in single-thread but other threads may register new tasks
55  * and unregister old tasks.  However, it is enforced that the actual
56  * task is always run in the main thread.  The scheduler is context based
57  * which makes it possible to allocate several schedulers for one application.
58  * Since the scheduler must be run in single-thread, a multi-threaded
59  * application could be created by allocating own scheduler for each of the
60  * worker threads.
61  *
62  ***/
63
64 #ifndef SILCSCHEDULE_H
65 #define SILCSCHEDULE_H
66
67 /****s* silcutil/SilcScheduleAPI/SilcSchedule
68  *
69  * NAME
70  *
71  *    typedef struct SilcScheduleStruct *SilcSchedule;
72  *
73  * DESCRIPTION
74  *
75  *    This context is the actual Scheduler and is allocated by
76  *    the silc_schedule_init funtion.  The context is given as argument
77  *    to all silc_schedule_* functions.  It must be freed by the
78  *    silc_schedule_uninit function.
79  *
80  ***/
81 typedef struct SilcScheduleStruct *SilcSchedule;
82
83 /****s* silcutil/SilcScheduleAPI/SilcTask
84  *
85  * NAME
86  *
87  *    typedef struct SilcTaskStruct *SilcTask;
88  *
89  * DESCRIPTION
90  *
91  *    This object represents one task in the scheduler.  It is allocated
92  *    by the silc_schedule_task_add function and freed by one of the
93  *    silc_schedule_task_del* functions.
94  *
95  ***/
96 typedef struct SilcTaskStruct *SilcTask;
97
98 /****d* silcutil/SilcScheduleAPI/SilcTaskEvent
99  *
100  * NAME
101  *
102  *    typedef enum { ... } SilcTaskEvent;
103  *
104  * DESCRIPTION
105  *
106  *    SILC Task event types.  The event type indicates the occurred
107  *    event of the task.  This type will be given as argument to the
108  *    SilcTaskCallback function to indicate the event for the caller.
109  *    The SILC_TASK_READ and SILC_TASK_WRITE may be set by the caller
110  *    of the silc_schedule_set_listen_fd, if the caller needs to control
111  *    the events for the task. The SILC_TASK_EXPIRE is set always only
112  *    by the scheduler when timeout expires for timeout task.  The
113  *    SILC_TASK_INTERRUPT is set for signal callback.
114  *
115  * SOURCE
116  */
117 typedef enum {
118   SILC_TASK_READ         = 0x0001,               /* Reading */
119   SILC_TASK_WRITE        = 0x0002,               /* Writing */
120   SILC_TASK_EXPIRE       = 0x0004,               /* Timeout */
121   SILC_TASK_INTERRUPT    = 0x0008,               /* Signal */
122 } SilcTaskEvent;
123 /***/
124
125 /****f* silcutil/SilcScheduleAPI/SilcTaskCallback
126  *
127  * SYNOPSIS
128  *
129  *    typedef void (*SilcTaskCallback)(SilcSchedule schedule,
130  *                                     void *app_context,
131  *                                     SilcTaskEvent type, SilcUInt32 fd,
132  *                                     void *context);
133  *
134  * DESCRIPTION
135  *
136  *    The task callback function.  This function will be called by the
137  *    scheduler when some event of the task is performed.  For example,
138  *    when data is available from the connection this will be called.
139  *
140  *    The `schedule' is the scheduler context, the `type' is the indicated
141  *    event, the `fd' is the file descriptor of the task and the `context'
142  *    is a caller specified context. If multiple events occurred this
143  *    callback is called separately for all events.  The `app_context'
144  *    is application specific context that was given as argument to the
145  *    silc_schedule_init function.  If the task is timeout task then `fd'
146  *    is zero (0).
147  *
148  *    To specify task callback function in the application using the
149  *    SILC_TASK_CALLBACK macro is recommended.
150  *
151  *    The callback should not perform lenghty or blocking operations as
152  *    this would also block all other waiting tasks.  The task callback
153  *    should either handle the operation fast or issue an asynchronous
154  *    call (like to register 0 timeout task) to handle it later.
155  *
156  ***/
157 typedef void (*SilcTaskCallback)(SilcSchedule schedule, void *app_context,
158                                  SilcTaskEvent type, SilcUInt32 fd,
159                                  void *context);
160
161 /****f* silcutil/SilcScheduleAPI/SilcTaskNotifyCb
162  *
163  * SYNOPSIS
164  *
165  *    typedef void (*SilcTaskNotifyCb)(SilcSchedule schedule,
166  *                                     SilcBool added, SilcTask task,
167  *                                     SilcBool fd_task, SilcUInt32 fd,
168  *                                     SilcTaskEvent event,
169  *                                     long seconds, long useconds,
170  *                                     void *context);
171  *
172  * DESCRIPTION
173  *
174  *    Task notify callback.  Callback of this type can be set to scheduler
175  *    by calling silc_schedule_set_notify and will be called whenever new
176  *    task is added or old task is removed.  If `added' is TRUE then `task'
177  *    is added to scheduler.  If `added' is FALSE then `task' will be removed
178  *    from the scheduler.  If `fd_task' is TRUE the `task' is file descriptor
179  *    task and has `fd' is its file descriptor.  If `fd_task' is FALSE then
180  *    the task is timeout task and `seconds' and `useconds' specify the
181  *    timeout.  The `context' is the context given to silc_schedule_set_notify.
182  *
183  * NOTES
184  *
185  *    The `schedule' is locked while this callback is called.  This means that
186  *    new tasks cannot be added or removed inside this callback.
187  *
188  *    When timeout task expires this callback is not called.  This is called
189  *    only when task is explicitly deleted from the scheduler.  Note that,
190  *    when timeout task expires it is removed from the scheduler and `task'
191  *    will become invalid.
192  *
193  *    If fd task changes its events, this will be called as if it was a new
194  *    task with different `event' mask.
195  *
196  ***/
197 typedef void (*SilcTaskNotifyCb)(SilcSchedule schedule,
198                                  SilcBool added, SilcTask task,
199                                  SilcBool fd_task, SilcUInt32 fd,
200                                  SilcTaskEvent event,
201                                  long seconds, long useconds,
202                                  void *app_context);
203
204 /* Macros */
205
206 /****d* silcutil/SilcScheduleAPI/SILC_ALL_TASKS
207  *
208  * NAME
209  *
210  *    #define SILC_ALL_TASKS ...
211  *
212  * DESCRIPTION
213  *
214  *    Marks for all tasks in the scheduler. This can be passed to
215  *    silc_schedule_task_del function to delete all tasks at once.
216  *
217  * SOURCE
218  */
219 #define SILC_ALL_TASKS ((SilcTask)1)
220 /***/
221
222 /****d* silcutil/SilcScheduleAPI/SILC_TASK_CALLBACK
223  *
224  * NAME
225  *
226  *    #define SILC_TASK_CALLBACK ...
227  *
228  * DESCRIPTION
229  *
230  *    Generic macro to declare task callback functions. This defines a
231  *    function with name `func' as a task callback function.
232  *
233  * SOURCE
234  */
235 #define SILC_TASK_CALLBACK(func)                                        \
236 void func(SilcSchedule schedule, void *app_context, SilcTaskEvent type, \
237           SilcUInt32 fd, void *context)
238 /***/
239
240 /* Prototypes */
241
242 #include "silcschedule_i.h"
243
244 /****f* silcutil/SilcScheduleAPI/silc_schedule_init
245  *
246  * SYNOPSIS
247  *
248  *    SilcSchedule silc_schedule_init(int max_tasks, void *app_context,
249  *                                    SilcStack stack);
250  *
251  * DESCRIPTION
252  *
253  *    Initializes the scheduler. This returns the scheduler context that
254  *    is given as argument usually to all silc_schedule_* functions.
255  *    The `app_context' is application specific context that is delivered
256  *    to all task callbacks. The caller must free that context.  The
257  *    'app_context' can be for example the application itself.
258  *
259  *    The `max_tasks' is the maximum number of file descriptor and socket
260  *    tasks in the scheduler.  Set value to 0 to use default.  Operating
261  *    system will enforce the final limit.  On some operating systems the
262  *    limit can be significantly increased when this function is called in
263  *    priviliged mode (as super user).
264  *
265  *    If `stack' is non-NULL all memory allocation for the scheduler is done
266  *    from the `stack'.  Scheduler's stack may be retrieved by calling
267  *    silc_schedule_get_stack.  A stack is created for scheduler always even
268  *    if `stack' is NULL.  If it is non-NULL the created stack is a child
269  *    stack using `stack' as its parent.  This means that memory allocated
270  *    by the scheduler will be returned to the `stack' when scheduler is
271  *    destroyed.
272  *
273  ***/
274 SilcSchedule silc_schedule_init(int max_tasks, void *app_context,
275                                 SilcStack stack);
276
277 /****f* silcutil/SilcScheduleAPI/silc_schedule_uninit
278  *
279  * SYNOPSIS
280  *
281  *    SilcBool silc_schedule_uninit(SilcSchedule schedule);
282  *
283  * DESCRIPTION
284  *
285  *    Uninitializes the scheduler. This is called when the program is ready
286  *    to end. This removes all tasks from the scheduler. Returns FALSE if the
287  *    scheduler could not be uninitialized. This happens when the scheduler
288  *    is still valid and silc_schedule_stop has not been called.
289  *
290  *    If SilcStack was given to silc_schedule_init all memory allocated
291  *    during the life time of the scheduler will be returned back to the
292  *    given stack.
293  *
294  ***/
295 SilcBool silc_schedule_uninit(SilcSchedule schedule);
296
297 /****f* silcutil/SilcScheduleAPI/silc_schedule_stop
298  *
299  * SYNOPSIS
300  *
301  *    void silc_schedule_stop(SilcSchedule schedule);
302  *
303  * DESCRIPTION
304  *
305  *    Stops the scheduler even if it is not supposed to be stopped yet.
306  *    After calling this, one must call silc_schedule_uninit (after the
307  *    silc_schedule has returned).  After this is called it is guaranteed
308  *    that next time the scheduler enters the main loop it will be stopped.
309  *    However, untill it enters the main loop it will not detect that
310  *    it is stopped for example if this is called from another thread.
311  *
312  ***/
313 void silc_schedule_stop(SilcSchedule schedule);
314
315 /****f* silcutil/SilcScheduleAPI/silc_schedule
316  *
317  * SYNOPSIS
318  *
319  *    void silc_schedule(SilcSchedule schedule);
320  *
321  * DESCRIPTION
322  *
323  *    The SILC scheduler.  The program will run inside this function.
324  *    When this returns the program is to be ended.  Before this function
325  *    can be called, one must call silc_schedule_init function.
326  *
327  * NOTES
328  *
329  *    On Windows this will block the calling thread but will continue
330  *    to dispatch window messages, and thus can be used as the main loop
331  *    of the program.
332  *
333  *    On Symbian this will block the calling thread.  The Symbian Active
334  *    Scheduler must be running before calling this function.
335  *
336  ***/
337 void silc_schedule(SilcSchedule schedule);
338
339 /****f* silcutil/SilcScheduleAPI/silc_schedule_one
340  *
341  * SYNOPSIS
342  *
343  *    SilcBool silc_schedule_one(SilcSchedule schedule, int timeout_usecs);
344  *
345  * DESCRIPTION
346  *
347  *    Same as the silc_schedule but runs the scheduler only one round
348  *    and then returns.  This function is handy when the SILC scheduler
349  *    is used inside some other external scheduler, for example.  If
350  *    the `timeout_usecs' is non-negative a timeout will be added to the
351  *    scheduler.  The function will not return in this timeout unless
352  *    some other event occurs.
353  *
354  *    Typically this would be called from a timeout or idle task
355  *    periodically (typically from 5-50 ms) to schedule SILC tasks.  In
356  *    this case the `timeout_usecs' is usually 0 to make the function
357  *    return immediately.
358  *
359  ***/
360 SilcBool silc_schedule_one(SilcSchedule schedule, int timeout_usecs);
361
362 /****f* silcutil/SilcScheduleAPI/silc_schedule_wakeup
363  *
364  * SYNOPSIS
365  *
366  *    void silc_schedule_wakeup(SilcSchedule schedule);
367  *
368  * DESCRIPTION
369  *
370  *    Wakes up the scheduler. This is may be used in multi-threaded
371  *    environments where threads may add new tasks or remove old tasks
372  *    from the scheduler. This is called to wake up the scheduler in the
373  *    main thread so that it detects the changes in the scheduler.
374  *    If threads support is not compiled in this function has no effect.
375  *
376  ***/
377 void silc_schedule_wakeup(SilcSchedule schedule);
378
379 /****f* silcutil/SilcScheduleAPI/silc_schedule_get_context
380  *
381  * SYNOPSIS
382  *
383  *    void *silc_schedule_get_context(SilcSchedule schedule);
384  *
385  * DESCRIPTION
386  *
387  *    Returns the application specific context that was saved into the
388  *    scheduler in silc_schedule_init function.  The context is also
389  *    returned to application in the SilcTaskCallback, but this function
390  *    may be used to get it as well if needed.
391  *
392  ***/
393 void *silc_schedule_get_context(SilcSchedule schedule);
394
395 /****f* silcutil/SilcScheduleAPI/silc_schedule_get_stack
396  *
397  * SYNOPSIS
398  *
399  *    SilcStack silc_schedule_get_stack(SilcSchedule schedule);
400  *
401  * DESCRIPTION
402  *
403  *    Returns the stack of the `schedule'.  If it is used to make memory
404  *    allocations outside the scheduler, it is recommended that a new
405  *    child stack is created by using the returned stack as a parent and
406  *    using the child stack to make the memory allocations.
407  *
408  ***/
409 SilcStack silc_schedule_get_stack(SilcSchedule schedule);
410
411 /****f* silcutil/SilcScheduleAPI/silc_schedule_set_notify
412  *
413  * SYNOPSIS
414  *
415  *    void silc_schedule_set_notify(SilcSchedule schedule,
416  *                                  SilcTaskNotifyCb notify, void *context);
417  *
418  * DESCRIPTION
419  *
420  *    Set notify callback to scheduler.  The `notify' will be called whenever
421  *    task is added to or deleted from scheduler.
422  *
423  ***/
424 void silc_schedule_set_notify(SilcSchedule schedule,
425                               SilcTaskNotifyCb notify, void *context);
426
427 /****f* silcutil/SilcScheduleAPI/silc_schedule_set_global
428  *
429  * SYNOPSIS
430  *
431  *    void silc_schedule_set_global(SilcSchedule schedule);
432  *
433  * DESCRIPTION
434  *
435  *    Sets global SilcSchedule `schedule' that can be retrieved at any time
436  *    by using silc_schedule_get_global.  The global scheduler is global only
437  *    to the current thread.  Each thread can have their own global scheduler.
438  *    If each thread must have global scheduler this must be called in each
439  *    thread.  If the global scheduler has been set already, new call will
440  *    replace the old one.
441  *
442  *    This routine is provided only as a convenience function to store
443  *    program's or thread's scheduler in one global place.  It is not mandatory
444  *    to call this function in order to use SilcSchedule.
445  *
446  *    Many routines that require SilcSchedule as an argument will call
447  *    silc_schedule_get_global if the scheduler is not provided to try to
448  *    get global scheduler.  Almost all routines in SilcSchedule API will call
449  *    silc_schedule_get_global if the SilcSchedule is not provided as argument.
450  *
451  ***/
452 void silc_schedule_set_global(SilcSchedule schedule);
453
454 /****f* silcutil/SilcScheduleAPI/silc_schedule_get_global
455  *
456  * SYNOPSIS
457  *
458  *    SilcSchedule silc_schedule_get_global(void);
459  *
460  * DESCRIPTION
461  *
462  *    Returns the thread's global scheduler that was set by calling
463  *    silc_schedule_set_global or NULL if global scheduler has not been set.
464  *
465  ***/
466 SilcSchedule silc_schedule_get_global(void);
467
468 /****f* silcutil/SilcScheduleAPI/silc_schedule_task_add_fd
469  *
470  * SYNOPSIS
471  *
472  *    SilcTask
473  *    silc_schedule_task_add_fd(SilcSchedule schedule, SilcUInt32 fd,
474  *                              SilcTaskCallback callback, void *context);
475  *
476  * DESCRIPTION
477  *
478  *    Add file descriptor task to scheduler.  The `fd' may be either real
479  *    file descriptor, socket or on some platforms an opaque file descriptor
480  *    handle.  To receive events for the file descriptor set the correct
481  *    request events with silc_schedule_set_listen_fd function.
482  *
483  *    The task will be initially set for SILC_TASK_READ events.  Setting that
484  *    event immediately after this call returns is not necessary.
485  *
486  *    This returns the new task or NULL on error.  If a task with `fd' has
487  *    already been added this will return the existing task pointer.
488  *
489  *    If `schedule' is NULL this will call silc_schedule_get_global to try to
490  *    get global scheduler.
491  *
492  ***/
493 #define silc_schedule_task_add_fd(schedule, fd, callback, context)      \
494   silc_schedule_task_add(schedule, fd, callback, context, 0, 0, SILC_TASK_FD)
495
496 /****f* silcutil/SilcScheduleAPI/silc_schedule_task_add_timeout
497  *
498  * SYNOPSIS
499  *
500  *    SilcTask
501  *    silc_schedule_task_add_timeout(SilcSchedule schedule,
502  *                                   SilcTaskCallback callback, void *context,
503  *                                   long seconds, long useconds);
504  *
505  * DESCRIPTION
506  *
507  *    Add timeout task to scheduler.  The `callback' will be called once
508  *    the specified timeout has elapsed.  The task will be removed from the
509  *    scheduler automatically once the task expires.  The event returned
510  *    to the `callback' is SILC_TASK_EXPIRE.  A task added with zero (0)
511  *    timeout will be executed immediately next time tasks are scheduled.
512  *
513  *    If `schedule' is NULL this will call silc_schedule_get_global to try to
514  *    get global scheduler.
515  *
516  ***/
517 #define silc_schedule_task_add_timeout(schedule, callback, context, s, u) \
518   silc_schedule_task_add(schedule, 0, callback, context, s, u,          \
519                          SILC_TASK_TIMEOUT)
520
521 /****f* silcutil/SilcScheduleAPI/silc_schedule_task_add_signal
522  *
523  * SYNOPSIS
524  *
525  *    SilcTask
526  *    silc_schedule_task_add_signal(SilcSchedule schedule, int signal,
527  *                                  SilcTaskCallback callback, void *context);
528  *
529  * DESCRIPTION
530  *
531  *    Add platform specific process signal handler to scheduler.  On Unix
532  *    systems the `signal' is one of the signal specified in signal(7).  On
533  *    other platforms this function may not be available at all, and has no
534  *    effect when called.  The event delivered to the `callback' is
535  *    SILC_TASK_INTERRUPT.
536  *
537  *    If `schedule' is NULL this will call silc_schedule_get_global to try to
538  *    get global scheduler.
539  *
540  * NOTES
541  *
542  *    One signal may be registered only one callback.  Adding second callback
543  *    for signal that already has one will fail.
544  *
545  *    This function always returns NULL.  To remove signal from scheduler by
546  *    the signal call silc_schedule_task_del_by_fd.
547  *
548  ***/
549 #define silc_schedule_task_add_signal(schedule, sig, callback, context) \
550   silc_schedule_task_add(schedule, sig, callback, context, 0, 0,        \
551                          SILC_TASK_SIGNAL)
552
553 /****f* silcutil/SilcScheduleAPI/silc_schedule_task_del
554  *
555  * SYNOPSIS
556  *
557  *    SilcBool silc_schedule_task_del(SilcSchedule schedule, SilcTask task);
558  *
559  * DESCRIPTION
560  *
561  *    Deletes the `task' from the scheduler indicated by the `schedule'.
562  *    After deleting the task it is guaranteed that the task callback
563  *    will not be called. If the `task' is SILC_ALL_TASKS then all
564  *    tasks is removed from the scheduler.  Returns always TRUE.
565  *
566  *    It is safe to call this function in any place. Tasks may be removed
567  *    in task callbacks (including in the task's own task callback) and
568  *    in multi-threaded environment in other threads as well.
569  *
570  *    If `schedule' is NULL this will call silc_schedule_get_global to try to
571  *    get global scheduler.
572  *
573  ***/
574 SilcBool silc_schedule_task_del(SilcSchedule schedule, SilcTask task);
575
576 /****f* silcutil/SilcScheduleAPI/silc_schedule_task_del_by_fd
577  *
578  * SYNOPSIS
579  *
580  *    SilcBool silc_schedule_task_del_by_fd(SilcSchedule schedule,
581  *                                          SilcUInt32 fd);
582  *
583  * DESCRIPTION
584  *
585  *    Deletes a task from the scheduler by the specified `fd'.  Returns
586  *    FALSE if such fd task does not exist.
587  *
588  *    It is safe to call this function in any place. Tasks may be removed
589  *    in task callbacks (including in the task's own task callback) and
590  *    in multi-threaded environment in other threads as well.
591  *
592  *    If `schedule' is NULL this will call silc_schedule_get_global to try to
593  *    get global scheduler.
594  *
595  ***/
596 SilcBool silc_schedule_task_del_by_fd(SilcSchedule schedule, SilcUInt32 fd);
597
598 /****f* silcutil/SilcScheduleAPI/silc_schedule_task_del_by_callback
599  *
600  * SYNOPSIS
601  *
602  *    SilcBool silc_schedule_task_del_by_callback(SilcSchedule schedule,
603  *                                                SilcTaskCallback callback);
604  *
605  * DESCRIPTION
606  *
607  *    Deletes a task from the scheduler by the specified `callback' task
608  *    callback function.  Returns FALSE if such task with such callback
609  *    does not exist.
610  *
611  *    It is safe to call this function in any place. Tasks may be removed
612  *    in task callbacks (including in the task's own task callback) and
613  *    in multi-threaded environment in other threads as well.
614  *
615  *    If `schedule' is NULL this will call silc_schedule_get_global to try to
616  *    get global scheduler.
617  *
618  ***/
619 SilcBool silc_schedule_task_del_by_callback(SilcSchedule schedule,
620                                             SilcTaskCallback callback);
621
622 /****f* silcutil/SilcScheduleAPI/silc_schedule_task_del_by_context
623  *
624  * SYNOPSIS
625  *
626  *    SilcBool silc_schedule_task_del_by_context(SilcSchedule schedule,
627  *                                               void *context);
628  *
629  * DESCRIPTION
630  *
631  *    Deletes a task from the scheduler by the specified `context'.  Returns
632  *    FALSE if such task with such context does not exist.
633  *
634  *    It is safe to call this function in any place. Tasks may be removed
635  *    in task callbacks (including in the task's own task callback) and
636  *    in multi-threaded environment in other threads as well.
637  *
638  *    If `schedule' is NULL this will call silc_schedule_get_global to try to
639  *    get global scheduler.
640  *
641  ***/
642 SilcBool silc_schedule_task_del_by_context(SilcSchedule schedule,
643                                            void *context);
644
645 /****f* silcutil/SilcScheduleAPI/silc_schedule_task_del_by_all
646  *
647  * SYNOPSIS
648  *
649  *    SilcBool silc_schedule_task_del_by_all(SilcSchedule schedule, int fd,
650  *                                           SilcTaskCallback callback,
651  *                                           void *context);
652  *
653  * DESCRIPTION
654  *
655  *    Deletes a task from the scheduler by the specified `fd', `callback'
656  *    and `context'.  Returns FALSE if such task does not exist.
657  *
658  *    It is safe to call this function in any place. Tasks may be removed
659  *    in task callbacks (including in the task's own task callback) and
660  *    in multi-threaded environment in other threads as well.
661  *
662  *    If `schedule' is NULL this will call silc_schedule_get_global to try to
663  *    get global scheduler.
664  *
665  ***/
666 SilcBool silc_schedule_task_del_by_all(SilcSchedule schedule, int fd,
667                                        SilcTaskCallback callback,
668                                        void *context);
669
670 /****f* silcutil/SilcScheduleAPI/silc_schedule_set_listen_fd
671  *
672  * SYNOPSIS
673  *
674  *    SilcBool silc_schedule_set_listen_fd(SilcSchedule schedule,
675  *                                         SilcUInt32 fd,
676  *                                         SilcTaskEvent mask,
677  *                                         SilcBool send_events);
678  *
679  * DESCRIPTION
680  *
681  *    Sets a file descriptor `fd' to be listened by the scheduler for
682  *    `mask' events.  To tell scheduler not to listen anymore for this
683  *    file descriptor call the silc_schedule_unset_listen_fd function.
684  *    When new task is created with silc_schedule_task_add the event
685  *    for the task's fd is initially set to SILC_TASK_READ. If you need
686  *    to control the task's fd's events you must call this function
687  *    whenever you need to change the events. This can be called multiple
688  *    times to change the events.
689  *
690  *    If the `send_events' is TRUE then this function sends the events
691  *    in `mask' to the application.  If FALSE then they are sent only
692  *    after the event occurs in reality.  In normal cases the `send_events'
693  *    is set to FALSE.
694  *
695  *    If `schedule' is NULL this will call silc_schedule_get_global to try to
696  *    get global scheduler.
697  *
698  *    Returns FALSE if the operation could not performed and TRUE if it
699  *    was a success.
700  *
701  ***/
702 SilcBool silc_schedule_set_listen_fd(SilcSchedule schedule, SilcUInt32 fd,
703                                      SilcTaskEvent mask, SilcBool send_events);
704
705 /****f* silcutil/SilcScheduleAPI/silc_schedule_get_fd_events
706  *
707  * SYNOPSIS
708  *
709  *    SilcTaskEvent silc_schedule_get_fd_events(SilcSchedule schedule,
710  *                                              SilcUInt32 fd);
711  *
712  * DESCRIPTION
713  *
714  *    Returns the file descriptor `fd' current requested events mask,
715  *    or 0 on error.
716  *
717  *    If `schedule' is NULL this will call silc_schedule_get_global to try to
718  *    get global scheduler.
719  *
720  ***/
721 SilcTaskEvent silc_schedule_get_fd_events(SilcSchedule schedule,
722                                           SilcUInt32 fd);
723
724 /****f* silcutil/SilcScheduleAPI/silc_schedule_unset_listen_fd
725  *
726  * SYNOPSIS
727  *
728  *    void silc_schedule_unset_listen_fd(SilcSchedule schedule, SilcUInt32 fd);
729  *
730  * DESCRIPTION
731  *
732  *    Tells the scheduler not to listen anymore for the specified
733  *    file descriptor `fd'. No events will be detected for the `fd'
734  *    after calling this function.
735  *
736  *    If `schedule' is NULL this will call silc_schedule_get_global to try to
737  *    get global scheduler.
738  *
739  ***/
740 void silc_schedule_unset_listen_fd(SilcSchedule schedule, SilcUInt32 fd);
741
742 #endif