Merged silc_1_0_branch to trunk.
[silc.git] / lib / silcutil / win32 / silcwin32schedule.c
index 20f9c5a569f276fc90db1df6a23acc7eb6b70079..c3dedee37270ebcd574a59be92023d798fafed3e 100644 (file)
@@ -20,6 +20,7 @@
 /* $Id$ */
 
 #include "silcincludes.h"
+#include "silcschedule_i.h"
 
 /* Our "select()" for WIN32. This mimics the behaviour of select() system
    call. It does not call the Winsock's select() though. Its functions
@@ -28,8 +29,7 @@
    This makes following assumptions, which I don't know whether they
    are correct or not:
 
-   o writefds are ignored, if set this will return immediately.
-   o exceptfds are ignored totally
+   o SILC_TASK_WRITE is ignored, if set this will return immediately.
    o If all arguments except timeout are NULL then this will register
      a timeout with SetTimer and will wait just for Windows messages
      with WaitMessage.
 
 */
 
-int silc_select(int n, fd_set *readfds, fd_set *writefds,
-               fd_set *exceptfds, struct timeval *timeout)
+int silc_select(SilcScheduleFd fds, SilcUInt32 fds_count, struct timeval *timeout)
 {
   HANDLE handles[MAXIMUM_WAIT_OBJECTS];
-  DWORD ready, curtime, timeo;
+  DWORD ready, curtime;
+  LONG timeo;
   int nhandles = 0, i;
   MSG msg;
 
-  /* Check fd sets (ignoring the exceptfds) */
-  if (readfds) {
-    for (i = 0; i < n; i++)
-      if (FD_ISSET(i, readfds))
-       handles[nhandles++] = (HANDLE)i;
+  if (fds_count > MAXIMUM_WAIT_OBJECTS)
+    fds_count = MAXIMUM_WAIT_OBJECTS;
 
-    FD_ZERO(readfds);
-  }
+  for (i = 0; i < fds_count; i++) {
+    if (!fds[i].events)
+      continue;
+
+    if (fds[i].events & SILC_TASK_READ)
+      handles[nhandles++] = (HANDLE)fds[i].fd;
 
-  /* If writefds is set then return immediately */
-  if (writefds) {
-    for (i = 0; i < n; i++)
-      if (FD_ISSET(i, writefds))
-       return 1;
+    /* If writing then just set the bit and return */
+    if (fds[i].events & SILC_TASK_WRITE) {
+      fds[i].revents = SILC_TASK_WRITE;
+      return 1;
+    }
+
+    fds[i].revents = 0;
   }
 
   timeo = (timeout ? (timeout->tv_sec * 1000) + (timeout->tv_usec / 1000) :
@@ -97,6 +100,7 @@ int silc_select(int n, fd_set *readfds, fd_set *writefds,
       KillTimer(NULL, timer);
       if (timeo != INFINITE) {
        timeo -= GetTickCount() - curtime;
+       curtime = GetTickCount();
        if (timeo < 0)
          timeo = 0;
       }
@@ -113,7 +117,6 @@ int silc_select(int n, fd_set *readfds, fd_set *writefds,
     /* Wait failed with error */
     SILC_LOG_WARNING(("WaitForMultipleObjects() failed"));
     return -1;
-
   } else if (ready >= WAIT_ABANDONED_0 &&
             ready < WAIT_ABANDONED_0 + nhandles) {
     /* Signal abandoned */
@@ -136,33 +139,38 @@ int silc_select(int n, fd_set *readfds, fd_set *writefds,
        return and we will give the wait another try. */
     if (timeo != INFINITE) {
       timeo -= GetTickCount() - curtime;
+      curtime = GetTickCount();
       if (timeo < 0)
        timeo = 0;
     }
 
     /* Give the wait another try */
    goto retry;
-  } else if (ready >= WAIT_OBJECT_0 && ready < WAIT_OBJECT_0 + nhandles &&
-            readfds) {
+  } else if (ready >= WAIT_OBJECT_0 && ready < WAIT_OBJECT_0 + nhandles) {
     /* Some other event, like SOCKET or something. */
 
     /* Go through all fds even though only one was set. This is to avoid
        starvation of high numbered fds. */
     ready -= WAIT_OBJECT_0;
-    i = 0;
     do {
-      /* Set the handle to fd set */
-      FD_SET((int)handles[ready], readfds);
-      i++;
+      for (i = 0; i < fds_count; i++) {
+       if (!fds[i].events)
+         continue;
+       
+       if (fds[i].fd == (int)handles[ready]) {
+         fds[i].revents |= SILC_TASK_READ;
+         break;
+       }
+      }
 
-      /* Check the status of the next handle and set it's fd to the fd
+      /* Check the status of the next handle and set its fd to the fd
         set if data is available. */
-      while (++ready < n)
+      while (++ready < fds_count)
        if (WaitForSingleObject(handles[ready], 0) == WAIT_OBJECT_0)
          break;
-    } while (ready < n);
+    } while (ready < fds_count);
 
-    return i;
+    return i + 1;
   }
 
   return -1;
@@ -183,14 +191,12 @@ SILC_TASK_CALLBACK(silc_schedule_wakeup_cb)
 
 #endif /* SILC_THREADS */
 
-/* Initializes the wakeup of the scheduler. In multi-threaded environment
+/* Initializes the platform specific scheduler.  This for example initializes
+   the wakeup mechanism of the scheduler.  In multi-threaded environment
    the scheduler needs to be wakenup when tasks are added or removed from
-   the task queues. This will initialize the wakeup for the scheduler.
-   Any tasks that needs to be registered must be registered to the `queue'.
-   It is guaranteed that the scheduler will automatically free any
-   registered tasks in this queue. This is system specific routine. */
+   the task queues.  Returns context to the platform specific scheduler. */
 
-void *silc_schedule_wakeup_init(void *queue)
+void *silc_schedule_internal_init(SilcSchedule schedule, void *app_context)
 {
 #ifdef SILC_THREADS
   SilcWin32Wakeup wakeup;
@@ -203,10 +209,11 @@ void *silc_schedule_wakeup_init(void *queue)
     return NULL;
   }
 
-  wakeup->wakeup_task = silc_task_register(queue, (int)wakeup->wakeup_sema,
-                                          silc_schedule_wakeup_cb, wakeup,
-                                          0, 0, SILC_TASK_FD, 
-                                          SILC_TASK_PRI_NORMAL);
+  wakeup->wakeup_task = 
+    silc_schedule_task_add(schedule, (int)wakeup->wakeup_sema,
+                          silc_schedule_wakeup_cb, wakeup,
+                          0, 0, SILC_TASK_FD, 
+                          SILC_TASK_PRI_NORMAL);
   if (!wakeup->wakeup_task) {
     CloseHandle(wakeup->wakeup_sema);
     silc_free(wakeup);
@@ -219,9 +226,9 @@ void *silc_schedule_wakeup_init(void *queue)
 #endif
 }
 
-/* Uninitializes the system specific wakeup. */
+/* Uninitializes the platform specific scheduler context. */
 
-void silc_schedule_wakeup_uninit(void *context)
+void silc_schedule_internal_uninit(void *context)
 {
 #ifdef SILC_THREADS
   SilcWin32Wakeup wakeup = (SilcWin32Wakeup)context;
@@ -236,7 +243,7 @@ void silc_schedule_wakeup_uninit(void *context)
 
 /* Wakes up the scheduler */
 
-void silc_schedule_wakeup_internal(void *context)
+void silc_schedule_internal_wakeup(void *context)
 {
 #ifdef SILC_THREADS
   SilcWin32Wakeup wakeup = (SilcWin32Wakeup)context;
@@ -247,3 +254,52 @@ void silc_schedule_wakeup_internal(void *context)
   ReleaseSemaphore(wakeup->wakeup_sema, 1, NULL);
 #endif
 }
+
+/* Register signal */
+
+void silc_schedule_internal_signal_register(void *context,
+                                            SilcUInt32 signal,
+                                            SilcTaskCallback callback,
+                                            void *callback_context)
+{
+
+}
+
+/* Unregister signal */
+
+void silc_schedule_internal_signal_unregister(void *context,
+                                              SilcUInt32 signal,
+                                              SilcTaskCallback callback,
+                                              void *callback_context)
+{
+
+}
+
+/* Mark signal to be called later. */
+
+void silc_schedule_internal_signal_call(void *context, SilcUInt32 signal)
+{
+
+}
+
+/* Call all signals */
+
+void silc_schedule_internal_signals_call(void *context,
+                                         SilcSchedule schedule)
+{
+
+}
+
+/* Block registered signals in scheduler. */
+
+void silc_schedule_internal_signals_block(void *context)
+{
+
+}
+
+/* Unblock registered signals in schedule. */
+
+void silc_schedule_internal_signals_unblock(void *context)
+{
+
+}