Porting Toolkit to Symbian. It should work while some sporadic
[silc.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 /* Macros */
162
163 /****d* silcutil/SilcScheduleAPI/SILC_ALL_TASKS
164  *
165  * NAME
166  *
167  *    #define SILC_ALL_TASKS ...
168  *
169  * DESCRIPTION
170  *
171  *    Marks for all tasks in the scheduler. This can be passed to
172  *    silc_schedule_task_del function to delete all tasks at once.
173  *
174  * SOURCE
175  */
176 #define SILC_ALL_TASKS ((SilcTask)1)
177 /***/
178
179 /****d* silcutil/SilcScheduleAPI/SILC_TASK_CALLBACK
180  *
181  * NAME
182  *
183  *    #define SILC_TASK_CALLBACK ...
184  *
185  * DESCRIPTION
186  *
187  *    Generic macro to define task callback functions. This defines a
188  *    static function with name `func' as a task callback function.
189  *
190  * SOURCE
191  */
192 #define SILC_TASK_CALLBACK(func)                                        \
193 void func(SilcSchedule schedule, void *app_context, SilcTaskEvent type, \
194           SilcUInt32 fd, void *context)
195 /***/
196
197 /* Prototypes */
198
199 #include "silcschedule_i.h"
200
201 /****f* silcutil/SilcScheduleAPI/silc_schedule_init
202  *
203  * SYNOPSIS
204  *
205  *    SilcSchedule silc_schedule_init(int max_tasks, void *app_context);
206  *
207  * DESCRIPTION
208  *
209  *    Initializes the scheduler. This returns the scheduler context that
210  *    is given as argument usually to all silc_schedule_* functions.
211  *    The `app_context' is application specific context that is delivered
212  *    to all task callbacks. The caller must free that context.  The
213  *    'app_context' can be for example the application itself.
214  *
215  *    The `max_tasks' is the maximum number of file descriptor and socket
216  *    tasks in the scheduler.  Set value to 0 to use default.  Operating
217  *    system will enforce the final limit.  On some operating systems the
218  *    limit can be significantly increased when this function is called in
219  *    priviliged mode (as super user).
220  *
221  ***/
222 SilcSchedule silc_schedule_init(int max_tasks, void *app_context);
223
224 /****f* silcutil/SilcScheduleAPI/silc_schedule_uninit
225  *
226  * SYNOPSIS
227  *
228  *    SilcBool silc_schedule_uninit(SilcSchedule schedule);
229  *
230  * DESCRIPTION
231  *
232  *    Uninitializes the scheduler. This is called when the program is ready
233  *    to end. This removes all tasks from the scheduler. Returns FALSE if the
234  *    scheduler could not be uninitialized. This happens when the scheduler
235  *    is still valid and silc_schedule_stop has not been called.
236  *
237  ***/
238 SilcBool silc_schedule_uninit(SilcSchedule schedule);
239
240 /****f* silcutil/SilcScheduleAPI/silc_schedule_stop
241  *
242  * SYNOPSIS
243  *
244  *    void silc_schedule_stop(SilcSchedule schedule);
245  *
246  * DESCRIPTION
247  *
248  *    Stops the scheduler even if it is not supposed to be stopped yet.
249  *    After calling this, one must call silc_schedule_uninit (after the
250  *    silc_schedule has returned).  After this is called it is guaranteed
251  *    that next time the scheduler enters the main loop it will be stopped.
252  *    However, untill it enters the main loop it will not detect that
253  *    it is stopped for example if this is called from another thread.
254  *
255  ***/
256 void silc_schedule_stop(SilcSchedule schedule);
257
258 /****f* silcutil/SilcScheduleAPI/silc_schedule
259  *
260  * SYNOPSIS
261  *
262  *    void silc_schedule(SilcSchedule schedule);
263  *
264  * DESCRIPTION
265  *
266  *    The SILC scheduler.  The program will run inside this function.
267  *    When this returns the program is to be ended.  Before this function
268  *    can be called, one must call silc_schedule_init function.
269  *
270  * NOTES
271  *
272  *    On Windows this will block the calling thread but will continue
273  *    to dispatch window messages, and thus can be used as the main loop
274  *    of the program.
275  *
276  *    On Symbian this will block the calling thread.  The Symbian Active
277  *    Scheduler must be running before calling this function.
278  *
279  ***/
280 void silc_schedule(SilcSchedule schedule);
281
282 /****f* silcutil/SilcScheduleAPI/silc_schedule_one
283  *
284  * SYNOPSIS
285  *
286  *    SilcBool silc_schedule_one(SilcSchedule schedule, int timeout_usecs);
287  *
288  * DESCRIPTION
289  *
290  *    Same as the silc_schedule but runs the scheduler only one round
291  *    and then returns.  This function is handy when the SILC scheduler
292  *    is used inside some other external scheduler, for example.  If
293  *    the `timeout_usecs' is non-negative a timeout will be added to the
294  *    scheduler.  The function will not return in this timeout unless
295  *    some other event occurs.
296  *
297  *    Typically this would be called from a timeout or idle task
298  *    periodically (typically from 5-50 ms) to schedule SILC tasks.  In
299  *    this case the `timeout_usecs' is usually 0 to make the function
300  *    return immediately.
301  *
302  ***/
303 SilcBool silc_schedule_one(SilcSchedule schedule, int timeout_usecs);
304
305 /****f* silcutil/SilcScheduleAPI/silc_schedule_wakeup
306  *
307  * SYNOPSIS
308  *
309  *    void silc_schedule_wakeup(SilcSchedule schedule);
310  *
311  * DESCRIPTION
312  *
313  *    Wakes up the scheduler. This is may be used in multi-threaded
314  *    environments where threads may add new tasks or remove old tasks
315  *    from the scheduler. This is called to wake up the scheduler in the
316  *    main thread so that it detects the changes in the scheduler.
317  *    If threads support is not compiled in this function has no effect.
318  *
319  ***/
320 void silc_schedule_wakeup(SilcSchedule schedule);
321
322 /****f* silcutil/SilcScheduleAPI/silc_schedule_get_context
323  *
324  * SYNOPSIS
325  *
326  *    void *silc_schedule_get_context(SilcSchedule schedule);
327  *
328  * DESCRIPTION
329  *
330  *    Returns the application specific context that was saved into the
331  *    scheduler in silc_schedule_init function.  The context is also
332  *    returned to application in the SilcTaskCallback, but this function
333  *    may be used to get it as well if needed.
334  *
335  ***/
336 void *silc_schedule_get_context(SilcSchedule schedule);
337
338 /****f* silcutil/SilcScheduleAPI/silc_schedule_task_add_fd
339  *
340  * SYNOPSIS
341  *
342  *    SilcTask
343  *    silc_schedule_task_add_fd(SilcSchedule schedule, SilcUInt32 fd,
344  *                              SilcTaskCallback callback, void *context);
345  *
346  * DESCRIPTION
347  *
348  *    Add file descriptor task to scheduler.  The `fd' may be either real
349  *    file descriptor, socket or on some platforms an opaque file descriptor
350  *    handle.  To receive events for the file descriptor set the correct
351  *    request events with silc_schedule_set_listen_fd function.
352  *
353  *    The task will be initially set for SILC_TASK_READ events.  Setting that
354  *    event immediately after this call returns is not necessary.
355  *
356  *    This returns the new task or NULL on error.  If a task with `fd' has
357  *    already been added this will return the existing task pointer.
358  *
359  ***/
360 #define silc_schedule_task_add_fd(schedule, fd, callback, context)      \
361   silc_schedule_task_add(schedule, fd, callback, context, 0, 0, SILC_TASK_FD)
362
363 /****f* silcutil/SilcScheduleAPI/silc_schedule_task_add_timeout
364  *
365  * SYNOPSIS
366  *
367  *    SilcTask
368  *    silc_schedule_task_add_timeout(SilcSchedule schedule,
369  *                                   SilcTaskCallback callback, void *context,
370  *                                   long seconds, long useconds);
371  *
372  * DESCRIPTION
373  *
374  *    Add timeout task to scheduler.  The `callback' will be called once
375  *    the specified timeout has elapsed.  The task will be removed from the
376  *    scheduler automatically once the task expires.  The event returned
377  *    to the `callback' is SILC_TASK_EXPIRE.  The task added with zero (0)
378  *    timeout will be executed immediately next time tasks are scheduled.
379  *
380  ***/
381 #define silc_schedule_task_add_timeout(schedule, callback, context, s, u) \
382   silc_schedule_task_add(schedule, 0, callback, context, s, u,          \
383                          SILC_TASK_TIMEOUT)
384
385 /****f* silcutil/SilcScheduleAPI/silc_schedule_task_add_signal
386  *
387  * SYNOPSIS
388  *
389  *    SilcTask
390  *    silc_schedule_task_add_signal(SilcSchedule schedule, int signal,
391  *                                  SilcTaskCallback callback, void *context);
392  *
393  * DESCRIPTION
394  *
395  *    Add platform specific process signal handler to scheduler.  On Unix
396  *    systems the `signal' is one of the signal specified in signal(7).  On
397  *    other platforms this function may not be available at all, and has no
398  *    effect when called.  The event delivered to the `callback' is
399  *    SILC_TASK_INTERRUPT.
400  *
401  * NOTES
402  *
403  *    One signal may be registered only one callback.  Adding second callback
404  *    for signal that already has one will fail.
405  *
406  *    This function always returns NULL.  To remove signal from scheduler by
407  *    the signal call silc_schedule_task_del_by_fd.
408  *
409  ***/
410 #define silc_schedule_task_add_signal(schedule, sig, callback, context) \
411   silc_schedule_task_add(schedule, sig, callback, context, 0, 0,        \
412                          SILC_TASK_SIGNAL)
413
414 /****f* silcutil/SilcScheduleAPI/silc_schedule_task_del
415  *
416  * SYNOPSIS
417  *
418  *    SilcBool silc_schedule_task_del(SilcSchedule schedule, SilcTask task);
419  *
420  * DESCRIPTION
421  *
422  *    Deletes the `task' from the scheduler indicated by the `schedule'.
423  *    After deleting the task it is guaranteed that the task callback
424  *    will not be called. If the `task' is SILC_ALL_TASKS then all
425  *    tasks is removed from the scheduler.  Returns always TRUE.
426  *
427  *    It is safe to call this function in any place. Tasks may be removed
428  *    in task callbacks (including in the task's own task callback) and
429  *    in multi-threaded environment in other threads as well.
430  *
431  ***/
432 SilcBool silc_schedule_task_del(SilcSchedule schedule, SilcTask task);
433
434 /****f* silcutil/SilcScheduleAPI/silc_schedule_task_del_by_fd
435  *
436  * SYNOPSIS
437  *
438  *    SilcBool silc_schedule_task_del_by_fd(SilcSchedule schedule,
439  *                                          SilcUInt32 fd);
440  *
441  * DESCRIPTION
442  *
443  *    Deletes a task from the scheduler by the specified `fd'.  Returns
444  *    FALSE if such fd task does not exist.
445  *
446  *    It is safe to call this function in any place. Tasks may be removed
447  *    in task callbacks (including in the task's own task callback) and
448  *    in multi-threaded environment in other threads as well.
449  *
450  ***/
451 SilcBool silc_schedule_task_del_by_fd(SilcSchedule schedule, SilcUInt32 fd);
452
453 /****f* silcutil/SilcScheduleAPI/silc_schedule_task_del_by_callback
454  *
455  * SYNOPSIS
456  *
457  *    SilcBool silc_schedule_task_del_by_callback(SilcSchedule schedule,
458  *                                                SilcTaskCallback callback);
459  *
460  * DESCRIPTION
461  *
462  *    Deletes a task from the scheduler by the specified `callback' task
463  *    callback function.  Returns FALSE if such task with such callback
464  *    does not exist.
465  *
466  *    It is safe to call this function in any place. Tasks may be removed
467  *    in task callbacks (including in the task's own task callback) and
468  *    in multi-threaded environment in other threads as well.
469  *
470  ***/
471 SilcBool silc_schedule_task_del_by_callback(SilcSchedule schedule,
472                                             SilcTaskCallback callback);
473
474 /****f* silcutil/SilcScheduleAPI/silc_schedule_task_del_by_context
475  *
476  * SYNOPSIS
477  *
478  *    SilcBool silc_schedule_task_del_by_context(SilcSchedule schedule,
479  *                                               void *context);
480  *
481  * DESCRIPTION
482  *
483  *    Deletes a task from the scheduler by the specified `context'.  Returns
484  *    FALSE if such task with such context does not exist.
485  *
486  *    It is safe to call this function in any place. Tasks may be removed
487  *    in task callbacks (including in the task's own task callback) and
488  *    in multi-threaded environment in other threads as well.
489  *
490  ***/
491 SilcBool silc_schedule_task_del_by_context(SilcSchedule schedule,
492                                            void *context);
493
494 /****f* silcutil/SilcScheduleAPI/silc_schedule_task_del_by_all
495  *
496  * SYNOPSIS
497  *
498  *    SilcBool silc_schedule_task_del_by_all(SilcSchedule schedule, int fd,
499  *                                           SilcTaskCallback callback,
500  *                                           void *context);
501  *
502  * DESCRIPTION
503  *
504  *    Deletes a task from the scheduler by the specified `fd', `callback'
505  *    and `context'.  Returns FALSE if such task does not exist.
506  *
507  *    It is safe to call this function in any place. Tasks may be removed
508  *    in task callbacks (including in the task's own task callback) and
509  *    in multi-threaded environment in other threads as well.
510  *
511  ***/
512 SilcBool silc_schedule_task_del_by_all(SilcSchedule schedule, int fd,
513                                        SilcTaskCallback callback,
514                                        void *context);
515
516 /****f* silcutil/SilcScheduleAPI/silc_schedule_set_listen_fd
517  *
518  * SYNOPSIS
519  *
520  *    SilcBool silc_schedule_set_listen_fd(SilcSchedule schedule,
521  *                                         SilcUInt32 fd,
522  *                                         SilcTaskEvent mask,
523  *                                         SilcBool send_events);
524  *
525  * DESCRIPTION
526  *
527  *    Sets a file descriptor `fd' to be listened by the scheduler for
528  *    `mask' events.  To tell scheduler not to listen anymore for this
529  *    file descriptor call the silc_schedule_unset_listen_fd function.
530  *    When new task is created with silc_schedule_task_add the event
531  *    for the task's fd is initially set to SILC_TASK_READ. If you need
532  *    to control the task's fd's events you must call this function
533  *    whenever you need to change the events. This can be called multiple
534  *    times to change the events.
535  *
536  *    If the `send_events' is TRUE then this function sends the events
537  *    in `mask' to the application.  If FALSE then they are sent only
538  *    after the event occurs in reality.  In normal cases the `send_events'
539  *    is set to FALSE.
540  *
541  *    Returns FALSE if the operation could not performed and TRUE if it
542  *    was a success.
543  *
544  ***/
545 SilcBool silc_schedule_set_listen_fd(SilcSchedule schedule, SilcUInt32 fd,
546                                      SilcTaskEvent mask, SilcBool send_events);
547
548 /****f* silcutil/SilcScheduleAPI/silc_schedule_get_fd_events
549  *
550  * SYNOPSIS
551  *
552  *    SilcTaskEvent silc_schedule_get_fd_events(SilcSchedule schedule,
553  *                                              SilcUInt32 fd);
554  *
555  * DESCRIPTION
556  *
557  *    Returns the file descriptor `fd' current requested events mask,
558  *    or 0 on error.
559  *
560  ***/
561 SilcTaskEvent silc_schedule_get_fd_events(SilcSchedule schedule,
562                                           SilcUInt32 fd);
563
564 /****f* silcutil/SilcScheduleAPI/silc_schedule_unset_listen_fd
565  *
566  * SYNOPSIS
567  *
568  *    void silc_schedule_unset_listen_fd(SilcSchedule schedule, SilcUInt32 fd);
569  *
570  * DESCRIPTION
571  *
572  *    Tells the scheduler not to listen anymore for the specified
573  *    file descriptor `fd'. No events will be detected for the `fd'
574  *    after calling this function.
575  *
576  ***/
577 void silc_schedule_unset_listen_fd(SilcSchedule schedule, SilcUInt32 fd);
578
579 #endif