Integer type name change.
[silc.git] / lib / silcutil / unix / silcunixschedule.c
1 /*
2
3   silcunixschedule.c
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 1998 - 2001 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; either version 2 of the License, or
12   (at your option) any later version.
13   
14   This program is distributed in the hope that it will be useful,
15   but WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   GNU General Public License for more details.
18
19 */
20 /* $Id$ */
21
22 #include "silcincludes.h"
23 #include "silcschedule_i.h"
24
25 /* Calls normal select() system call. */
26
27 int silc_select(SilcScheduleFd fds, SilcUInt32 fds_count, struct timeval *timeout)
28 {
29   fd_set in, out;
30   int ret, i, max_fd = 0;
31
32   FD_ZERO(&in);
33   FD_ZERO(&out);
34
35   for (i = 0; i < fds_count; i++) {
36     if (!fds[i].events)
37       continue;
38
39     if (fds[i].fd > max_fd)
40       max_fd = fds[i].fd;
41
42     if (fds[i].events & SILC_TASK_READ)
43       FD_SET(fds[i].fd, &in);
44     if (fds[i].events & SILC_TASK_WRITE)
45       FD_SET(fds[i].fd, &out);
46
47     fds[i].revents = 0;
48   }
49
50   ret = select(max_fd + 1, &in, &out, NULL, timeout);
51   if (ret <= 0)
52     return ret;
53
54   for (i = 0; i < fds_count; i++) {
55     if (!fds[i].events)
56       continue;
57
58     if (FD_ISSET(fds[i].fd, &in))
59       fds[i].revents |= SILC_TASK_READ;
60     if (FD_ISSET(fds[i].fd, &out))
61       fds[i].revents |= SILC_TASK_WRITE;
62   }
63
64   return ret;
65 }
66
67 #ifdef SILC_THREADS
68
69 /* Internal wakeup context. */
70 typedef struct {
71   int wakeup_pipe[2];
72   SilcTask wakeup_task;
73 } *SilcUnixWakeup;
74
75 SILC_TASK_CALLBACK(silc_schedule_wakeup_cb)
76 {
77   SilcUnixWakeup wakeup = (SilcUnixWakeup)context;
78   unsigned char c;
79
80   read(wakeup->wakeup_pipe[0], &c, 1);
81 }
82
83 #endif /* SILC_THREADS */
84
85 /* Initializes the wakeup 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. This will initialize the wakeup for the scheduler.
88    Any tasks that needs to be registered must be registered to the `queue'.
89    It is quaranteed that the scheduler will automatically free any
90    registered tasks in this queue. This is system specific routine. */
91
92 void *silc_schedule_wakeup_init(SilcSchedule schedule)
93 {
94 #ifdef SILC_THREADS
95   SilcUnixWakeup wakeup;
96
97   wakeup = silc_calloc(1, sizeof(*wakeup));
98
99   if (pipe(wakeup->wakeup_pipe)) {
100     silc_free(wakeup);
101     return NULL;
102   }
103
104   wakeup->wakeup_task = 
105     silc_schedule_task_add(schedule, wakeup->wakeup_pipe[0],
106                            silc_schedule_wakeup_cb, wakeup,
107                            0, 0, SILC_TASK_FD, 
108                            SILC_TASK_PRI_NORMAL);
109   if (!wakeup->wakeup_task) {
110     close(wakeup->wakeup_pipe[0]);
111     close(wakeup->wakeup_pipe[1]);
112     silc_free(wakeup);
113     return NULL;
114   }
115
116   return (void *)wakeup;
117 #endif
118   return NULL;
119 }
120
121 /* Uninitializes the system specific wakeup. */
122
123 void silc_schedule_wakeup_uninit(void *context)
124 {
125 #ifdef SILC_THREADS
126   SilcUnixWakeup wakeup = (SilcUnixWakeup)context;
127
128   if (!wakeup)
129     return;
130
131   close(wakeup->wakeup_pipe[0]);
132   close(wakeup->wakeup_pipe[1]);
133   silc_free(wakeup);
134 #endif
135 }
136
137 /* Wakes up the scheduler */
138
139 void silc_schedule_wakeup_internal(void *context)
140 {
141 #ifdef SILC_THREADS
142   SilcUnixWakeup wakeup = (SilcUnixWakeup)context;
143
144   if (!wakeup)
145     return;
146
147   write(wakeup->wakeup_pipe[1], "!", 1);
148 #endif
149 }