[IOMultiplexer] fixed possible endless loop when appending a timer to the descriptor...
[NeonServV5.git] / src / IOHandler.c
index 3c5bc3d9787c73a24c1672fbd3dbaae59f0c6bca..e4fa1dd6dd30e16c9df9e57c64926e9f7826e3e8 100644 (file)
@@ -41,6 +41,11 @@ iohandler_log_callback *iolog_backend = NULL;
 struct IODescriptor *first_descriptor = NULL;
 struct IODescriptor *timer_priority = NULL;
 
+#ifdef HAVE_PTHREAD_H
+static pthread_mutex_t io_thread_sync;
+static pthread_mutex_t io_poll_sync;
+#endif
+
 void iohandler_log(enum IOLogType type, char *text, ...) {
     va_list arg_list;
     char logBuf[MAXLOG+1];
@@ -61,16 +66,22 @@ void iohandler_log(enum IOLogType type, char *text, ...) {
 extern struct IOEngine engine_select; /* select system call (should always be useable) */
 extern struct IOEngine engine_kevent;
 extern struct IOEngine engine_epoll;
+extern struct IOEngine engine_win32;
 
 struct IOEngine *engine = NULL;
 
 static void iohandler_init_engine() {
     if(engine) return;
+    IOTHREAD_MUTEX_INIT(io_thread_sync);
+    IOTHREAD_MUTEX_INIT(io_poll_sync);
+    
     //try other engines
     if(!engine && engine_kevent.init && engine_kevent.init())
         engine = &engine_kevent;
     if(!engine && engine_epoll.init && engine_epoll.init())
         engine = &engine_epoll;
+    if(!engine && engine_win32.init && engine_win32.init())
+        engine = &engine_win32;
     
     if (!engine) {
         if(engine_select.init())
@@ -82,17 +93,10 @@ static void iohandler_init_engine() {
     }
     iohandler_log(IOLOG_DEBUG, "using %s IO engine", engine->name);
     iohandler_ssl_init();
-    #ifdef WIN32
-    WSADATA wsadata;
-    // Start Windows Sockets.
-    if (WSAStartup(MAKEWORD(2, 0), &wsadata)) {
-        iohandler_log(IOLOG_FATAL, "Unable to start Windows Sockets");
-        return;
-    }
-    #endif
 }
 
 static void iohandler_append(struct IODescriptor *descriptor) {
+    IOSYNCHRONIZE(io_thread_sync);
     struct timeval *timeout = ((descriptor->timeout.tv_sec || descriptor->timeout.tv_usec) ? &descriptor->timeout : NULL);
     if(timeout) {
         struct IODescriptor *iofd;
@@ -107,9 +111,9 @@ static void iohandler_append(struct IODescriptor *descriptor) {
                 if(timeval_is_smaler(timeout, (&iofd->timeout))) {
                     descriptor->prev = iofd->prev;
                     descriptor->next = iofd;
-                    iofd->prev = descriptor;
                     if(iofd->prev)
                         iofd->prev->next = descriptor;
+                    iofd->prev = descriptor;
                     if(set_priority)
                         timer_priority = descriptor;
                     break;
@@ -139,10 +143,12 @@ static void iohandler_append(struct IODescriptor *descriptor) {
             first_descriptor->prev = descriptor;
         first_descriptor = descriptor;
     }
+    IODESYNCHRONIZE(io_thread_sync);
 }
 
 static void iohandler_remove(struct IODescriptor *descriptor, int engine_remove) {
     //remove IODescriptor from the list
+    IOSYNCHRONIZE(io_thread_sync);
     if(descriptor->prev)
         descriptor->prev->next = descriptor->next;
     else
@@ -160,6 +166,7 @@ static void iohandler_remove(struct IODescriptor *descriptor, int engine_remove)
         free(descriptor->writebuf.buffer);
     iohandler_log(IOLOG_DEBUG, "removed IODescriptor (%d) of type `%s`", descriptor->fd, iohandler_iotype_name(descriptor->type));
     free(descriptor);
+    IODESYNCHRONIZE(io_thread_sync);
 }
 
 struct IODescriptor *iohandler_add(int sockfd, enum IOType type, struct timeval *timeout, iohandler_callback *callback) {
@@ -663,10 +670,12 @@ void iohandler_events(struct IODescriptor *iofd, int readable, int writeable) {
 
 void iohandler_poll() {
     if(engine) {
+        IOSYNCHRONIZE(io_poll_sync); //quite senceless multithread support... better support will follow
         struct timeval timeout;
         timeout.tv_sec = IO_MAX_TIMEOUT;
         timeout.tv_usec = 0;
         engine->loop(&timeout);
+        IODESYNCHRONIZE(io_poll_sync);
     }
 }