5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 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 /* Our "select()" for WIN32. This mimics the behaviour of select() system
26 call. It does not call the Winsock's select() though. Its functions
27 are derived from GLib's g_poll() and from some old Xemacs's sys_select().
29 This makes following assumptions, which I don't know whether they
32 o SILC_TASK_WRITE is ignored, if set this will return immediately.
33 o If all arguments except timeout are NULL then this will register
34 a timeout with SetTimer and will wait just for Windows messages
36 o MsgWaitForMultipleObjects is used to wait all kind of events, this
37 includes SOCKETs and Windows messages.
38 o All Windows messages are dispatched from this function.
39 o The Operating System has Winsock 2.
43 o http://msdn.microsoft.com/library/default.asp?
44 url=/library/en-us/winui/hh/winui/messques_77zk.asp
45 o http://msdn.microsoft.com/library/default.asp?
46 url=/library/en-us/winsock/hh/winsock/apistart_9g1e.asp
47 o http://msdn.microsoft.com/library/default.asp?
48 url=/library/en-us/dnmgmt/html/msdn_getpeek.asp
49 o http://developer.novell.com/support/winsock/doc/toc.htm
53 int silc_select(SilcScheduleFd fds, SilcUInt32 fds_count, struct timeval *timeout)
55 static HANDLE handles[MAXIMUM_WAIT_OBJECTS];
56 DWORD ready, curtime, timeo;
60 if (fds_count > MAXIMUM_WAIT_OBJECTS)
61 fds_count = MAXIMUM_WAIT_OBJECTS;
63 for (i = 0; i < fds_count; i++) {
67 if (fds[i].events & SILC_TASK_READ)
68 handles[nhandles++] = (HANDLE)fds[i].fd;
70 /* If writing then just set the bit and return */
71 if (fds[i].events & SILC_TASK_WRITE) {
72 fds[i].revents = SILC_TASK_WRITE;
79 timeo = (timeout ? (timeout->tv_sec * 1000) + (timeout->tv_usec / 1000) :
82 /* If we have nothing to wait and timeout is set then register a timeout
83 and wait just for windows messages. */
84 if (nhandles == 0 && timeout) {
85 UINT timer = SetTimer(NULL, 0, timeo, NULL);
87 GetMessage(&msg, NULL, 0, 0);
88 if (msg.message == WM_TIMER) {
89 KillTimer(NULL, timer);
92 TranslateMessage(&msg);
93 DispatchMessage(&msg);
95 KillTimer(NULL, timer);
100 curtime = GetTickCount();
101 ready = MsgWaitForMultipleObjects(nhandles, handles, FALSE, timeo,
104 if (ready == WAIT_FAILED) {
105 /* Wait failed with error */
106 SILC_LOG_WARNING(("WaitForMultipleObjects() failed"));
108 } else if (ready >= WAIT_ABANDONED_0 &&
109 ready < WAIT_ABANDONED_0 + nhandles) {
110 /* Signal abandoned */
111 SILC_LOG_WARNING(("WaitForMultipleObjects() failed (ABANDONED)"));
113 } else if (ready == WAIT_TIMEOUT) {
116 } else if (ready == WAIT_OBJECT_0 + nhandles) {
117 /* Windows messages. The MSDN online says that if the application
118 creates a window then its main loop (and we're assuming that
119 it is our SILC Scheduler) must handle the Windows messages, so do
120 it here as the MSDN suggests. */
121 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
122 TranslateMessage(&msg);
123 DispatchMessage(&msg);
126 /* If timeout is set then we must update the timeout since we won't
127 return and we will give the wait another try. */
128 if (timeo != INFINITE) {
129 timeo -= GetTickCount() - curtime;
134 /* Give the wait another try */
136 } else if (ready >= WAIT_OBJECT_0 && ready < WAIT_OBJECT_0 + nhandles) {
137 /* Some other event, like SOCKET or something. */
139 /* Go through all fds even though only one was set. This is to avoid
140 starvation of high numbered fds. */
141 ready -= WAIT_OBJECT_0;
143 for (i = 0; i < fds_count; i++) {
147 if (fds[i].fd == (int)handles[ready]) {
148 fds[i].revents |= SILC_TASK_READ;
153 /* Check the status of the next handle and set its fd to the fd
154 set if data is available. */
155 while (++ready < fds_count)
156 if (WaitForSingleObject(handles[ready], 0) == WAIT_OBJECT_0)
158 } while (ready < fds_count);
168 /* Internal wakeup context. */
171 SilcTask wakeup_task;
174 SILC_TASK_CALLBACK(silc_schedule_wakeup_cb)
179 #endif /* SILC_THREADS */
181 /* Initializes the platform specific scheduler. This for example initializes
182 the wakeup mechanism of the scheduler. In multi-threaded environment
183 the scheduler needs to be wakenup when tasks are added or removed from
184 the task queues. Returns context to the platform specific scheduler. */
186 void *silc_schedule_internal_init(SilcSchedule schedule, void *app_context)
189 SilcWin32Wakeup wakeup;
191 wakeup = silc_calloc(1, sizeof(*wakeup));
193 wakeup->wakeup_sema = CreateSemaphore(NULL, 0, 100, NULL);
194 if (!wakeup->wakeup_sema) {
199 wakeup->wakeup_task =
200 silc_schedule_task_add(schedule, (int)wakeup->wakeup_sema,
201 silc_schedule_wakeup_cb, wakeup,
203 SILC_TASK_PRI_NORMAL);
204 if (!wakeup->wakeup_task) {
205 CloseHandle(wakeup->wakeup_sema);
210 return (void *)wakeup;
216 /* Uninitializes the platform specific scheduler context. */
218 void silc_schedule_internal_uninit(void *context)
221 SilcWin32Wakeup wakeup = (SilcWin32Wakeup)context;
226 CloseHandle(wakeup->wakeup_sema);
231 /* Wakes up the scheduler */
233 void silc_schedule_internal_wakeup(void *context)
236 SilcWin32Wakeup wakeup = (SilcWin32Wakeup)context;
241 ReleaseSemaphore(wakeup->wakeup_sema, 1, NULL);
245 /* Register signal */
247 void silc_schedule_internal_signal_register(void *context,
249 SilcTaskCallback callback,
250 void *callback_context)
255 /* Unregister signal */
257 void silc_schedule_internal_signal_unregister(void *context,
259 SilcTaskCallback callback,
260 void *callback_context)
265 /* Mark signal to be called later. */
267 void silc_schedule_internal_signal_call(void *context, SilcUInt32 signal)
272 /* Call all signals */
274 void silc_schedule_internal_signals_call(void *context,
275 SilcSchedule schedule)
280 /* Block registered signals in scheduler. */
282 void silc_schedule_internal_signals_block(void *context)
287 /* Unblock registered signals in schedule. */
289 void silc_schedule_internal_signals_unblock(void *context)