Initial code commit for Toolkit 1.1.
[silc.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 - 2005 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   SilcUInt32 fd;
49   unsigned int events  : 16;
50   unsigned int revents : 16;
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   SilcMutex lock;                  /* Scheduler lock */
60   struct timeval timeout;          /* Current timeout */
61   unsigned int max_tasks     : 28; /* Max FD tasks */
62   unsigned int has_timeout   : 1;  /* Set if timeout is set */
63   unsigned int valid         : 1;  /* Set if scheduler is valid */
64   unsigned int is_locked     : 1;  /* Set if scheduler is locked */
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, SilcTaskCallback callback,
107                             void *callback_context);
108
109   /* Mark signal to be called later. */
110   void (*signal_call)(SilcSchedule schedule, void *context, SilcUInt32 signal);
111
112   /* Call all signals */
113   void (*signals_call)(SilcSchedule schedule, void *context);
114
115   /* Block registered signals in scheduler. */
116   void (*signals_block)(SilcSchedule schedule, void *context);
117
118   /* Unblock registered signals in schedule. */
119   void (*signals_unblock)(SilcSchedule schedule, void *context);
120 } SilcScheduleOps;
121
122 #endif /* SILCSCHEDULE_I_H */