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"
24 /* Our "select()" for WIN32. This mimics the behaviour of select() system
25 call. It does not call the Winsock's select() though. Its functions
26 are derived from GLib's g_poll() and from some old Xemacs's sys_select().
28 This makes following assumptions, which I don't know whether they
31 o writefds are ignored, if set this will return immediately.
32 o exceptfds are ignored totally
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(int n, fd_set *readfds, fd_set *writefds,
54 fd_set *exceptfds, struct timeval *timeout)
56 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
57 DWORD ready, curtime, timeo;
61 /* Check fd sets (ignoring the exceptfds) */
63 for (i = 0; i < n; i++)
64 if (FD_ISSET(i, readfds))
65 handles[nhandles++] = (HANDLE)i;
70 /* If writefds is set then return immediately */
72 for (i = 0; i < n; i++)
73 if (FD_ISSET(i, writefds))
77 timeo = (timeout ? (timeout->tv_sec * 1000) + (timeout->tv_usec / 1000) :
80 /* If we have nothing to wait and timeout is set then register a timeout
81 and wait just for windows messages. */
82 if (nhandles == 0 && timeout) {
83 UINT timer = SetTimer(NULL, 0, timeo, NULL);
84 curtime = GetTickCount();
88 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
89 if (msg.message == WM_TIMER) {
90 KillTimer(NULL, timer);
93 TranslateMessage(&msg);
94 DispatchMessage(&msg);
97 KillTimer(NULL, timer);
98 if (timeo != INFINITE) {
99 timeo -= GetTickCount() - curtime;
103 timer = SetTimer(NULL, 0, timeo, NULL);
108 curtime = GetTickCount();
109 ready = MsgWaitForMultipleObjects(nhandles, handles, FALSE, timeo,
112 if (ready == WAIT_FAILED) {
113 /* Wait failed with error */
114 SILC_LOG_WARNING(("WaitForMultipleObjects() failed"));
117 } else if (ready >= WAIT_ABANDONED_0 &&
118 ready < WAIT_ABANDONED_0 + nhandles) {
119 /* Signal abandoned */
120 SILC_LOG_WARNING(("WaitForMultipleObjects() failed (ABANDONED)"));
122 } else if (ready == WAIT_TIMEOUT) {
125 } else if (ready == WAIT_OBJECT_0 + nhandles) {
126 /* Windows messages. The MSDN online says that if the application
127 creates a window then its main loop (and we're assuming that
128 it is our SILC Scheduler) must handle the Windows messages, so do
129 it here as the MSDN suggests. */
130 while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
131 TranslateMessage(&msg);
132 DispatchMessage(&msg);
135 /* If timeout is set then we must update the timeout since we won't
136 return and we will give the wait another try. */
137 if (timeo != INFINITE) {
138 timeo -= GetTickCount() - curtime;
143 /* Give the wait another try */
145 } else if (ready >= WAIT_OBJECT_0 && ready < WAIT_OBJECT_0 + nhandles &&
147 /* Some other event, like SOCKET or something. */
149 /* Go through all fds even though only one was set. This is to avoid
150 starvation of high numbered fds. */
151 ready -= WAIT_OBJECT_0;
154 /* Set the handle to fd set */
155 FD_SET((int)handles[ready], readfds);
158 /* Check the status of the next handle and set it's fd to the fd
159 set if data is available. */
161 if (WaitForSingleObject(handles[ready], 0) == WAIT_OBJECT_0)
173 /* Internal wakeup context. */
176 SilcTask wakeup_task;
179 SILC_TASK_CALLBACK(silc_schedule_wakeup_cb)
184 #endif /* SILC_THREADS */
186 /* Initializes the wakeup of the scheduler. In multi-threaded environment
187 the scheduler needs to be wakenup when tasks are added or removed from
188 the task queues. This will initialize the wakeup for the scheduler.
189 Any tasks that needs to be registered must be registered to the `queue'.
190 It is guaranteed that the scheduler will automatically free any
191 registered tasks in this queue. This is system specific routine. */
193 void *silc_schedule_wakeup_init(void *queue)
196 SilcWin32Wakeup wakeup;
198 wakeup = silc_calloc(1, sizeof(*wakeup));
200 wakeup->wakeup_sema = CreateSemaphore(NULL, 0, 100, NULL);
201 if (!wakeup->wakeup_sema) {
206 wakeup->wakeup_task = silc_task_register(queue, (int)wakeup->wakeup_sema,
207 silc_schedule_wakeup_cb, wakeup,
209 SILC_TASK_PRI_NORMAL);
210 if (!wakeup->wakeup_task) {
211 CloseHandle(wakeup->wakeup_sema);
216 return (void *)wakeup;
222 /* Uninitializes the system specific wakeup. */
224 void silc_schedule_wakeup_uninit(void *context)
227 SilcWin32Wakeup wakeup = (SilcWin32Wakeup)context;
232 CloseHandle(wakeup->wakeup_sema);
237 /* Wakes up the scheduler */
239 void silc_schedule_wakeup_internal(void *context)
242 SilcWin32Wakeup wakeup = (SilcWin32Wakeup)context;
247 ReleaseSemaphore(wakeup->wakeup_sema, 1, NULL);