Added freelist for timeout tasks for fast task addition.
[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 - 2006 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 header */
31 struct SilcTaskStruct {
32   SilcTaskCallback callback;
33   void *context;
34   unsigned int type    : 1;     /* 0 = fd, 1 = timeout */
35   unsigned int valid   : 1;     /* Set if task is valid */
36 };
37
38 /* Timeout task */
39 typedef struct SilcTaskTimeoutStruct {
40   struct SilcTaskStruct header;
41   struct SilcTaskTimeoutStruct *next;
42   struct timeval timeout;
43 } *SilcTaskTimeout;
44
45 /* Fd task */
46 typedef struct {
47   struct SilcTaskStruct header;
48   unsigned int events  : 15;
49   unsigned int revents : 15;
50   SilcUInt32 fd;
51 } *SilcTaskFd;
52
53 /* Scheduler context */
54 struct SilcScheduleStruct {
55   void *internal;
56   void *app_context;               /* Application specific context */
57   SilcHashTable fd_queue;          /* FD task queue */
58   SilcList timeout_queue;          /* Timeout queue */
59   SilcList free_tasks;             /* Timeout task freelist */
60   SilcMutex lock;                  /* Scheduler lock */
61   struct timeval timeout;          /* Current timeout */
62   unsigned int max_tasks     : 28; /* Max FD tasks */
63   unsigned int has_timeout   : 1;  /* Set if timeout is set */
64   unsigned int valid         : 1;  /* Set if scheduler is valid */
65   unsigned int is_locked     : 1;  /* Set if scheduler is locked */
66   unsigned int signal_tasks  : 1;  /* Set if to dispatch signals */
67 };
68
69 /* Locks. These also blocks signals that we care about and thus guarantee
70    that while we are in scheduler no signals can happen.  This way we can
71    synchronise signals with SILC Scheduler. */
72 #define SILC_SCHEDULE_LOCK(schedule)                            \
73 do {                                                            \
74   schedule_ops.signals_block(schedule, schedule->internal);     \
75   silc_mutex_lock(schedule->lock);                              \
76 } while (0)
77 #define SILC_SCHEDULE_UNLOCK(schedule)                          \
78 do {                                                            \
79   silc_mutex_unlock(schedule->lock);                            \
80   schedule_ops.signals_unblock(schedule, schedule->internal);   \
81 } while (0)
82
83 /* Platform specific scheduler operations */
84 typedef struct {
85   /* Initializes the platform specific scheduler.  This for example initializes
86      the wakeup mechanism of the scheduler.  In multi-threaded environment
87      the scheduler needs to be wakenup when tasks are added or removed from
88      the task queues.  Returns context to the platform specific scheduler. */
89   void *(*init)(SilcSchedule schedule, void *app_context);
90
91   /* Uninitializes the platform specific scheduler context. */
92   void (*uninit)(SilcSchedule schedule, void *context);
93
94   /* System specific select(). Returns same values as normal select(). */
95   int (*select)(SilcSchedule schedule, void *context);
96
97   /* Wakes up the scheduler. This is platform specific routine */
98   void (*wakeup)(SilcSchedule schedule, void *context);
99
100   /* Register signal */
101   void (*signal_register)(SilcSchedule schedule, void *context,
102                           SilcUInt32 signal, SilcTaskCallback callback,
103                           void *callback_context);
104
105   /* Unregister signal */
106   void (*signal_unregister)(SilcSchedule schedule, void *context,
107                             SilcUInt32 signal, SilcTaskCallback callback,
108                             void *callback_context);
109
110   /* Mark signal to be called later. */
111   void (*signal_call)(SilcSchedule schedule, void *context, SilcUInt32 signal);
112
113   /* Call all signals */
114   void (*signals_call)(SilcSchedule schedule, void *context);
115
116   /* Block registered signals in scheduler. */
117   void (*signals_block)(SilcSchedule schedule, void *context);
118
119   /* Unblock registered signals in schedule. */
120   void (*signals_unblock)(SilcSchedule schedule, void *context);
121 } SilcScheduleOps;
122
123 #if defined(SILC_DEBUG)
124 /* Print scheduler statistics to stdout. */
125 void silc_schedule_stats(SilcSchedule schedule);
126 #endif /* SILC_DEBUG */
127
128 #endif /* SILCSCHEDULE_I_H */