replaces eventfd with pipe and closes aio branch aio

Sat, 13 Jan 2018 18:48:19 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 13 Jan 2018 18:48:19 +0100
branch
aio
changeset 192
6a145e13d933
parent 191
391ccd490d97
child 193
aa8393527b1e

replaces eventfd with pipe and closes aio branch

src/server/daemon/event_linux.c file | annotate | diff | comparison | revisions
src/server/daemon/event_linux.h file | annotate | diff | comparison | revisions
src/server/daemon/sessionhandler.c file | annotate | diff | comparison | revisions
src/server/daemon/sessionhandler.h file | annotate | diff | comparison | revisions
src/server/safs/service.c file | annotate | diff | comparison | revisions
--- a/src/server/daemon/event_linux.c	Sat Jan 13 16:44:05 2018 +0100
+++ b/src/server/daemon/event_linux.c	Sat Jan 13 18:48:19 2018 +0100
@@ -56,14 +56,18 @@
             // TODO: error
             return NULL;
         }
-        handler->eventfd = eventfd(0, 0);
-        if(handler->eventfd == 0) {
+        
+        int eventpipe[2];
+        if(pipe(eventpipe)) {
             return NULL;
         }
+        handler->eventin = eventpipe[0];
+        handler->eventout = eventpipe[1];
+        
         struct epoll_event epev;
         epev.events = EPOLLIN | EPOLLET; // input event, edge triggered
         epev.data.ptr = NULL;
-        if(epoll_ctl(handler->ep, EPOLL_CTL_ADD, handler->eventfd, &epev)) {
+        if(epoll_ctl(handler->ep, EPOLL_CTL_ADD, handler->eventin, &epev)) {
             return NULL;
         }
         
@@ -96,14 +100,25 @@
         for(int i=0;i<ret;i++) {
             Event *event = events[i].data.ptr;
             if(!event) {
-                ssize_t r = read(ev->eventfd, &event, sizeof(Event*));
-                if(r == sizeof(Event*)) {
+                char ebuf[sizeof(Event*)];
+                int ebufpos = 0;
+                char *b = ebuf;
+                while(ebufpos < sizeof(Event*)) {
+                    ssize_t r = read(ev->eventin, b + ebufpos, sizeof(Event*)-ebufpos);
+                    if(r < 0) {
+                        break;
+                    }
+                    ebufpos += r;
+                }
+                if(ebufpos == sizeof(Event*)) {
+                    intptr_t *p = (intptr_t*)b;
+                    *(&event) = (Event*)*p;
                     if(event->fn) {
                         if(!event->fn(ev, event) && event->finish) {
                             event->finish(ev, event);
                         }
                     }
-                }               
+                }              
             } else if(event->fn) {
                 int saved_ev = event->events;
                 if(!event->fn(ev, event)) {
@@ -180,7 +195,10 @@
 int event_send(EventHandler *h, Event *event) {
     event->object = 0;
     event->events = 0;
-    ssize_t r = write(h->eventfd, &event, sizeof(Event*));
+    ssize_t r = write(h->eventout, &event, sizeof(Event*));
+    if(r < sizeof(Event*)) {
+        log_ereport(LOG_FAILURE, "failed to send event: %s", strerror(errno));
+    }
     return r > 0 ? 0 : 1;
 }
 
--- a/src/server/daemon/event_linux.h	Sat Jan 13 16:44:05 2018 +0100
+++ b/src/server/daemon/event_linux.h	Sat Jan 13 18:48:19 2018 +0100
@@ -42,9 +42,13 @@
      */
     int ep;
     /*
-     * eventfd
+     * pipe read fd
      */
-    int eventfd;
+    int eventin;
+    /*
+     * pipe write fd
+     */
+    int eventout;
 };
 
 void ev_handle_events(EventHandler *ev);
--- a/src/server/daemon/sessionhandler.c	Sat Jan 13 16:44:05 2018 +0100
+++ b/src/server/daemon/sessionhandler.c	Sat Jan 13 18:48:19 2018 +0100
@@ -93,7 +93,12 @@
     free(conn);    
 }
 
-IOStream* create_connection_iostream(SessionHandler *sh, Connection *conn, pool_handle_t *pool, WSBool *ssl) {
+IOStream* create_connection_iostream(
+        SessionHandler *sh,
+        Connection *conn,
+        pool_handle_t *pool,
+        WSBool *ssl)
+{
     IOStream *io = NULL;
     if(conn->ssl) {
         io = sslstream_new(pool, conn->ssl);
--- a/src/server/daemon/sessionhandler.h	Sat Jan 13 16:44:05 2018 +0100
+++ b/src/server/daemon/sessionhandler.h	Sat Jan 13 18:48:19 2018 +0100
@@ -121,7 +121,11 @@
  * generic create_iostream function for BasicSessionHandler
  * and EventSessionHandler
  */
-IOStream* create_connection_iostream(SessionHandler *sh, Connection *conn, pool_handle_t *pool, WSBool *ssl);
+IOStream* create_connection_iostream(
+        SessionHandler *sh,
+        Connection *conn,
+        pool_handle_t *pool,
+        WSBool *ssl);
 
 
 SessionHandler* create_basic_session_handler();
--- a/src/server/safs/service.c	Sat Jan 13 16:44:05 2018 +0100
+++ b/src/server/safs/service.c	Sat Jan 13 18:48:19 2018 +0100
@@ -632,11 +632,12 @@
         length = s.st_size;
     }
     
+    int ret = REQ_NOACTION;
     if(single_range) {
         // send response header
         http_start_response(sn, rq);
         // send content
-        int ret = send_range_aio(sn, rq, fd, offset, length, NULL, 0);
+        ret = send_range_aio(sn, rq, fd, offset, length, NULL, 0);
         if(ret == REQ_PROCESSING) {
             return ret;
         }
@@ -646,16 +647,15 @@
         }
 //*/
     } else {
-        if(send_multi_range(sn, rq, fd, s.st_size, range)) {
-            // TODO: error
-        }
+        ret = send_multi_range(sn, rq, fd, s.st_size, range);
+        // TODO: error
     }
     
     // cleanup
     vfs_close(fd);
     free_range(sn, range);
 
-    return REQ_PROCEED;
+    return ret;
 }
 
 

mercurial