summaryrefslogtreecommitdiff
path: root/src/ivyloop.c
diff options
context:
space:
mode:
authorfourdan2007-01-29 10:09:59 +0000
committerfourdan2007-01-29 10:09:59 +0000
commit68e3c0ed570ba3e994f8a1c4b20594372fb0b514 (patch)
tree8d6c0d6876340742ea6f49dc9faa40aef9855563 /src/ivyloop.c
parent6fc08ea508e4fa39a9bf7f5495e84a68bd0bf88a (diff)
parentf70181ffe0b37952384ff5e0ce0185541828014f (diff)
downloadivy-c-68e3c0ed570ba3e994f8a1c4b20594372fb0b514.zip
ivy-c-68e3c0ed570ba3e994f8a1c4b20594372fb0b514.tar.gz
ivy-c-68e3c0ed570ba3e994f8a1c4b20594372fb0b514.tar.bz2
ivy-c-68e3c0ed570ba3e994f8a1c4b20594372fb0b514.tar.xz
Rename protocol_v3 as trunk to avoid confusion on protocol version to use.
Diffstat (limited to 'src/ivyloop.c')
-rw-r--r--src/ivyloop.c66
1 files changed, 42 insertions, 24 deletions
diff --git a/src/ivyloop.c b/src/ivyloop.c
index ab50f75..3d1008c 100644
--- a/src/ivyloop.c
+++ b/src/ivyloop.c
@@ -6,8 +6,8 @@
*
* Main loop based on select
*
- * Authors: François-Régis Colin <fcolin@cena.fr>
- * Stéphane Chatty <chatty@cena.fr>
+ * Authors: François-Régis Colin <fcolin@cena.dgac.fr>
+ * Stéphane Chatty <chatty@cena.dgac.fr>
*
* $Id$
*
@@ -15,17 +15,16 @@
* copyright notice regarding this software
*/
-
+#ifdef WIN32
+#include <windows.h>
+#endif
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
-#ifdef WIN32
-#include <crtdbg.h>
-#include <windows.h>
-#else
+#ifndef WIN32
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
@@ -43,14 +42,13 @@
struct _channel {
Channel next;
- IVY_HANDLE fd;
+ HANDLE fd;
void *data;
int tobedeleted;
ChannelHandleDelete handle_delete;
ChannelHandleRead handle_read;
};
-
static Channel channels_list = NULL;
static int channel_initialized = 0;
@@ -58,6 +56,13 @@ static int channel_initialized = 0;
static fd_set open_fds;
static int MainLoop = 1;
+/* Hook callback & data */
+static IvyHookPtr BeforeSelect = NULL;
+static IvyHookPtr AfterSelect = NULL;
+
+static void *BeforeSelectData = NULL;
+static void *AfterSelectData = NULL;
+
#ifdef WIN32
WSADATA WsaData;
#endif
@@ -89,24 +94,21 @@ ChannelDefferedDelete ()
}
}
-Channel IvyChannelAdd (IVY_HANDLE fd, void *data,
+Channel IvyChannelAdd (HANDLE fd, void *data,
ChannelHandleDelete handle_delete,
ChannelHandleRead handle_read
)
{
Channel channel;
- IVY_LIST_ADD (channels_list, channel);
- if (!channel) {
- fprintf(stderr,"NOK Memory Alloc Error\n");
- exit(0);
- }
+ IVY_LIST_ADD_START (channels_list, channel)
channel->fd = fd;
channel->tobedeleted = 0;
channel->handle_delete = handle_delete;
channel->handle_read = handle_read;
channel->data = data;
-
+ IVY_LIST_ADD_END (channels_list, channel)
+
FD_SET (channel->fd, &open_fds);
return channel;
@@ -132,7 +134,7 @@ IvyChannelHandleExcpt (fd_set *current)
if (FD_ISSET (channel->fd, current)) {
if (channel->handle_delete)
(*channel->handle_delete)(channel->data);
-/* IvyChannelRemove (channel); */
+/* IvyChannelClose (channel); */
}
}
}
@@ -161,10 +163,9 @@ void IvyChannelInit (void)
void IvyChannelStop (void)
{
MainLoop = 0;
- ChannelDefferedDelete(); /* this will force select to exit on multithread */
}
-void IvyMainLoop(void(*hook)(void))
+void IvyMainLoop(void)
{
fd_set rdset;
@@ -172,23 +173,29 @@ void IvyMainLoop(void(*hook)(void))
int ready;
while (MainLoop) {
+
ChannelDefferedDelete();
- if (hook) (*hook)();
+
+ if (BeforeSelect)
+ (*BeforeSelect)(BeforeSelectData);
rdset = open_fds;
exset = open_fds;
ready = select(64, &rdset, 0, &exset, TimerGetSmallestTimeout());
+
+ if (AfterSelect)
+ (*AfterSelect)(AfterSelectData);
+
if (ready < 0 && (errno != EINTR)) {
fprintf (stderr, "select error %d\n",errno);
perror("select");
return;
}
- TimerScan();
- if ( MainLoop && ready > 0) {
+ TimerScan(); /* should be spliited in two part ( next timeout & callbacks */
+ if (ready > 0) {
IvyChannelHandleExcpt(&exset);
IvyChannelHandleRead(&rdset);
- continue;
}
- }
+ }
}
void IvyIdle()
@@ -216,3 +223,14 @@ void IvyIdle()
}
+
+void IvySetBeforeSelectHook(IvyHookPtr before, void *data )
+{
+ BeforeSelect = before;
+ BeforeSelectData = data;
+}
+void IvySetAfterSelectHook(IvyHookPtr after, void *data )
+{
+ AfterSelect = after;
+ AfterSelectData = data;
+}