Added silc_likely and silc_unlikely GCC branch prediction macros.
[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 signal_tasks  : 1;  /* Set if to dispatch signals */
66 };
67
68 /* Locks. These also blocks signals that we care about and thus guarantee
69    that while we are in scheduler no signals can happen.  This way we can
70    synchronise signals with SILC Scheduler. */
71 #define SILC_SCHEDULE_LOCK(schedule)                            \
72 do {                                                            \
73   schedule_ops.signals_block(schedule, schedule->internal);     \
74   silc_mutex_lock(schedule->lock);                              \
75 } while (0)
76 #define SILC_SCHEDULE_UNLOCK(schedule)                          \
77 do {                                                            \
78   silc_mutex_unlock(schedule->lock);                            \
79   schedule_ops.signals_unblock(schedule, schedule->internal);   \
80 } while (0)
81
82 /* Platform specific scheduler operations */
83 typedef struct {
84   /* Initializes the platform specific scheduler.  This for example initializes
85      the wakeup mechanism of the scheduler.  In multi-threaded environment
86      the scheduler needs to be wakenup when tasks are added or removed from
87      the task queues.  Returns context to the platform specific scheduler. */
88   void *(*init)(SilcSchedule schedule, void *app_context);
89
90   /* Uninitializes the platform specific scheduler context. */
91   void (*uninit)(SilcSchedule schedule, void *context);
92
93   /* System specific select(). Returns same values as normal select(). */
94   int (*select)(SilcSchedule schedule, void *context);
95
96   /* Wakes up the scheduler. This is platform specific routine */
97   void (*wakeup)(SilcSchedule schedule, void *context);
98
99   /* Register signal */
100   void (*signal_register)(SilcSchedule schedule, void *context,
101                           SilcUInt32 signal, SilcTaskCallback callback,
102                           void *callback_context);
103
104   /* Unregister signal */
105   void (*signal_unregister)(SilcSchedule schedule, void *context,
106                             SilcUInt32 signal);
107
108   /* Call all signals */
109   void (*signals_call)(SilcSchedule schedule, void *context);
110
111   /* Block registered signals in scheduler. */
112   void (*signals_block)(SilcSchedule schedule, void *context);
113
114   /* Unblock registered signals in schedule. */
115   void (*signals_unblock)(SilcSchedule schedule, void *context);
116 } SilcScheduleOps;
117
118 /* The generic function to add any type of task to the scheduler.  This
119    used to be exported as is to application, but now they should use the
120    macro wrappers defined in silcschedule.h.  For Fd task the timeout must
121    be zero, for timeout task the timeout must not be zero, for signal task
122    the fd argument is the signal. */
123 SilcTask silc_schedule_task_add(SilcSchedule schedule, SilcUInt32 fd,
124                                 SilcTaskCallback callback, void *context,
125                                 long seconds, long useconds,
126                                 SilcTaskType type);
127
128 #ifdef SILC_DIST_INPLACE
129 /* Print scheduler statistics to stdout. */
130 void silc_schedule_stats(SilcSchedule schedule);
131 #endif /* SILC_DIST_INPLACE */
132
133 #endif /* SILCSCHEDULE_I_H */