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, SilcUInt32 fds_count,
28 struct timeval *timeout)
31 int ret, i, max_fd = 0;
36 for (i = 0; i < fds_count; i++) {
40 if (fds[i].fd > max_fd)
43 if (fds[i].events & SILC_TASK_READ)
44 FD_SET(fds[i].fd, &in);
45 if (fds[i].events & SILC_TASK_WRITE)
46 FD_SET(fds[i].fd, &out);
51 ret = select(max_fd + 1, &in, &out, NULL, timeout);
55 for (i = 0; i < fds_count; i++) {
59 if (FD_ISSET(fds[i].fd, &in))
60 fds[i].revents |= SILC_TASK_READ;
61 if (FD_ISSET(fds[i].fd, &out))
62 fds[i].revents |= SILC_TASK_WRITE;
70 /* Internal wakeup context. */
76 SILC_TASK_CALLBACK(silc_schedule_wakeup_cb)
78 SilcUnixWakeup wakeup = (SilcUnixWakeup)context;
81 read(wakeup->wakeup_pipe[0], &c, 1);
84 #endif /* SILC_THREADS */
86 /* Initializes the wakeup 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. This will initialize the wakeup for the scheduler.
89 Any tasks that needs to be registered must be registered to the `queue'.
90 It is quaranteed that the scheduler will automatically free any
91 registered tasks in this queue. This is system specific routine. */
93 void *silc_schedule_wakeup_init(SilcSchedule schedule)
96 SilcUnixWakeup wakeup;
98 wakeup = silc_calloc(1, sizeof(*wakeup));
100 if (pipe(wakeup->wakeup_pipe)) {
105 wakeup->wakeup_task =
106 silc_schedule_task_add(schedule, wakeup->wakeup_pipe[0],
107 silc_schedule_wakeup_cb, wakeup,
109 SILC_TASK_PRI_NORMAL);
110 if (!wakeup->wakeup_task) {
111 close(wakeup->wakeup_pipe[0]);
112 close(wakeup->wakeup_pipe[1]);
117 return (void *)wakeup;
122 /* Uninitializes the system specific wakeup. */
124 void silc_schedule_wakeup_uninit(void *context)
127 SilcUnixWakeup wakeup = (SilcUnixWakeup)context;
132 close(wakeup->wakeup_pipe[0]);
133 close(wakeup->wakeup_pipe[1]);
138 /* Wakes up the scheduler */
140 void silc_schedule_wakeup_internal(void *context)
143 SilcUnixWakeup wakeup = (SilcUnixWakeup)context;
148 write(wakeup->wakeup_pipe[1], "!", 1);