6c8d462d56eaf2948a51250f8fb0801e73f35a56
[crypto.git] / lib / silcutil / silcschedule_i.h
1 /*
2
3   silcschedule_i.h.
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2001 - 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 #ifndef SILCSCHEDULE_I_H
21 #define SILCSCHEDULE_I_H
22
23 #ifndef SILCSCHEDULE_H
24 #error "Do not include this header directly"
25 #endif
26
27 #include "silchashtable.h"
28 #include "silclist.h"
29
30 /* Task types */
31 typedef enum {
32   /* File descriptor task that performs some event over file descriptors.
33      These tasks are for example network connections. */
34   SILC_TASK_FD           = 0,
35
36   /* Timeout tasks are tasks that are executed after the specified
37      time has elapsed. After the task is executed the task is removed
38      automatically from the scheduler. It is safe to re-register the
39      task in task callback. It is also safe to unregister a task in
40      the task callback. */
41   SILC_TASK_TIMEOUT,
42
43   /* Platform specific process signal task.  On Unix systems this is one of
44      the signals described in signal(7).  On other platforms this may not
45      be available at all.  Only one callback per signal may be added. */
46   SILC_TASK_SIGNAL
47 } SilcTaskType;
48
49 /* Task header */
50 struct SilcTaskStruct {
51   struct SilcTaskStruct *next;
52   SilcTaskCallback callback;
53   void *context;
54   unsigned int type    : 1;     /* 0 = fd, 1 = timeout */
55   unsigned int valid   : 1;     /* Set if task is valid */
56 };
57
58 /* Timeout task */
59 typedef struct SilcTaskTimeoutStruct {
60   struct SilcTaskStruct header;
61   struct timeval timeout;
62 } *SilcTaskTimeout;
63
64 /* Fd task */
65 typedef struct SilcTaskFdStruct {
66   struct SilcTaskStruct header;
67   unsigned int scheduled  : 1;
68   unsigned int events     : 14;
69   unsigned int revents    : 15;
70   SilcUInt32 fd;
71 } *SilcTaskFd;
72
73 /* Scheduler context */
74 struct SilcScheduleStruct {
75   void *internal;
76   void *app_context;               /* Application specific context */
77   SilcTaskNotifyCb notify;         /* Notify callback */
78   void *notify_context;            /* Notify context */
79   SilcStack stack;                 /* Stack */
80   SilcHashTable fd_queue;          /* FD task queue */
81   SilcList fd_dispatch;            /* Dispatched FDs */
82   SilcList timeout_queue;          /* Timeout queue */
83   SilcList free_tasks;             /* Timeout task freelist */
84   SilcMutex lock;                  /* Scheduler lock */
85   struct timeval timeout;          /* Current timeout */
86   unsigned int max_tasks     : 29; /* Max FD tasks */
87   unsigned int has_timeout   : 1;  /* Set if timeout is set */
88   unsigned int valid         : 1;  /* Set if scheduler is valid */
89   unsigned int signal_tasks  : 1;  /* Set if to dispatch signals */
90 };
91
92 /* Locks. These also blocks signals that we care about and thus guarantee
93    that while we are in scheduler no signals can happen.  This way we can
94    synchronise signals with SILC Scheduler. */
95 #define SILC_SCHEDULE_LOCK(schedule)                            \
96 do {                                                            \
97   silc_mutex_lock(schedule->lock);                              \
98   schedule_ops.signals_block(schedule, schedule->internal);     \
99 } while (0)
100 #define SILC_SCHEDULE_UNLOCK(schedule)                          \
101 do {                                                            \
102   schedule_ops.signals_unblock(schedule, schedule->internal);   \
103   silc_mutex_unlock(schedule->lock);                            \
104 } while (0)
105
106 /* Platform specific scheduler operations */
107 typedef struct {
108   /* Initializes the platform specific scheduler.  This for example initializes
109      the wakeup mechanism of the scheduler.  In multi-threaded environment
110      the scheduler needs to be wakenup when tasks are added or removed from
111      the task queues.  Returns context to the platform specific scheduler.
112      If this returns NULL the scheduler initialization will fail.  Do not
113      add FD tasks inside function.  Timeout tasks can be added. */
114   void *(*init)(SilcSchedule schedule, void *app_context);
115
116   /* Uninitializes the platform specific scheduler context. */
117   void (*uninit)(SilcSchedule schedule, void *context);
118
119   /* System specific waiter. This must fill the schedule->fd_dispatch queue
120      with valid tasks that has something to dispatch, when this returns. */
121   int (*schedule)(SilcSchedule schedule, void *context);
122
123   /* Schedule `task' with events `event_mask'. Zero `event_mask'
124      unschedules the task. */
125   SilcBool (*schedule_fd)(SilcSchedule schedule, void *context,
126                           SilcTaskFd task, SilcTaskEvent event_mask);
127
128   /* Wakes up the scheduler. This is platform specific routine */
129   void (*wakeup)(SilcSchedule schedule, void *context);
130
131   /* Register signal */
132   void (*signal_register)(SilcSchedule schedule, void *context,
133                           SilcUInt32 signal, SilcTaskCallback callback,
134                           void *callback_context);
135
136   /* Unregister signal */
137   void (*signal_unregister)(SilcSchedule schedule, void *context,
138                             SilcUInt32 signal);
139
140   /* Call all signals */
141   void (*signals_call)(SilcSchedule schedule, void *context);
142
143   /* Block registered signals in scheduler. */
144   void (*signals_block)(SilcSchedule schedule, void *context);
145
146   /* Unblock registered signals in schedule. */
147   void (*signals_unblock)(SilcSchedule schedule, void *context);
148 } SilcScheduleOps;
149
150 /* The generic function to add any type of task to the scheduler.  This
151    used to be exported as is to application, but now they should use the
152    macro wrappers defined in silcschedule.h.  For Fd task the timeout must
153    be zero, for timeout task the timeout must not be zero, for signal task
154    the fd argument is the signal. */
155 SilcTask silc_schedule_task_add(SilcSchedule schedule, SilcUInt32 fd,
156                                 SilcTaskCallback callback, void *context,
157                                 long seconds, long useconds,
158                                 SilcTaskType type);
159
160 #ifdef SILC_DIST_INPLACE
161 /* Print scheduler statistics to stdout. */
162 void silc_schedule_stats(SilcSchedule schedule);
163 #endif /* SILC_DIST_INPLACE */
164
165 #endif /* SILCSCHEDULE_I_H */