5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 1998 - 2001 Pekka Riikonen
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.
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.
22 #include "silcincludes.h"
23 #include "silcschedule_i.h"
25 /* Calls normal select() system call. */
27 int silc_select(SilcScheduleFd fds, uint32 fds_count, struct timeval *timeout)
30 int ret, i, max_fd = 0;
35 for (i = 0; i < fds_count; i++) {
39 if (fds[i].fd > max_fd)
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);
50 ret = select(max_fd + 1, &in, &out, NULL, timeout);
54 for (i = 0; i < fds_count; i++) {
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;
69 /* Internal wakeup context. */
75 SILC_TASK_CALLBACK(silc_schedule_wakeup_cb)
77 SilcUnixWakeup wakeup = (SilcUnixWakeup)context;
80 read(wakeup->wakeup_pipe[0], &c, 1);
83 #endif /* SILC_THREADS */
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. */
92 void *silc_schedule_wakeup_init(SilcSchedule schedule)
95 SilcUnixWakeup wakeup;
97 wakeup = silc_calloc(1, sizeof(*wakeup));
99 if (pipe(wakeup->wakeup_pipe)) {
104 wakeup->wakeup_task =
105 silc_schedule_task_add(schedule, wakeup->wakeup_pipe[0],
106 silc_schedule_wakeup_cb, wakeup,
108 SILC_TASK_PRI_NORMAL);
109 if (!wakeup->wakeup_task) {
110 close(wakeup->wakeup_pipe[0]);
111 close(wakeup->wakeup_pipe[1]);
116 return (void *)wakeup;
121 /* Uninitializes the system specific wakeup. */
123 void silc_schedule_wakeup_uninit(void *context)
126 SilcUnixWakeup wakeup = (SilcUnixWakeup)context;
131 close(wakeup->wakeup_pipe[0]);
132 close(wakeup->wakeup_pipe[1]);
137 /* Wakes up the scheduler */
139 void silc_schedule_wakeup_internal(void *context)
142 SilcUnixWakeup wakeup = (SilcUnixWakeup)context;
147 write(wakeup->wakeup_pipe[1], "!", 1);