Integer type name change.
[silc.git] / lib / silcutil / silcschedule.h
1 /*
2   
3   silcschedule.h
4  
5   COPYRIGHT
6  
7   Author: Pekka Riikonen <priikone@silcnet.org>
8  
9   Copyright (C) 1998 - 2001 Pekka Riikonen
10  
11   This program is free software; you can redistribute it and/or modify
12   it under the terms of the GNU General Public License as published by
13   the Free Software Foundation; either version 2 of the License, or
14   (at your option) any later version.
15  
16   This program is distributed in the hope that it will be useful,
17   but WITHOUT ANY WARRANTY; without even the implied warranty of
18   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   GNU General Public License for more details.
20
21 */
22  
23 /****h* silcutil/SilcScheduleAPI
24  *
25  * DESCRIPTION
26  *
27  * The SILC Scheduler is the heart of any application. The scheduler provides
28  * the application's main loop that can handle incoming data, outgoing data,
29  * timeouts and dispatch different kind of tasks.
30  *
31  * The SILC Scheduler supports file descriptor based tasks, timeout tasks
32  * and generic tasks. File descriptor tasks are tasks that perform some 
33  * operation over the specified file descriptor. These include network 
34  * connections, for example. The timeout tasks are timeouts that are executed
35  * after the specified timeout has elapsed. The generic tasks are tasks that
36  * apply to all registered file descriptors thus providing one task that
37  * applies to many independent connections.
38  *
39  * The SILC Scheduler is designed to be the sole main loop of the application
40  * so that the application does not need any other main loop.  However,
41  * SILC Scheduler does support running the scheduler only once, so that the
42  * scheduler does not block, and thus providing a possiblity that some
43  * external main loop is run over the SILC Scheduler. However, these 
44  * applications are considered to be special cases.
45  *
46  * Typical application first initializes the scheduler and then registers
47  * the very first tasks to the scheduler and then run the scheduler.  After
48  * the scheduler's run function returns the application is considered to be 
49  * ended.
50  *
51  * On WIN32 systems the SILC Scheduler is too designed to work as the main
52  * loop of the GUI application. It can handle all Windows messages and
53  * it dispatches them from the scheduler, and thus makes it possible to
54  * create GUI applications. The scheduler can also handle all kinds of
55  * WIN32 handles, this includes sockets created by the SILC Net API routines,
56  * WSAEVENT handle objects created by Winsock2 routines and arbitrary 
57  * WIN32 HANDLE objects.
58  *
59  * The SILC Scheduler supports multi-threads as well. The actual scheduler
60  * must be run in single-thread but other threads may register new tasks
61  * and unregister old tasks.  However, it is enforced that the actual
62  * task is always run in the main thread.  The scheduler is context based
63  * which makes it possible to allocate several schedulers for one application.
64  * Since the scheduler must be run in single-thread, a multi-threaded
65  * application could be created by allocating own scheduler for each of the
66  * worker threads.
67  *
68  ***/
69
70 #ifndef SILCSCHEDULE_H
71 #define SILCSCHEDULE_H
72
73 /****s* silcutil/SilcScheduleAPI/SilcSchedule
74  *
75  * NAME
76  * 
77  *    typedef struct SilcScheduleStruct *SilcSchedule;
78  *
79  * DESCRIPTION
80  *
81  *    This context is the actual Scheduler and is allocated by
82  *    the silc_schedule_init funtion.  The context is given as argument
83  *    to all silc_schedule_* functions.  It must be freed by the 
84  *    silc_schedule_uninit function.
85  *
86  ***/
87 typedef struct SilcScheduleStruct *SilcSchedule;
88
89 /****s* silcutil/SilcScheduleAPI/SilcTask
90  *
91  * NAME
92  * 
93  *    typedef struct SilcTaskStruct *SilcTask;
94  *
95  * DESCRIPTION
96  *
97  *    This object represents one task in the scheduler.  It is allocated
98  *    by the silc_schedule_task_add function and freed by one of the
99  *    silc_schedule_task_del* functions.
100  *
101  ***/
102 typedef struct SilcTaskStruct *SilcTask;
103
104 /****d* silcutil/SilcScheduleAPI/SilcTaskType
105  *
106  * NAME
107  * 
108  *    typedef enum { ... } SilcTaskType;
109  *
110  * DESCRIPTION
111  *
112  *    SILC has three types of tasks, non-timeout tasks (tasks that perform
113  *    over file descriptors), timeout tasks and generic tasks (tasks that
114  *    apply to every file descriptor). This type is sent as argument for the 
115  *    task registering function, silc_schedule_task_add.
116  *
117  * SOURCE
118  */
119 typedef enum {
120   /* File descriptor task that performs some event over file descriptors.
121      These tasks are for example network connections. */
122   SILC_TASK_FD,
123   
124   /* Timeout tasks are tasks that are executed after the specified 
125      time has elapsed. After the task is executed the task is removed
126      automatically from the scheduler. It is safe to re-register the
127      task in task callback. It is also safe to unregister a task in
128      the task callback. */
129   SILC_TASK_TIMEOUT,
130
131   /* Generic tasks are non-timeout tasks and they apply to all file 
132      descriptors, except to those that have explicitly registered a 
133      non-timeout task. These tasks are there to make it simpler and faster 
134      to execute common code that applies to all connections. These are,
135      for example, receiving packets from network and sending packets to
136      network. It doesn't make much sense to register a task that receives
137      a packet from network to every connection when you can have one task
138      that applies to all connections. This is what generic tasks are for.
139      Generic tasks are not bound to any specific file descriptor, however,
140      the correct file descriptor must be passed as argument to task
141      registering function. */
142   SILC_TASK_GENERIC,
143 } SilcTaskType;
144 /***/
145
146 /****d* silcutil/SilcScheduleAPI/SilcTaskEvent
147  *
148  * NAME
149  * 
150  *    typedef enum { ... } SilcTaskEvent;
151  *
152  * DESCRIPTION
153  *
154  *    SILC Task event types.  The event type indicates the occurred
155  *    event of the task.  This type will be given as argument to the
156  *    SilcTaskCallback function to indicate the event for the caller.
157  *    The SILC_TASK_READ and SILC_TASK_WRITE may be set by the caller
158  *    of the silc_schedule_set_listen_fd, if the caller needs to control
159  *    the events for the task. The SILC_TASK_EXPIRE is set always only
160  *    by the scheduler when timeout expires for timeout task.
161  *
162  * SOURCE
163  */
164 typedef enum {
165   SILC_TASK_READ      = 0x0001,          /* Reading */
166   SILC_TASK_WRITE     = 0x0002,          /* Writing */
167   SILC_TASK_EXPIRE    = 0x0004,          /* Timeout */
168 } SilcTaskEvent;
169 /***/
170
171 /****d* silcutil/SilcScheduleAPI/SilcTaskPriority
172  *
173  * NAME
174  * 
175  *    typedef enum { ... } SilcTaskPriority;
176  *
177  * DESCRIPTION
178  *
179  *    Task priorities. Tasks may be registered with different priorities.
180  *    This type defines the different task priorities. The priorities
181  *    behaves same for all type of tasks, fd tasks, timeout tasks and
182  *    generic tasks.
183  *
184  * SOURCE
185  */
186 typedef enum {
187   /* Lowest priority. The task is scheduled to run after its timeout
188      has expired only and only when every other task with higher priority 
189      has already been run. For non-timeout tasks this priority behaves
190      same way. Life is not fair for tasks with this priority. */
191   SILC_TASK_PRI_LOW,
192
193   /* Normal priority that is used mostly in SILC. This is priority that
194      should always be used unless you specificly need some other priority.
195      The scheduler will run this task as soon as its timeout has expired.
196      For non-timeout tasks this priority behaves same way. Tasks are run 
197      in FIFO (First-In-First-Out) order. */
198   SILC_TASK_PRI_NORMAL,
199 } SilcTaskPriority;
200 /***/
201
202 /****f* silcutil/SilcScheduleAPI/SilcTaskCallback
203  *
204  * SYNOPSIS
205  *
206  *    typedef void (*SilcTaskCallback)(SilcSchedule schedule, 
207  *                                     SilcTaskEvent type, SilcUInt32 fd, 
208  *                                     void *context);
209  *
210  * DESCRIPTION
211  *
212  *    The task callback function.  This function will be called by the
213  *    scheduler when some event of the task is performed.  For example,
214  *    when data is available from the connection this will be called.
215  *
216  *    The `schedule' is the scheduler context, the `type' is the indicated
217  *    event, the `fd' is the file descriptor of the task and the `context'
218  *    is a caller specified context. If multiple events occurred this
219  *    callback is called separately for all events.
220  *
221  *    To specify task callback function in the application using the
222  *    SILC_TASK_CALLBACK and SILC_TASK_CALLBACK_GLOBAL macros is
223  *    recommended.
224  *
225  ***/
226 typedef void (*SilcTaskCallback)(SilcSchedule schedule, SilcTaskEvent type,
227                                  SilcUInt32 fd, void *context);
228
229 /* Macros */
230
231 /****d* silcutil/SilcScheduleAPI/SILC_ALL_TASKS
232  *
233  * NAME
234  * 
235  *    #define SILC_ALL_TASKS ...
236  *
237  * DESCRIPTION
238  *
239  *    Marks for all tasks in the scheduler. This can be passed to 
240  *    silc_schedule_task_del function to delete all tasks at once.
241  *
242  * SOURCE
243  */
244 #define SILC_ALL_TASKS ((SilcTask)1)
245 /***/
246
247 /****d* silcutil/SilcScheduleAPI/SILC_TASK_CALLBACK
248  *
249  * NAME
250  * 
251  *    #define SILC_TASK_CALLBACK ...
252  *
253  * DESCRIPTION
254  *
255  *    Generic macro to define task callback functions. This defines a
256  *    static function with name `func' as a task callback function.
257  *
258  * SOURCE
259  */
260 #define SILC_TASK_CALLBACK(func)                                \
261 static void func(SilcSchedule schedule, SilcTaskEvent type,     \
262                  SilcUInt32 fd, void *context)
263 /***/
264
265 /****d* silcutil/SilcScheduleAPI/SILC_TASK_CALLBACK_GLOBAL
266  *
267  * NAME
268  * 
269  *    #define SILC_TASK_CALLBACK_GLOBAL ...
270  *
271  * DESCRIPTION
272  *
273  *    Generic macro to define task callback functions. This defines a
274  *    function with name `func' as a task callback function.  This
275  *    differs from SILC_TASK_CALLBACK in that the defined function is
276  *    not static.
277  *
278  * SOURCE
279  */
280 #define SILC_TASK_CALLBACK_GLOBAL(func)                 \
281 void func(SilcSchedule schedule, SilcTaskEvent type,    \
282           SilcUInt32 fd, void *context)
283 /***/
284
285 /* Prototypes */
286
287 /****f* silcutil/SilcScheduleAPI/silc_schedule_init
288  *
289  * SYNOPSIS
290  *
291  *    SilcSchedule silc_schedule_init(int max_tasks);
292  *
293  * DESCRIPTION
294  *
295  *    Initializes the scheduler. This returns the scheduler context that
296  *    is given as argument usually to all silc_schedule_* functions.
297  *    The `max_tasks' indicates the number of maximum tasks that the
298  *    scheduler can handle.
299  *
300  ***/
301 SilcSchedule silc_schedule_init(int max_tasks);
302
303 /****f* silcutil/SilcScheduleAPI/silc_schedule_uninit
304  *
305  * SYNOPSIS
306  *
307  *    bool silc_schedule_uninit(SilcSchedule schedule);
308  *
309  * DESCRIPTION
310  *
311  *    Uninitializes the scheduler. This is called when the program is ready
312  *    to end. This removes all tasks from the scheduler. Returns FALSE if the
313  *    scheduler could not be uninitialized. This happens when the scheduler
314  *    is still valid and silc_schedule_stop has not been called.
315  *
316  ***/
317 bool silc_schedule_uninit(SilcSchedule schedule);
318
319 /****f* silcutil/SilcScheduleAPI/silc_schedule_reinit
320  *
321  * SYNOPSIS
322  *
323  *    SilcSchedule silc_schedule_reinit(int max_tasks);
324  *
325  * DESCRIPTION
326  *
327  *    This function can be called to enlarge the task handling capabilities
328  *    of the scheduler indicated by `schedule'.  The `max_tasks' must be
329  *    larger than what was set in silc_schedule_init function.  This function
330  *    returns FALSE if it cannot reinit the scheduler.  This function does
331  *    not do anything else except ready the scheduler to handle `max_tasks'
332  *    number of tasks after this function returns.  It is safe to call this
333  *    function at any time, and it is guaranteed that existing tasks remain
334  *    as they are in the scheduler.
335  *
336  ***/
337 bool silc_schedule_reinit(SilcSchedule schedule, int max_tasks);
338
339 /****f* silcutil/SilcScheduleAPI/silc_schedule_stop
340  *
341  * SYNOPSIS
342  *
343  *    void silc_schedule_stop(SilcSchedule schedule);
344  *
345  * DESCRIPTION
346  *
347  *    Stops the scheduler even if it is not supposed to be stopped yet. 
348  *    After calling this, one must call silc_schedule_uninit (after the 
349  *    silc_schedule has returned).  After this is called it is guaranteed
350  *    that next time the scheduler enters the main loop it will be stopped.
351  *    However, untill it enters the main loop it will not detect that
352  *    it is stopped for example if this is called from another thread.
353  *
354  ***/
355 void silc_schedule_stop(SilcSchedule schedule);
356
357 /****f* silcutil/SilcScheduleAPI/silc_schedule
358  *
359  * SYNOPSIS
360  *
361  *    void silc_schedule(SilcSchedule schedule);
362  *
363  * DESCRIPTION
364  *
365  *    The SILC scheduler. This is actually the main routine in SILC programs.
366  *    When this returns the program is to be ended. Before this function can
367  *    be called, one must call silc_schedule_init function.
368  *
369  ***/
370 void silc_schedule(SilcSchedule schedule);
371
372 /****f* silcutil/SilcScheduleAPI/silc_schedule_one
373  *
374  * SYNOPSIS
375  *
376  *    bool silc_schedule_one(SilcSchedule schedule, int block);
377  *
378  * DESCRIPTION
379  *
380  *    Same as the silc_schedule but runs the scheduler only one round
381  *    and then returns.  This function is handy when the SILC scheduler
382  *    is used inside some other external scheduler, for example.  If
383  *    the `timeout_usecs' is non-negative a timeout will be added to the
384  *    scheduler.  The function will not return in this timeout unless
385  *    some other event occurs.
386  *
387  ***/
388 bool silc_schedule_one(SilcSchedule schedule, int timeout_usecs);
389
390 /****f* silcutil/SilcScheduleAPI/silc_schedule_wakeup
391  *
392  * SYNOPSIS
393  *
394  *    void silc_schedule_wakeup(SilcSchedule schedule);
395  *
396  * DESCRIPTION
397  *
398  *    Wakes up the scheduler. This is used only in multi-threaded
399  *    environments where threads may add new tasks or remove old tasks
400  *    from the scheduler. This is called to wake up the scheduler in the
401  *    main thread so that it detects the changes in the scheduler.
402  *    If threads support is not compiled in this function has no effect.
403  *    Implementation of this function may be platform specific.
404  *
405  ***/
406 void silc_schedule_wakeup(SilcSchedule schedule);
407
408 /****f* silcutil/SilcScheduleAPI/silc_schedule_task_add
409  *
410  * SYNOPSIS
411  *
412  *    SilcTask silc_schedule_task_add(SilcSchedule schedule, SilcUInt32 fd,
413  *                                    SilcTaskCallback callback, 
414  *                                    void *context, 
415  *                                    long seconds, long useconds, 
416  *                                    SilcTaskType type, 
417  *                                    SilcTaskPriority priority);
418  *
419  * DESCRIPTION
420  *
421  *    Registers a new task to the scheduler. This same function is used
422  *    to register all types of tasks. The `type' argument tells what type
423  *    of the task is. Note that when registering non-timeout tasks one
424  *    should also pass 0 as timeout, as the timeout will be ignored anyway. 
425  *    Also, note, that one cannot register timeout task with 0 timeout.
426  *    There cannot be zero timeouts, passing zero means no timeout is used
427  *    for the task and SILC_TASK_FD is used as default task type in
428  *    this case.
429  *
430  *    The `schedule' is the scheduler context. The `fd' is the file
431  *    descriptor of the task. On WIN32 systems the `fd' is not actual
432  *    file descriptor but some WIN32 event handle. On WIN32 system the `fd'
433  *    may be a socket created by the SILC Net API routines, WSAEVENT object
434  *    created by Winsock2 network routines or arbitrary WIN32 HANDLE object.
435  *    On Unix systems the `fd' is always the real file descriptor.
436  *
437  *    The `callback' is the task callback that will be called when some
438  *    event occurs for this task. The `context' is sent as argument to
439  *    the task `callback' function. For timeout tasks the callback is
440  *    called after the specified timeout has elapsed.
441  *
442  *    If the `type' is SILC_TASK_TIMEOUT then `seconds' and `useconds'
443  *    may be non-zero.  Otherwise they should be zero. The `priority'
444  *    indicates the priority of the task.
445  *
446  *    It is always safe to call this function in any place. New tasks
447  *    may be added also in task callbacks, and in multi-threaded environment
448  *    in other threads as well.
449  *   
450  ***/
451 SilcTask silc_schedule_task_add(SilcSchedule schedule, SilcUInt32 fd,
452                                 SilcTaskCallback callback, void *context, 
453                                 long seconds, long useconds, 
454                                 SilcTaskType type, 
455                                 SilcTaskPriority priority);
456
457 /****f* silcutil/SilcScheduleAPI/silc_schedule_task_del
458  *
459  * SYNOPSIS
460  *
461  *    void silc_schedule_task_del(SilcSchedule schedule, SilcTask task);
462  *
463  * DESCRIPTION
464  *
465  *    Deletes the `task' from the scheduler indicated by the `schedule'.
466  *    After deleting the task it is guaranteed that the task callback
467  *    will not be called. If the `task' is SILC_ALL_TASKS then all
468  *    tasks is removed from the scheduler.
469  *
470  *    It is safe to call this function in any place. Tasks may be removed
471  *    in task callbacks (including in the task's own task callback) and
472  *    in multi-threaded environment in other threads as well.
473  *
474  ***/
475 void silc_schedule_task_del(SilcSchedule schedule, SilcTask task);
476
477 /****f* silcutil/SilcScheduleAPI/silc_schedule_task_del_by_fd
478  *
479  * SYNOPSIS
480  *
481  *    void silc_schedule_task_del_by_fd(SilcSchedule schedule, SilcUInt32 fd);
482  *
483  * DESCRIPTION
484  *
485  *    Deletes a task from the scheduler by the specified `fd'.
486  *
487  *    It is safe to call this function in any place. Tasks may be removed
488  *    in task callbacks (including in the task's own task callback) and
489  *    in multi-threaded environment in other threads as well.
490  *
491  *    Note that generic tasks cannot be deleted using this function
492  *    since generic tasks does not match any specific fd.
493  *
494  ***/
495 void silc_schedule_task_del_by_fd(SilcSchedule schedule, SilcUInt32 fd);
496
497 /****f* silcutil/SilcScheduleAPI/silc_schedule_task_del_by_callback
498  *
499  * SYNOPSIS
500  *
501  *    void silc_schedule_task_del_by_callback(SilcSchedule schedule,
502  *                                            SilcTaskCallback callback);
503  *
504  * DESCRIPTION
505  *
506  *    Deletes a task from the scheduler by the specified `callback' task
507  *    callback function.
508  *
509  *    It is safe to call this function in any place. Tasks may be removed
510  *    in task callbacks (including in the task's own task callback) and
511  *    in multi-threaded environment in other threads as well.
512  *
513  ***/
514 void silc_schedule_task_del_by_callback(SilcSchedule schedule,
515                                         SilcTaskCallback callback);
516
517 /****f* silcutil/SilcScheduleAPI/silc_schedule_task_del_by_context
518  *
519  * SYNOPSIS
520  *
521  *    void silc_schedule_task_del_by_context(SilcSchedule schedule, 
522  *                                           void *context);
523  *
524  * DESCRIPTION
525  *
526  *    Deletes a task from the scheduler by the specified `context'.
527  *
528  *    It is safe to call this function in any place. Tasks may be removed
529  *    in task callbacks (including in the task's own task callback) and
530  *    in multi-threaded environment in other threads as well.
531  *
532  ***/
533 void silc_schedule_task_del_by_context(SilcSchedule schedule, void *context);
534
535 /****f* silcutil/SilcScheduleAPI/silc_schedule_set_listen_fd
536  *
537  * SYNOPSIS
538  *
539  *    void silc_schedule_set_listen_fd(SilcSchedule schedule, SilcUInt32 fd,
540  *                                     SilcTaskEvent mask);
541  *
542  * DESCRIPTION
543  *
544  *    Sets a file descriptor `fd' to be listened by the scheduler for
545  *    `mask' events.  To tell scheduler not to listen anymore for this
546  *    file descriptor call the silc_schedule_unset_listen_fd function.
547  *    When new task is created with silc_schedule_task_add the event
548  *    for the task's fd is initially set to SILC_TASK_READ. If you need
549  *    to control the task's fd's events you must call this function
550  *    whenever you need to change the events. This can be called multiple
551  *    times to change the events.
552  *
553  ***/
554 void silc_schedule_set_listen_fd(SilcSchedule schedule, SilcUInt32 fd,
555                                  SilcTaskEvent mask);
556
557 /****f* silcutil/SilcScheduleAPI/silc_schedule_unset_listen_fd
558  *
559  * SYNOPSIS
560  *
561  *    void silc_schedule_unset_listen_fd(SilcSchedule schedule, SilcUInt32 fd);
562  *
563  * DESCRIPTION
564  *
565  *    Tells the scheduler not to listen anymore for the specified
566  *    file descriptor `fd'. No events will be detected for the `fd'
567  *    after calling this function.
568  *
569  ***/
570 void silc_schedule_unset_listen_fd(SilcSchedule schedule, SilcUInt32 fd);
571
572 #endif